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)); } } } }
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.GetNetType(types[1].ToLowerInvariant(), property.Value.Format) : NetTypeMapper.GetNetType(types[0].ToLowerInvariant(), property.Value.Format); type = IsNullableType(type) ? type + "?" : type; } return(type); }
private static string GetType(KeyValuePair <string, JsonSchema> property, bool isEnum, string enumName) { if (isEnum) { return(enumName); } var type = NetTypeMapper.GetNetType(property.Value.Type, property.Value.Format); 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)); }
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.GetNetType(schema.Items.First().Type, schema.Items.First().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 key = ParseObject(property.Key, item, objects, enums, item); if (key != null) { prop.Type = CollectionTypeHelper.GetCollectionType(NetNamingMapper.GetObjectName(key)); } } } }
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.GetNetType(property.Value.Type, property.Value.Format); 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)); }
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, schemaObjects); } 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.GetNetType(schema.Items.First().Type, schema.Items.First().Format); } } } 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.GetNetType(v4Schema.Items.First().Type, v4Schema.Items.First().Format); } } } else { ParseProperties(objects, obj.Properties, v4Schema, enums); } } return(obj); }
public void ShouldConvertToIntWhenFormatIsInt() { Assert.AreEqual("int", NetTypeMapper.GetNetType("number", "int")); }
public void ShouldConvertIntegerToInt() { Assert.AreEqual("int", NetTypeMapper.GetNetType(JsonSchemaType.Integer, null)); }
public void ShouldConvertToLongWhenFormatIsInt64() { Assert.AreEqual("long", NetTypeMapper.GetNetType("number", "int64")); }
public void ShouldConvertToDateTimeWhenRfc3339() { Assert.AreEqual("DateTime", NetTypeMapper.GetNetType("datetime", "rfc3339")); }
public void ShouldConvertToDecimalWhenNumber() { Assert.AreEqual("decimal", NetTypeMapper.GetNetType("number", null)); }
public void ShouldConvertToByteArrayWhenFile() { Assert.AreEqual("byte[]", NetTypeMapper.GetNetType("file", null)); }
public void ShouldConvertToDecimal() { Assert.AreEqual("decimal", NetTypeMapper.GetNetType(JsonSchemaType.Float, null)); }
public void ShouldConvertToBool() { Assert.AreEqual("bool", NetTypeMapper.GetNetType(JsonSchemaType.Boolean, null)); }
public void ShouldConvertToString() { Assert.AreEqual("string", NetTypeMapper.GetNetType(JsonSchemaType.String, null)); }
public void ShouldConvertToDoubleWhenFormatIsDouble() { Assert.AreEqual("double", NetTypeMapper.GetNetType("number", "double")); }
public void ShouldConvertToFloatWhenFormatIsFloat() { Assert.AreEqual("float", NetTypeMapper.GetNetType("number", "float")); }
public void ShouldConvertToByteWhenFormatIsInt8() { Assert.AreEqual("byte", NetTypeMapper.GetNetType("number", "int8")); }
public void ShouldConvertToDateTimeWhenDatetimeOnly() { Assert.AreEqual("DateTime", NetTypeMapper.GetNetType("datetime-only", null)); }
public void ShouldConvertToIntWhenInteger() { Assert.AreEqual("int", NetTypeMapper.GetNetType("integer", null)); }
public void ShouldConvertToDateTimeOffsetWhenRfc2616() { Assert.AreEqual("DateTimeOffset", NetTypeMapper.GetNetType("datetime", "rfc2616")); }
public void ShouldConvertToShortWhenFormatIsInt16() { Assert.AreEqual("short", NetTypeMapper.GetNetType("number", "int16")); }