Ejemplo n.º 1
0
        private void ParseProperties(IDictionary <string, ApiObject> objects, ICollection <Property> props, IDictionary <string, JsonSchema> properties)
        {
            if (properties == null)
            {
                return;
            }

            foreach (var property in properties)
            {
                if (property.Value.Type == null || property.Value.Type == JsonSchemaType.Null ||
                    property.Value.Type == JsonSchemaType.None)
                {
                    continue;
                }

                var key = property.Key;
                if (string.IsNullOrWhiteSpace(key))
                {
                    key = GetUniqueName(props);
                }

                var prop = new Property
                {
                    Name        = NetNamingMapper.GetPropertyName(key),
                    Type        = NetTypeMapper.Map(property.Value.Type),
                    Description = property.Value.Description
                };

                ParseComplexTypes(objects, property.Value, prop, property, key);
                props.Add(prop);
            }
        }
Ejemplo n.º 2
0
        public static IList <Property> ConvertHeadersToProperties(IEnumerable <Parameter> headers)
        {
            var properties = new List <Property>();

            if (headers == null)
            {
                return(properties);
            }

            foreach (var header in headers)
            {
                var description = ParserHelpers.RemoveNewLines(header.Description);

                var type       = NetTypeMapper.Map(header.Type);
                var typeSuffix = (type == "string" || header.Required ? "" : "?");

                properties.Add(new Property
                {
                    Type         = type + typeSuffix,
                    Name         = NetNamingMapper.GetPropertyName(header.DisplayName),
                    OriginalName = header.DisplayName,
                    Description  = description,
                    Example      = header.Example,
                    Required     = header.Required
                });
            }
            return(properties);
        }
Ejemplo n.º 3
0
        private Property GetPropertyFromScalar(RamlType prop, KeyValuePair <string, RamlType> kv)
        {
            if (prop.Scalar.Enum != null && prop.Scalar.Enum.Any())
            {
                if (!enums.ContainsKey(kv.Key))
                {
                    var apiEnum = new ApiEnum
                    {
                        Name        = NetNamingMapper.GetPropertyName(kv.Key),
                        Description = kv.Value.Description,
                        Values      = GetEnumValues(kv.Value.Scalar)
                    };
                    enums.Add(kv.Key, apiEnum);
                }
            }

            return(new Property
            {
                Minimum = ToDouble(prop.Scalar.Minimum),
                Maximum = ToDouble(prop.Scalar.Maximum),
                Type = GetPropertyType(prop, kv),
                MaxLength = prop.Scalar.MaxLength,
                MinLength = prop.Scalar.MinLength,
                Name = NetNamingMapper.GetPropertyName(kv.Key),
                Required = prop.Required || kv.Value.Scalar.Required,
                Example = prop.Example,
                Description = prop.Description,
                IsEnum = prop.Scalar.Enum != null && prop.Scalar.Enum.Any(),
                OriginalName = kv.Key.TrimEnd('?')
            });
        }
        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 = responseTypesService.GetResponseType(method, resource, mimeType, key, response.Code, fullUrl);

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

            var name = NetNamingMapper.GetPropertyName(CollectionTypeHelper.GetBaseType(type));

            if (properties.Any(p => p.Name == name))
            {
                name = name + response.Code;
            }

            var property = new Property
            {
                Name        = name,
                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);
        }
Ejemplo n.º 5
0
        private IList <Property> ParseSchema(IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> schema, IDictionary <string, ApiObject> objects)
        {
            var props = new List <Property>();

            foreach (var kv in schema)
            {
                var prop = new Property
                {
                    Name        = NetNamingMapper.GetPropertyName(kv.Key),
                    Type        = NetTypeMapper.Map(kv.Value.Type),
                    Description = kv.Value.Description
                };
                ParseComplexTypes(objects, kv.Value, prop, kv);
                props.Add(prop);
            }
            return(props);
        }
        private string GetPropertyType(RamlType prop, KeyValuePair <string, RamlType> kv)
        {
            if (string.IsNullOrWhiteSpace(prop.Type))
            {
                return("string");
            }

            if (prop.Type == "object" || (prop.Scalar.Enum != null && prop.Scalar.Enum.Any()))
            {
                return(NetNamingMapper.GetPropertyName(kv.Key));
            }

            var propertyType = NetTypeMapper.GetNetType(prop.Scalar.Type, prop.Scalar.Format);

            if (propertyType != null)
            {
                if (!prop.Required && !prop.Scalar.Required && propertyType != "string" && prop.Type != "file")
                {
                    return(propertyType + "?");
                }

                return(propertyType);
            }

            if (schemaObjects.ContainsKey(prop.Type))
            {
                var obj = schemaObjects[prop.Type];
                if (obj.IsScalar)
                {
                    return(obj.Properties.First().Type);
                }

                return(obj.Type);
            }

            if (enums.ContainsKey(prop.Type))
            {
                return(prop.Type);
            }


            return("object");
        }
Ejemplo n.º 7
0
        public IList <Property> ConvertParametersToProperties(IEnumerable <KeyValuePair <string, Parameter> > parameters)
        {
            var properties = new List <Property>();

            foreach (var parameter in parameters.Where(parameter => parameter.Value != null && parameter.Value.Type != null))
            {
                var description = ParserHelpers.RemoveNewLines(parameter.Value.Description);

                properties.Add(new Property
                {
                    Type         = GetType(parameter.Value),
                    Name         = NetNamingMapper.GetPropertyName(parameter.Key),
                    OriginalName = parameter.Key,
                    Description  = description,
                    Example      = parameter.Value.Example,
                    Required     = parameter.Value.Required
                });
            }
            return(properties);
        }
Ejemplo n.º 8
0
        private void ParseProperties(IDictionary <string, ApiObject> objects, IList <Property> props, IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> properties)
        {
            foreach (var property in properties)
            {
                if (property.Value.Type == null || property.Value.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Null ||
                    property.Value.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.None)
                {
                    continue;
                }

                var prop = new Property
                {
                    Name        = NetNamingMapper.GetPropertyName(property.Key),
                    Type        = NetTypeMapper.Map(property.Value.Type),
                    Description = property.Value.Description
                };

                ParseComplexTypes(objects, property.Value, prop, property);
                props.Add(prop);
            }
        }
        public static IList <Property> ConvertParametersToProperties(IEnumerable <KeyValuePair <string, Parameter> > parameters)
        {
            var properties = new List <Property>();

            foreach (var parameter in parameters)
            {
                var description = ParserHelpers.RemoveNewLines(parameter.Value.Description);

                properties.Add(new Property
                {
                    Type =
                        NetTypeMapper.Map(parameter.Value.Type) +
                        (NetTypeMapper.Map(parameter.Value.Type) == "string" || parameter.Value.Required ? "" : "?"),
                    Name        = NetNamingMapper.GetPropertyName(parameter.Key),
                    Description = description,
                    Example     = parameter.Value.Example,
                    Required    = parameter.Value.Required
                });
            }
            return(properties);
        }
        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);
        }
Ejemplo n.º 11
0
        public static IList <Property> ConvertHeadersToProperties(IDictionary <string, Parameter> headers)
        {
            var properties = new List <Property>();

            if (headers == null)
            {
                return(properties);
            }

            foreach (var header in headers)
            {
                var description = ParserHelpers.RemoveNewLines(header.Value.Description);

                var type       = "string";
                var typeSuffix = "";
                if (header.Value.Type != null && header.Value.Type.Contains("|"))
                {
                    type = "object";
                }
                else
                {
                    type       = NetTypeMapper.GetNetType(header.Value.Type, header.Value.Format);
                    typeSuffix = (type == "string" || header.Value.Required ? "" : "?");
                }

                properties.Add(new Property
                {
                    Type         = type + typeSuffix,
                    Name         = NetNamingMapper.GetPropertyName(header.Key),
                    OriginalName = header.Value.DisplayName,
                    Description  = description,
                    Example      = header.Value.Example,
                    Required     = header.Value.Required
                });
            }
            return(properties);
        }
Ejemplo n.º 12
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);
        }
        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);
        }