コード例 #1
0
        protected void CleanProperties(IDictionary <string, ApiObject> apiObjects)
        {
            var keys            = apiObjects.Keys.ToList();
            var apiObjectsCount = keys.Count - 1;

            for (var i = apiObjectsCount; i >= 0; i--)
            {
                var apiObject = apiObjects[keys[i]];
                var count     = apiObject.Properties.Count;
                for (var index = count - 1; index >= 0; index--)
                {
                    var prop = apiObject.Properties[index];
                    var type = prop.Type;
                    if (!string.IsNullOrWhiteSpace(type) && IsCollectionType(type))
                    {
                        type = CollectionTypeHelper.GetBaseType(type);
                    }

                    if (prop.IsAdditionalProperties)
                    {
                        continue;
                    }

                    if (!NetTypeMapper.IsPrimitiveType(type) && schemaResponseObjects.All(o => o.Value.Name != type) &&
                        schemaRequestObjects.All(o => o.Value.Name != type) &&
                        enums.All(e => e.Value.Name != type.TrimEnd('?')) &&
                        schemaObjects.All(o => o.Value.Name != type))
                    {
                        apiObject.Properties.Remove(prop);
                    }
                }
            }
        }
コード例 #2
0
        private void AddProperty(Resource resource, Method method, string key, Response response, ICollection <Property> properties, string fullUrl)
        {
            var mimeType = GeneratorServiceHelper.GetMimeType(response);

            if (mimeType == null)
            {
                return;
            }

            var type = GetReturnTypeFromResponse(method, resource, mimeType, key, response.Code, fullUrl);

            if (string.IsNullOrWhiteSpace(type))
            {
                return;
            }

            var property = new Property
            {
                Name        = CollectionTypeHelper.GetBaseType(type),
                Description = response.Description + " " + mimeType.Description,
                Example     = mimeType.Example,
                Type        = type,
                StatusCode  = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), response.Code),
                JSONSchema  = mimeType.Schema == null ? null : mimeType.Schema.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "").Replace("\"", "\\\"")
            };

            properties.Add(property);
        }
コード例 #3
0
        public static string GetTypeFromApiObject(ApiObject apiObject)
        {
            if (apiObject.IsArray)
            {
                if (string.IsNullOrWhiteSpace(apiObject.Type))
                {
                    return(CollectionTypeHelper.GetCollectionType(apiObject.Name));
                }

                if (CollectionTypeHelper.IsCollection(apiObject.Type))
                {
                    return(apiObject.Type);
                }

                return(CollectionTypeHelper.GetCollectionType(apiObject.Type));
            }
            if (apiObject.IsMap)
            {
                return(apiObject.Type);
            }

            if (apiObject.IsScalar)
            {
                return(apiObject.Properties.First().Type);
            }

            if (!string.IsNullOrWhiteSpace(apiObject.Type) && apiObject.Type != apiObject.Name)
            {
                return(apiObject.Type);
            }

            return(apiObject.Name);
        }
コード例 #4
0
        private ApiObject ParseArray(string key, RamlType ramlType)
        {
            var typeOfArray = GetTypeOfArray(key, ramlType);

            var baseType = CollectionTypeHelper.GetBaseType(typeOfArray);

            if (!NetTypeMapper.IsPrimitiveType(baseType) &&
                ramlType.Array.Items != null && ramlType.Array.Items.Type == "object")
            {
                if (baseType == typeOfArray)
                {
                    baseType = typeOfArray + "Item";
                }

                baseType = NetNamingMapper.GetObjectName(baseType);

                var itemType = ParseNestedType(ramlType.Array.Items, baseType);
                schemaObjects.Add(baseType, itemType);
                typeOfArray = CollectionTypeHelper.GetCollectionType(baseType);
            }

            return(new ApiObject
            {
                IsArray = true,
                Name = NetNamingMapper.GetObjectName(key),
                Description = ramlType.Description,
                Example = ramlType.Example,
                Type = typeOfArray
            });
        }
コード例 #5
0
        public static string DecodeRaml1Type(string type)
        {
            // TODO: can I handle this better ?
            if (type.Contains("(") || type.Contains("|"))
            {
                return("object");
            }

            if (type.EndsWith("[][]")) // array of arrays
            {
                var strippedType = type.Substring(0, type.Length - 4);
                if (NetTypeMapper.Map(strippedType) == null)
                {
                    strippedType = NetNamingMapper.GetObjectName(strippedType);
                }

                var decodeRaml1Type = CollectionTypeHelper.GetCollectionType(CollectionTypeHelper.GetCollectionType(strippedType));
                return(decodeRaml1Type);
            }

            if (type.EndsWith("[]")) // array
            {
                var strippedType = type.Substring(0, type.Length - 2);

                if (NetTypeMapper.Map(strippedType) == null)
                {
                    strippedType = NetNamingMapper.GetObjectName(strippedType);
                }

                var decodeRaml1Type = CollectionTypeHelper.GetCollectionType(strippedType);
                return(decodeRaml1Type);
            }

            if (type.EndsWith("{}")) // Map
            {
                var subtype = type.Substring(0, type.Length - 2);
                var netType = NetTypeMapper.Map(subtype);
                if (netType != null)
                {
                    return("IDictionary<string, " + netType + ">");
                }

                return("IDictionary<string, " + NetNamingMapper.GetObjectName(subtype) + ">");
            }

            if (CollectionTypeHelper.IsCollection(type))
            {
                return(type);
            }

            return(NetNamingMapper.GetObjectName(type));
        }
コード例 #6
0
        private static string GetTypeFromApiObject(ApiObject apiObject)
        {
            if (!apiObject.IsArray)
            {
                return(apiObject.Name);
            }

            if (apiObject.Type == null)
            {
                return(CollectionTypeHelper.GetCollectionType(apiObject.Name));
            }

            return(CollectionTypeHelper.GetCollectionType(apiObject.Type));
        }
コード例 #7
0
        private bool IsUsedAsReferenceInAnyObject(ApiObject requestObj)
        {
            return(schemaObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name ||
                                                                         x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) ||
                                                                         x.Type == requestObj.BaseClass ||
                                                                         x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass))

                   || schemaRequestObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name ||
                                                                                   x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) ||
                                                                                   x.Type == requestObj.BaseClass ||
                                                                                   x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass))

                   || schemaResponseObjects.SelectMany(o => o.Value.Properties).Any(x => x.Type == requestObj.Name ||
                                                                                    x.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name) ||
                                                                                    x.Type == requestObj.BaseClass ||
                                                                                    x.Type == CollectionTypeHelper.GetCollectionType(requestObj.BaseClass)));
        }
コード例 #8
0
        private static string GetTypeOfArray(string key, RamlType ramlType)
        {
            if (!string.IsNullOrWhiteSpace(ramlType.Type))
            {
                var pureType = ramlType.Type.EndsWith("[]") ? ramlType.Type.Substring(0, ramlType.Type.Length - 2) : ramlType.Type;

                if (pureType != "array" && pureType != "object")
                {
                    if (NetTypeMapper.IsPrimitiveType(pureType))
                    {
                        pureType = NetTypeMapper.Map(pureType);
                    }
                    else
                    {
                        pureType = NetNamingMapper.GetObjectName(pureType);
                    }

                    return(CollectionTypeHelper.GetCollectionType(pureType));
                }
            }
            if (!string.IsNullOrWhiteSpace(ramlType.Array.Items.Type))
            {
                if (ramlType.Array.Items.Type != "object")
                {
                    var netType = ramlType.Array.Items.Type;
                    if (NetTypeMapper.IsPrimitiveType(netType))
                    {
                        netType = NetTypeMapper.Map(netType);
                    }
                    else
                    {
                        netType = NetNamingMapper.GetObjectName(netType);
                    }

                    return(CollectionTypeHelper.GetCollectionType(netType));
                }
            }

            if (!string.IsNullOrWhiteSpace(ramlType.Array.Items.Name))
            {
                return(CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(ramlType.Array.Items.Name)));
            }

            return(NetNamingMapper.GetObjectName(key));
        }
コード例 #9
0
        private ApiObject ParseArray(string key, RamlType ramlType)
        {
            var typeOfArray = GetTypeOfArray(key, ramlType);

            var baseType = CollectionTypeHelper.GetBaseType(typeOfArray);

            if (!NetTypeMapper.IsPrimitiveType(baseType) &&
                ramlType.Array.Items != null && ramlType.Array.Items.Type == "object")
            {
                if (baseType == typeOfArray)
                {
                    baseType = typeOfArray + "Item";
                }

                baseType = NetNamingMapper.GetObjectName(baseType);

                var itemType = ParseNestedType(ramlType.Array.Items, baseType);
                if (schemaObjects.ContainsKey(baseType) && !UniquenessHelper.HasSameProperties(itemType, schemaObjects, baseType, emptyDic, emptyDic))
                {
                    baseType      = UniquenessHelper.GetUniqueName(schemaObjects, baseType, emptyDic, emptyDic);
                    itemType.Name = baseType;
                }
                if (!schemaObjects.ContainsKey(baseType))
                {
                    schemaObjects.Add(baseType, itemType);
                }

                typeOfArray = CollectionTypeHelper.GetCollectionType(baseType);
            }

            return(new ApiObject
            {
                IsArray = true,
                Name = NetNamingMapper.GetObjectName(key),
                Description = ramlType.Description,
                Example = ramlType.Example,
                Type = typeOfArray
            });
        }
コード例 #10
0
        private ApiObject ParseUnion(string key, RamlType ramlType)
        {
            var name      = NetNamingMapper.GetObjectName(key);
            var apiObject = new ApiObject
            {
                IsUnionType = true,
                Name        = name,
                Description = ramlType.Description,
                Example     = GetExample(ramlType.Example, ramlType.Examples),
                Type        = NetNamingMapper.GetObjectName(key)
            };

            var originalType = ramlType.Type;

            var isArray = false;

            if (originalType.StartsWith("(") && originalType.EndsWith(")[]"))
            {
                isArray      = true;
                originalType = originalType.Substring(0, originalType.Length - 2);
            }
            originalType = originalType.Replace("(", string.Empty).Replace(")", string.Empty);

            var types = originalType.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var type in types)
            {
                apiObject.Properties.Add(new Property(name)
                {
                    Name = NetNamingMapper.GetPropertyName(type.Trim()),
                    Type = isArray
                        ? CollectionTypeHelper.GetCollectionType(RamlTypesHelper.DecodeRaml1Type(type.Trim()))
                        : RamlTypesHelper.DecodeRaml1Type(type.Trim())
                });
            }
            return(apiObject);
        }
コード例 #11
0
        private string DecodeRequestRaml1Type(string type)
        {
            // TODO: can I handle this better ?
            if (type.Contains("(") || type.Contains("|"))
            {
                return("string");
            }

            if (type.EndsWith("[][]")) // array of arrays
            {
                var subtype = type.Substring(0, type.Length - 4);
                if (NetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NetTypeMapper.Map(subtype);
                }
                else
                {
                    subtype = NetNamingMapper.GetObjectName(subtype);
                }

                return(CollectionTypeHelper.GetCollectionType(CollectionTypeHelper.GetCollectionType(subtype)));
            }

            if (type.EndsWith("[]")) // array
            {
                var subtype = type.Substring(0, type.Length - 2);

                if (NetTypeMapper.IsPrimitiveType(subtype))
                {
                    subtype = NetTypeMapper.Map(subtype);
                }
                else
                {
                    subtype = NetNamingMapper.GetObjectName(subtype);
                }


                return(CollectionTypeHelper.GetCollectionType(subtype));
            }

            if (type.EndsWith("{}")) // Map
            {
                var subtype = type.Substring(0, type.Length - 2);
                var netType = NetTypeMapper.Map(subtype);
                if (!string.IsNullOrWhiteSpace(netType))
                {
                    return("IDictionary<string, " + netType + ">");
                }

                var apiObject = GetRequestApiObjectByName(subtype);
                if (apiObject != null)
                {
                    return("IDictionary<string, " + RamlTypesHelper.GetTypeFromApiObject(apiObject) + ">");
                }

                return("IDictionary<string, object>");
            }

            if (NetTypeMapper.IsPrimitiveType(type))
            {
                return(NetTypeMapper.Map(type));
            }

            if (CollectionTypeHelper.IsCollection(type))
            {
                return(type);
            }

            return(NetNamingMapper.GetObjectName(type));
        }
コード例 #12
0
 private bool IsCollectionType(string type)
 {
     return(CollectionTypeHelper.IsCollection(type));
 }
コード例 #13
0
        private IList <Property> GetProperties(IDictionary <string, RamlType> properties)
        {
            var props = new List <Property>();

            foreach (var kv in properties)
            {
                var prop = kv.Value;
                if (prop.Scalar != null)
                {
                    var newProp = GetPropertyFromScalar(prop, kv);
                    props.Add(newProp);
                    continue;
                }
                if (prop.Object != null)
                {
                    var name = NetNamingMapper.GetPropertyName(kv.Key);
                    if (!schemaObjects.ContainsKey(name) && !schemaObjects.ContainsKey(prop.Type))
                    {
                        var newApiObject = GetApiObjectFromObject(prop, name);
                        schemaObjects.Add(name, newApiObject);
                        props.Add(new Property {
                            Name = name, Type = name, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                        });
                    }
                    else
                    {
                        ApiObject apiObject;
                        if (schemaObjects.ContainsKey(prop.Type))
                        {
                            apiObject = schemaObjects[prop.Type];
                        }
                        else
                        {
                            apiObject = schemaObjects[name];
                        }

                        props.Add(new Property {
                            Name = name, Type = apiObject.Name, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                        });
                    }

                    continue;
                }
                if (prop.Array != null)
                {
                    var name = NetNamingMapper.GetPropertyName(kv.Key);
                    var type = kv.Value.Type;
                    if (kv.Value.Array.Items != null)
                    {
                        if (NetTypeMapper.IsPrimitiveType(kv.Value.Array.Items.Type))
                        {
                            type = CollectionTypeHelper.GetCollectionType(NetTypeMapper.Map(kv.Value.Array.Items.Type));
                        }
                        else
                        {
                            var obj = ParseArray(kv.Key, kv.Value);
                            // type = CollectionTypeHelper.GetCollectionType(obj.Type);
                            type = obj.Type;
                        }
                    }
                    if (type.EndsWith("[]"))
                    {
                        type = type.Substring(0, type.Length - 2);
                        if (!NetTypeMapper.IsPrimitiveType(type))
                        {
                            type = NetNamingMapper.GetObjectName(type);
                        }

                        type = CollectionTypeHelper.GetCollectionType(type);
                    }

                    props.Add(new Property {
                        Name = name, Type = type, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                    });
                }
            }
            return(props);
        }
コード例 #14
0
        private IList <Property> GetProperties(IDictionary <string, RamlType> properties, string className)
        {
            var props = new List <Property>();

            foreach (var kv in properties)
            {
                var prop = kv.Value;
                if (prop.Scalar != null)
                {
                    var newProp = GetPropertyFromScalar(prop, kv, className);
                    props.Add(newProp);
                    continue;
                }
                if (prop.Object != null)
                {
                    var name = NetNamingMapper.GetPropertyName(kv.Key);
                    if (!schemaObjects.ContainsKey(name) && !schemaObjects.ContainsKey(prop.Type))
                    {
                        var newApiObject = GetApiObjectFromObject(prop, name);
                        if (schemaObjects.ContainsKey(name) && !UniquenessHelper.HasSameProperties(newApiObject, schemaObjects, name, emptyDic, emptyDic))
                        {
                            name = UniquenessHelper.GetUniqueName(schemaObjects, name, emptyDic, emptyDic);
                            newApiObject.Name = name;
                        }
                        if (!schemaObjects.ContainsKey(name))
                        {
                            schemaObjects.Add(name, newApiObject);
                        }
                        props.Add(new Property(className)
                        {
                            Name = name, Type = name, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                        });
                    }
                    else
                    {
                        ApiObject apiObject;
                        if (schemaObjects.ContainsKey(prop.Type))
                        {
                            apiObject = schemaObjects[prop.Type];
                        }
                        else
                        {
                            apiObject = schemaObjects[name];
                        }

                        props.Add(new Property(className)
                        {
                            Name = name, Type = apiObject.Name, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                        });
                    }

                    continue;
                }
                if (prop.Array != null)
                {
                    var name = NetNamingMapper.GetPropertyName(kv.Key);
                    var type = kv.Value.Type;
                    if (kv.Value.Array.Items != null)
                    {
                        if (kv.Value.Array.Items.Object == null && NetTypeMapper.IsPrimitiveType(kv.Value.Array.Items.Type))
                        {
                            type = CollectionTypeHelper.GetCollectionType(NetTypeMapper.Map(kv.Value.Array.Items.Type));
                        }
                        else
                        {
                            var obj = ParseArray(kv.Key, kv.Value);
                            // type = CollectionTypeHelper.GetCollectionType(obj.Type);
                            type = obj.Type;
                        }
                    }
                    if (type.EndsWith("[]"))
                    {
                        type = type.Substring(0, type.Length - 2);
                        if (!NetTypeMapper.IsPrimitiveType(type))
                        {
                            type = NetNamingMapper.GetObjectName(type);
                        }

                        type = CollectionTypeHelper.GetCollectionType(type);
                    }

                    props.Add(new Property(className)
                    {
                        Name = name, Type = type, Required = prop.Required, OriginalName = kv.Key.TrimEnd('?')
                    });
                }
                if (!string.IsNullOrWhiteSpace(prop.Type))
                {
                    var newProp = new Property(className)
                    {
                        Type         = NetNamingMapper.GetObjectName(kv.Key),
                        Name         = NetNamingMapper.GetPropertyName(kv.Key),
                        Required     = prop.Required,
                        Example      = prop.Example,
                        Description  = prop.Description,
                        OriginalName = kv.Key.TrimEnd('?')
                    };
                    props.Add(newProp);
                    continue;
                }
            }
            return(props);
        }
コード例 #15
0
 public bool IsUsedAsParameterInAnyMethod(IEnumerable <ClassObject> classes, ApiObject requestObj)
 {
     return(classes.Any(c => c.Methods
                        .Any(m => m.Parameter != null &&
                             (m.Parameter.Type == requestObj.Name || m.Parameter.Type == CollectionTypeHelper.GetCollectionType(requestObj.Name)))));
 }
コード例 #16
0
 public bool IsUsedAsResponseInAnyMethod(IEnumerable <ClassObject> classes, ApiObject requestObj)
 {
     return(classes.Any(c => c.Methods.Any(m => m.ReturnType == requestObj.Name || m.ReturnType == CollectionTypeHelper.GetCollectionType(requestObj.Name))));
 }