private void ParseArray(IDictionary <string, ApiObject> objects, JsonSchema schema, Property prop, KeyValuePair <string, JsonSchema> property, IDictionary <string, ApiEnum> enums)
        {
            if (schema.Items == null || !schema.Items.Any())
            {
                return;
            }

            var netType = NetTypeMapper.GetNetType(schema.Items.First().Type, property.Value.Format);

            if (netType != null)
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(netType);
            }
            else
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(property.Key));
                foreach (var item in schema.Items)
                {
                    var modifiedKey = ParseObject(property.Key, item, objects, enums);
                    if (modifiedKey != null)
                    {
                        prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(modifiedKey));
                    }
                }
            }
        }
예제 #2
0
        private static string HandleMultipleTypes(KeyValuePair <string, JsonSchema> property)
        {
            var type  = "object";
            var types = property.Value.Type.ToString().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            if (types.Length == 2)
            {
                type = types[0] == "Null"
                    ? NetTypeMapper.Map(types[1].ToLowerInvariant())
                    : NetTypeMapper.Map(types[0].ToLowerInvariant());
                type = IsNullableType(type) ? type + "?" : type;
            }
            return(type);
        }
예제 #3
0
        private static string GetType(KeyValuePair <string, JsonSchema> property, bool isEnum, string enumName)
        {
            if (isEnum)
            {
                return(enumName);
            }

            var type = NetTypeMapper.Map(property.Value.Type);

            if (!string.IsNullOrWhiteSpace(type))
            {
                if (type == "string" || (property.Value.Required != null && property.Value.Required.Value))
                {
                    return(type);
                }

                return(type + "?");
            }

            if (HasMultipleTypes(property))
            {
                return(HandleMultipleTypes(property));
            }

            if (!string.IsNullOrWhiteSpace(property.Value.Id))
            {
                return(NetNamingMapper.GetObjectName(property.Value.Id));
            }

            // if it is a "body less" array then I assume it's an array of strings
            if (property.Value.Type == JsonSchemaType.Array && (property.Value.Items == null || !property.Value.Items.Any()))
            {
                return(CollectionTypeHelper.GetCollectionType("string"));
            }

            // if it is a "body less" object then use object as the type
            if (property.Value.Type == JsonSchemaType.Object && (property.Value.Properties == null || !property.Value.Properties.Any()))
            {
                return("object");
            }

            if (property.Value == null)
            {
                return(null);
            }

            return(NetNamingMapper.GetObjectName(property.Key));
        }
예제 #4
0
        private void ParseArray(IDictionary <string, ApiObject> objects, Newtonsoft.JsonV4.Schema.JsonSchema schema, Property prop, KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property, IDictionary <string, ApiEnum> enums)
        {
            var netType = NetTypeMapper.Map(schema.Items.First().Type);

            if (netType != null)
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(netType);
            }
            else
            {
                prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(property.Key));
                foreach (var item in schema.Items)
                {
                    ParseObject(property.Key, item.Properties, objects, enums, item);
                }
            }
        }
예제 #5
0
        private static string GetType(KeyValuePair <string, Newtonsoft.JsonV4.Schema.JsonSchema> property, bool isEnum, string enumName, ICollection <string> requiredProps)
        {
            if (property.Value.OneOf != null && property.Value.OneOf.Count > 0)
            {
                return(NetNamingMapper.GetObjectName(property.Key));
            }

            if (isEnum)
            {
                return(enumName);
            }

            var type = NetTypeMapper.Map(property.Value.Type);

            if (!string.IsNullOrWhiteSpace(type))
            {
                if (type == "string" || (requiredProps != null && requiredProps.Contains(property.Key)))
                {
                    return(type);
                }

                return(type + "?");
            }

            if (HasMultipleTypes(property))
            {
                return(HandleMultipleTypes(property));
            }

            if (!string.IsNullOrWhiteSpace(property.Value.Id))
            {
                return(NetNamingMapper.GetObjectName(property.Value.Id));
            }

            return(NetNamingMapper.GetObjectName(property.Key));
        }
예제 #6
0
 public void ShouldConvertToInt()
 {
     Assert.AreEqual("int", NetTypeMapper.Map(JsonSchemaType.Integer));
 }
예제 #7
0
 public void ShouldConvertToDecimal()
 {
     Assert.AreEqual("decimal", NetTypeMapper.Map(JsonSchemaType.Float));
 }
예제 #8
0
 public void ShouldConvertToBool()
 {
     Assert.AreEqual("bool", NetTypeMapper.Map(JsonSchemaType.Boolean));
 }
예제 #9
0
 public void ShouldConvertToString()
 {
     Assert.AreEqual("string", NetTypeMapper.Map(JsonSchemaType.String));
 }
예제 #10
0
 public void ShouldConvertToIntWhenFormatIsInt()
 {
     Assert.AreEqual("int", NetTypeMapper.GetNetType("number", "int"));
 }
예제 #11
0
 public void ShouldConvertToDecimalWhenNumber()
 {
     Assert.AreEqual("decimal", NetTypeMapper.GetNetType("number", null));
 }
예제 #12
0
 public void ShouldConvertToByteWhenFormatIsInt8()
 {
     Assert.AreEqual("byte", NetTypeMapper.GetNetType("number", "int8"));
 }
예제 #13
0
 public void ShouldConvertToDateTimeWhenDatetimeOnly()
 {
     Assert.AreEqual("DateTime", NetTypeMapper.GetNetType("datetime-only", null));
 }
예제 #14
0
 public void ShouldConvertToDateTimeOffsetWhenRfc2616()
 {
     Assert.AreEqual("DateTimeOffset", NetTypeMapper.GetNetType("datetime", "rfc2616"));
 }
예제 #15
0
 public void ShouldConvertToByteArrayWhenFile()
 {
     Assert.AreEqual("byte[]", NetTypeMapper.GetNetType("file", null));
 }
예제 #16
0
 public void ShouldConvertToDoubleWhenFormatIsDouble()
 {
     Assert.AreEqual("double", NetTypeMapper.GetNetType("number", "double"));
 }
예제 #17
0
 public void ShouldConvertToFloatWhenFormatIsFloat()
 {
     Assert.AreEqual("float", NetTypeMapper.GetNetType("number", "float"));
 }
예제 #18
0
 public void ShouldConvertToShortWhenFormatIsInt16()
 {
     Assert.AreEqual("short", NetTypeMapper.GetNetType("number", "int16"));
 }
 public void ShouldTrimBeforeConvertingInt()
 {
     Assert.AreEqual("int", NetTypeMapper.Map(" integer "));
 }
예제 #20
0
        public ApiObject Parse(string key, string jsonSchema, IDictionary <string, ApiObject> objects, IDictionary <string, string> warnings,
                               IDictionary <string, ApiEnum> enums, IDictionary <string, ApiObject> otherObjects, IDictionary <string, ApiObject> schemaObjects)
        {
            this.otherObjects  = otherObjects;
            this.schemaObjects = schemaObjects;
            var obj = new ApiObject
            {
                Name       = NetNamingMapper.GetObjectName(key),
                Properties = new List <Property>(),
                JSONSchema = jsonSchema.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\n", "")
                             .Replace("\\", "\\\\").Replace("\"", "\\\"")
                             // .Replace("\\/", "\\\\/").Replace("\"", "\\\"").Replace("\\\\\"", "\\\\\\\"")
            };
            JsonSchema schema = null;

            Newtonsoft.JsonV4.Schema.JsonSchema v4Schema = null;
            if (jsonSchema.Contains("\"oneOf\""))
            {
                v4Schema = ParseV4Schema(key, jsonSchema, warnings, objects);
            }
            else
            {
                schema = ParseV3OrV4Schema(key, jsonSchema, warnings, ref v4Schema, objects);
            }

            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())
                    {
                        if (schema.Items.First().Properties != null)
                        {
                            ParseProperties(objects, obj.Properties, schema.Items.First().Properties, enums);
                        }
                        else
                        {
                            obj.Type = NetTypeMapper.Map(schema.Items.First().Type);
                        }
                    }
                }
                else
                {
                    ParseProperties(objects, obj.Properties, schema.Properties, enums);
                    AdditionalProperties(obj.Properties, schema);
                }
            }
            else
            {
                if (v4Schema.Type == Newtonsoft.JsonV4.Schema.JsonSchemaType.Array)
                {
                    obj.IsArray = true;
                    if (v4Schema.Items != null && v4Schema.Items.Any())
                    {
                        if (v4Schema.Items.First().Properties != null)
                        {
                            ParseProperties(objects, obj.Properties, v4Schema.Items.First(), enums);
                        }
                        else
                        {
                            obj.Type = NetTypeMapper.Map(v4Schema.Items.First().Type);
                        }
                    }
                }
                else
                {
                    ParseProperties(objects, obj.Properties, v4Schema, enums);
                }
            }
            return(obj);
        }
예제 #21
0
 public void ShouldConvertIntegerToInt()
 {
     Assert.AreEqual("int", NetTypeMapper.GetNetType(JsonSchemaType.Integer, null));
 }
예제 #22
0
 public void ShouldConvertToDateTimeWhenRfc3339()
 {
     Assert.AreEqual("DateTime", NetTypeMapper.GetNetType("datetime", "rfc3339"));
 }
예제 #23
0
 public void ShouldConvertToIntWhenInteger()
 {
     Assert.AreEqual("int", NetTypeMapper.GetNetType("integer", null));
 }
예제 #24
0
 public void ShouldConvertToLongWhenFormatIsInt64()
 {
     Assert.AreEqual("long", NetTypeMapper.GetNetType("number", "int64"));
 }
 public void ShouldTrimBeforeConvertingString()
 {
     Assert.AreEqual("string", NetTypeMapper.Map(" string "));
 }