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));
        }
Example #2
0
        private void ParseComplexTypes(IDictionary <string, ApiObject> objects, JsonSchema schema, Property prop, KeyValuePair <string, JsonSchema> property, string key)
        {
            if (schema.Type == JsonSchemaType.Object && schema.Properties != null)
            {
                ParseObject(key, schema.Properties, objects);
                prop.Type = NetNamingMapper.GetObjectName(key);
            }

            if (schema.Type == JsonSchemaType.Array)
            {
                ParseArray(objects, schema, prop, property);
            }
        }
Example #3
0
        public ApiObject ParseObject(string key, string value, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings)
        {
            var obj = ParseSchema(key, value, objects, warnings);

            if (obj == null)
            {
                return(null);
            }

            obj.Name = NetNamingMapper.GetObjectName(key);

            return(obj);
        }
Example #4
0
        private void ParseObject(string key, IDictionary <string, Newtonsoft.JsonV4.Schema.JsonSchema> schema, IDictionary <string, ApiObject> objects)
        {
            var obj = new ApiObject
            {
                Name       = NetNamingMapper.GetObjectName(key),
                Properties = ParseSchema(schema, objects)
            };

            // Avoid duplicated keys and names
            if (objects.ContainsKey(key) || objects.Any(o => o.Value.Name == obj.Name) || !obj.Properties.Any())
            {
                return;
            }

            objects.Add(key, obj);
        }
Example #5
0
        private void ParseArray(IDictionary <string, ApiObject> objects, Newtonsoft.JsonV4.Schema.JsonSchema schema, Property prop, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property)
        {
            var netType = NetTypeMapper.Map(schema.Items.First().Type);

            if (netType != null)
            {
                prop.Type = netType + "[]";
            }
            else
            {
                prop.Type = NetNamingMapper.GetObjectName(property.Key) + "[]";
                foreach (var item in schema.Items)
                {
                    ParseObject(property.Key, item.Properties, objects);
                }
            }
        }
        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));
        }
        private ApiObject ParseScalar(string key, RamlType ramlType)
        {
            if (ramlType.Scalar.Enum != null && ramlType.Scalar.Enum.Any())
            {
                if (enums.ContainsKey(key))
                {
                    return(null);
                }

                enums.Add(key, new ApiEnum
                {
                    Name        = NetNamingMapper.GetObjectName(key),
                    Description = ramlType.Description,
                    Values      = GetEnumValues(ramlType.Scalar, NetNamingMapper.GetObjectName(key))
                });
                return(null);
            }

            var type = GetScalarType(ramlType);

            var name = NetNamingMapper.GetObjectName(key);

            return(new ApiObject
            {
                Type = NetNamingMapper.GetObjectName(key),
                Name = name,
                Example = ramlType.Example,
                Description = ramlType.Description,
                Properties = new List <Property>
                {
                    new Property(name)
                    {
                        Name = "Value",
                        Type = type,
                        Minimum = (double?)ramlType.Scalar.Minimum,
                        Maximum = (double?)ramlType.Scalar.Maximum,
                        MinLength = ramlType.Scalar.MinLength,
                        MaxLength = ramlType.Scalar.MaxLength,
                        OriginalName = key,
                        Required = ramlType.Scalar.Required || ramlType.Required
                    }
                },
                IsScalar = true,
            });
        }
        private ApiObject GetApiObjectFromObject(RamlType ramlType, string defaultName = "")
        {
            var name = defaultName == "" ? ramlType.Name : defaultName;

            if (string.IsNullOrWhiteSpace(name))
            {
                name = "Type" + DateTime.Now.Ticks;
            }

            return(new ApiObject
            {
                Type = NetNamingMapper.GetObjectName(name),
                Name = NetNamingMapper.GetObjectName(name),
                BaseClass = ramlType.Type != "object" ? NetNamingMapper.GetObjectName(ramlType.Type) : string.Empty,
                Description = ramlType.Description,
                Example = GetExample(ramlType.Example, ramlType.Examples),
                Properties = GetProperties(ramlType.Object.Properties)
            });
        }
        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
            });
        }
        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);
        }
Example #11
0
        public ApiObject ParseObject(string key, string value, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings, IDictionary <string, ApiEnum> enums, IDictionary <string, ApiObject> otherObjects, IDictionary <string, ApiObject> schemaObjects, string targetNamespace)
        {
            var obj = ParseSchema(key, value, objects, warnings, enums, otherObjects, schemaObjects, targetNamespace);

            if (obj == null)
            {
                return(null);
            }

            obj.Name = NetNamingMapper.GetObjectName(key);

            if (schemaObjects.Values.Any(o => o.Name == obj.Name) || objects.Values.Any(o => o.Name == obj.Name) ||
                otherObjects.Values.Any(o => o.Name == obj.Name))
            {
                if (UniquenessHelper.HasSameProperties(obj, objects, key, otherObjects, schemaObjects))
                {
                    return(null);
                }

                obj.Name = UniquenessHelper.GetUniqueName(objects, obj.Name, otherObjects, schemaObjects);
            }

            return(obj);
        }
        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));
        }
Example #13
0
        protected string GetUniqueObjectName(Resource resource, Resource parent)
        {
            string objectName;

            if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
            {
                objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource));
            }
            else
            {
                if (resource.RelativeUri == "/")
                {
                    objectName = "RootUrl";
                }
                else
                {
                    objectName = NetNamingMapper.GetObjectName(resource.RelativeUri);
                }

                if (classesNames.Contains(objectName))
                {
                    objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(resource));
                }
            }

            if (string.IsNullOrWhiteSpace(objectName))
            {
                throw new InvalidOperationException("object name is null for " + resource.RelativeUri);
            }

            if (!classesNames.Contains(objectName))
            {
                return(objectName);
            }

            if (parent == null || string.IsNullOrWhiteSpace(parent.RelativeUri))
            {
                return(GetUniqueObjectName(objectName));
            }

            if (resource.RelativeUri.StartsWith("/{") && parent.RelativeUri.EndsWith("}"))
            {
                objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent)) + objectName;
            }
            else
            {
                objectName = NetNamingMapper.GetObjectName(parent.RelativeUri) + objectName;
                if (classesNames.Contains(objectName))
                {
                    objectName = NetNamingMapper.Capitalize(GetObjectNameForParameter(parent));
                }
            }

            if (string.IsNullOrWhiteSpace(objectName))
            {
                throw new InvalidOperationException("object name is null for " + resource.RelativeUri);
            }

            if (!classesNames.Contains(objectName))
            {
                return(objectName);
            }

            return(GetUniqueObjectName(objectName));
        }
Example #14
0
        public ApiObject Parse(string key, string jsonSchema, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings)
        {
            var obj = new ApiObject
            {
                Name       = NetNamingMapper.GetObjectName(key),
                Properties = new List <Property>()
            };
            JsonSchema schema = null;

            Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null;
            try
            {
                schema = JsonSchema.Parse(jsonSchema);
            }
            catch (Exception exv3)             // NewtonJson does not support Json Schema v4
            {
                try
                {
                    schema   = null;
                    v4Schema = Newtonsoft.JsonV4.Schema.JsonSchema.Parse(jsonSchema);
                }
                catch (Exception exv4)
                {
                    //dynamic dynObj = JsonConvert.DeserializeObject(jsonSchema);
                    //foreach (var kv in dynObj)
                    //{
                    //	//TODO: manual parse schema ? or parse using example ?
                    //}
                    if (!warnings.ContainsKey(key))
                    {
                        warnings.Add(key, "Could not parse JSON Schema. v3 parser message: " + exv3.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty) + ". v4 parser message: " + exv4.Message.Replace("\r\n", string.Empty).Replace("\n", string.Empty));
                    }
                }
            }

            if (schema == null && v4Schema == null)
            {
                return(obj);
            }

            if (schema != null)
            {
                if (schema.Type == JsonSchemaType.Array)
                {
                    obj.IsArray = true;
                    if (schema.Items != null && schema.Items.Any())
                    {
                        ParseProperties(objects, obj.Properties, schema.Items.First().Properties);
                    }
                }
                else
                {
                    ParseProperties(objects, obj.Properties, schema.Properties);
                }
            }
            else
            {
                if (v4Schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array)
                {
                    obj.IsArray = true;
                    if (v4Schema.Items != null && v4Schema.Items.Any())
                    {
                        ParseProperties(objects, obj.Properties, v4Schema.Items.First().Properties);
                    }
                }
                else
                {
                    ParseProperties(objects, obj.Properties, v4Schema.Properties);
                }
            }
            return(obj);
        }
        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);
        }