private XmlSchema CreateRootElementSchema(RamlType ramlType, ConversionOptions options) { var schema = new XmlSchema(); schema.ElementFormDefault = XmlSchemaForm.Qualified; schema.AttributeFormDefault = XmlSchemaForm.Unqualified; if (!string.IsNullOrEmpty(options.XmlNamespace)) { schema.TargetNamespace = options.XmlNamespace; schema.Namespaces.Add("", options.XmlNamespace); } var fileName = Path.GetFileNameWithoutExtension(_ramlFile.FullPath) + "." + FileExtensions.XmlSchema; var include = new XmlSchemaInclude(); include.SchemaLocation = fileName; schema.Includes.Add(include); var element = new XmlSchemaElement(); element.Name = System.Char.ToLowerInvariant(ramlType.Name[0]) + ramlType.Name.Substring(1); element.SchemaTypeName = new XmlQualifiedName(ramlType.Name); schema.Items.Add(element); return(schema); }
public void ShouldParseOptionalStringArrayProperty() { var ramlTypesOrderedDictionary = new RamlTypesOrderedDictionary(); var ramlType = new RamlType { Type = "object", Object = new ObjectType { Properties = new Dictionary <string, RamlType>() } }; var messages = new RamlType { Type = "string[]", Array = new ArrayType() }; ramlType.Object.Properties.Add("Messages?", messages); ramlTypesOrderedDictionary.Add("Test", ramlType); var objects = new Dictionary <string, ApiObject>(); var typeParser = new RamlTypeParser(ramlTypesOrderedDictionary, objects, "SomeNamespace", new Dictionary <string, ApiEnum>(), new Dictionary <string, string>()); typeParser.Parse(); Assert.AreEqual("Messages", objects.First().Value.Properties.First().Name); Assert.AreEqual(false, objects.First().Value.Properties.First().Required); }
private static ArrayType GetArray(IDictionary <string, object> dynamicRaml, string key) { var array = new ArrayType(); array.MaxItems = DynamicRamlParser.GetIntOrNull(dynamicRaml, "maxItems"); array.MinItems = DynamicRamlParser.GetIntOrNull(dynamicRaml, "minItems"); array.UniqueItems = DynamicRamlParser.GetBoolOrNull(dynamicRaml, "maxProperties"); RamlType items = null; if (dynamicRaml.ContainsKey("items")) { var asDictionary = dynamicRaml["items"] as IDictionary <string, object>; if (asDictionary != null) { items = GetRamlType(new KeyValuePair <string, object>("", asDictionary)); } else { var asString = dynamicRaml["items"] as string; items = new RamlType { Type = asString }; } } array.Items = items; return(array); }
private static void HandleValidationAttribute(RamlType ramlTypeProp, CustomAttributeData attribute) { switch (attribute.AttributeType.Name) { case "MaxLengthAttribute": ramlTypeProp.Scalar.MaxLength = (int?)attribute.ConstructorArguments.First().Value; break; case "MinLengthAttribute": ramlTypeProp.Scalar.MinLength = (int?)attribute.ConstructorArguments.First().Value; break; case "RangeAttribute": if (!TypeBuilderHelper.IsMinValue(attribute.ConstructorArguments.First())) { ramlTypeProp.Scalar.Minimum = ConvertToNullableDecimal(attribute.ConstructorArguments.First().Value); } if (!TypeBuilderHelper.IsMaxValue(attribute.ConstructorArguments.Last())) { ramlTypeProp.Scalar.Maximum = ConvertToNullableDecimal(attribute.ConstructorArguments.Last().Value); } break; case "EmailAddressAttribute": ramlTypeProp.Scalar.Pattern = @"pattern: [^\\s@]+@[^\\s@]+\\.[^\\s@]"; break; case "UrlAttribute": ramlTypeProp.Scalar.Pattern = @"pattern: ^(ftp|http|https):\/\/[^ \""]+$"; break; //case "RegularExpressionAttribute": // ramlTypeProp.Scalar.Pattern = "pattern: " + attribute.ConstructorArguments.First().Value; // break; } }
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 string GetScalarType(RamlType ramlType) { var type = NetTypeMapper.GetNetType(ramlType.Scalar.Type, ramlType.Scalar.Format); if (type != null) { return(type); } if (!ramlTypes.ContainsKey(ramlType.Scalar.Type)) { return("object"); } var subRamlType = ramlTypes[ramlType.Scalar.Type]; if (subRamlType.Scalar == null) { return(NetNamingMapper.GetObjectName(ramlType.Scalar.Type)); } type = GetScalarType(subRamlType); if (type != null) { return(type); } throw new InvalidOperationException("Cannot determine type of scalar " + ramlType.Name); }
private ApiObject ParseMap(RamlType ramlType, string key) { var name = NetNamingMapper.GetObjectName(key ?? ramlType.Name); var type = ramlType.Object.Properties.First().Value.Type; if (ramlType.Object.Properties.First().Value.Object != null && ramlType.Object.Properties.First().Value.Type == "object") { var itemName = name + "Item"; var nestedObject = ParseObject(itemName, ramlType.Object.Properties.First().Value); type = nestedObject.Name; schemaObjects.Add(itemName, nestedObject); } type = RamlTypesHelper.DecodeRaml1Type(type); if (NetTypeMapper.IsPrimitiveType(type)) { type = NetTypeMapper.Map(type); } return(new ApiObject { Type = name, Name = name, BaseClass = "Dictionary<string," + type + ">", Description = ramlType.Description, Example = GetExample(ramlType.Example, ramlType.Examples), Properties = new Property[0], IsMap = true }); }
private void Serialize(StringBuilder sb, string propertyTitle, RamlType ramlType, int indentation) { sb.AppendFormat("{0}:".Indent(indentation), propertyTitle); sb.AppendLine(); SerializeTypeProperty(sb, indentation, ramlType.Type); SerializeCommonProperties(sb, ramlType, indentation); SerializeExamples(sb, ramlType, indentation); if (ramlType.Scalar != null) { SerializeScalar(sb, ramlType, indentation); } if (ramlType.Object != null) { SerializeObject(sb, ramlType, indentation); } if (ramlType.Array != null) { SerializeArray(sb, indentation, ramlType.Array); } if (ramlType.External != null) { SerializeExternal(sb, ramlType, indentation); } }
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 }); }
private void HandleValidationAttributes(RamlType ramlTypeProp, IEnumerable <CustomAttributeData> customAttributes) { foreach (var attribute in customAttributes) { HandleValidationAttribute(ramlTypeProp, attribute); } }
public void ShouldParseRquiredAttribute() { var ramlTypesOrderedDictionary = new RamlTypesOrderedDictionary(); var ramlType = new RamlType { Type = "object", Object = new ObjectType { Properties = new Dictionary <string, RamlType>() } }; var property = new Parser.Expressions.Property { Type = "string", Required = true, DisplayName = "subject" }; var subject = new RamlType { Type = "string", Scalar = property }; ramlType.Object.Properties.Add("subject", subject); ramlTypesOrderedDictionary.Add("mail", ramlType); var objects = new Dictionary <string, ApiObject>(); var typeParser = new RamlTypeParser(ramlTypesOrderedDictionary, objects, "SomeNamespace", new Dictionary <string, ApiEnum>(), new Dictionary <string, string>()); typeParser.Parse(); Assert.AreEqual(true, objects.First().Value.Properties.First().Required); }
private void SerializeObject(StringBuilder sb, RamlType ramlType, int indentation) { RamlSerializerHelper.SerializeProperty(sb, "maxProperties", ramlType.Object.MaxProperties, indentation + 4); RamlSerializerHelper.SerializeProperty(sb, "minProperties", ramlType.Object.MinProperties, indentation + 4); RamlSerializerHelper.SerializeProperty(sb, "discriminator", ramlType.Object.Discriminator as string, indentation + 4); RamlSerializerHelper.SerializeProperty(sb, "discriminatorValue", ramlType.Object.DiscriminatorValue, indentation + 4); SerializeObjectProperties(sb, ramlType.Object.Properties, indentation + 4); }
private void SerializeScalar(StringBuilder sb, RamlType ramlType, int indentation) { RamlSerializerHelper.SerializeCommonParameterProperties(sb, ramlType.Scalar, indentation); RamlSerializerHelper.SerializeProperty(sb, "multipleOf", ramlType.Scalar.MultipleOf, indentation); RamlSerializerHelper.SerializeListProperty(sb, "fileTypes", ramlType.Scalar.FileTypes, indentation); SerializeFormat(sb, indentation, ramlType.Scalar.Format); SerializeAnnotations(sb, ramlType.Scalar.Annotations, indentation); }
private static void SerializeCommonProperties(StringBuilder sb, RamlType ramlType, int indentation) { RamlSerializerHelper.SerializeProperty(sb, "description", ramlType.Description, indentation + 4); RamlSerializerHelper.SerializeProperty(sb, "displayName", ramlType.DisplayName, indentation + 4); RamlSerializerHelper.SerializeProperty(sb, "example", ramlType.Example, indentation + 4); SerializeFacets(sb, ramlType.Facets, indentation + 4); }
private static void SerializeExternal(StringBuilder sb, RamlType ramlType, int indentation) { if (!string.IsNullOrWhiteSpace(ramlType.External.Schema)) { RamlSerializerHelper.SerializeSchema(sb, "schema", ramlType.External.Schema, indentation + 4); } if (!string.IsNullOrWhiteSpace(ramlType.External.Xml)) { RamlSerializerHelper.SerializeSchema(sb, "schema", ramlType.External.Xml, indentation + 4); } }
private RamlType GetEnum(Type type) { var ramlType = new RamlType { Type = "string", Scalar = new Property { Enum = type.GetTypeInfo().GetEnumNames() } // TODO: check!! }; return(ramlType); }
private void AddType(Type type, RamlType raml1Type) { var typeName = GetTypeName(type); // handle case of different types with same class name if (raml1Types.ContainsKey(typeName)) { typeName = GetUniqueName(typeName); } raml1Types.Add(typeName, raml1Type); }
private RamlType GetScalar(Type type) { var ramlType = new RamlType { Scalar = new Property { Type = Raml1TypeMapper.Map(type), } }; return(ramlType); }
private ApiObject ParseObject(string key, RamlType ramlType) { if (ramlType.Object.Properties.Count == 1 && ramlType.Object.Properties.First().Key.StartsWith("[") && ramlType.Object.Properties.First().Key.EndsWith("]")) { return(ParseMap(ramlType, key)); } else { return(GetApiObjectFromObject(ramlType, key)); } }
private RamlType GetMap(Type type) { Type subtype; if (IsDictionary(type)) { subtype = type.GetGenericArguments()[1]; } else { subtype = type.GetTypeInfo().BaseType.GetGenericArguments()[1]; } var subtypeName = GetTypeName(subtype); if (Raml1TypeMapper.Map(subtype) != null) { subtypeName = Raml1TypeMapper.Map(subtype); } else { subtypeName = Add(subtype); } if (string.IsNullOrWhiteSpace(subtypeName)) { return(null); } var raml1Type = new RamlType { Object = new ObjectType { Properties = new Dictionary <string, RamlType>() { { "[]", new RamlType { Type = subtypeName, Required = true } } } } }; return(raml1Type); }
public MimeType GetMimeType(object mimeType) { var value = mimeType as IDictionary <string, object>; if (value == null) { var schema = mimeType as string; return(!string.IsNullOrWhiteSpace(schema) ? new MimeType { Schema = schema } : null); } if (value.ContainsKey("body") && value["body"] is IDictionary <string, object> ) { value = (IDictionary <string, object>)value["body"]; } RamlType ramlType = null; if (value.ContainsKey("type") && value.ContainsKey("properties")) { ramlType = TypeBuilder.GetRamlType(new KeyValuePair <string, object>("obj", mimeType)); } if (value.ContainsKey("application/json")) { value = value["application/json"] as IDictionary <string, object>; } if (value.ContainsKey("type") && (value["type"] as IDictionary <string, object>) != null && ((IDictionary <string, object>)value["type"]).ContainsKey("properties")) { ramlType = TypeBuilder.GetRamlType(new KeyValuePair <string, object>("obj", value["type"])); } return(new MimeType { Type = TypeExtractor.GetType(value), InlineType = ramlType, Description = value.ContainsKey("description") ? (string)value["description"] : null, Example = DynamicRamlParser.GetExample(value), Schema = GetSchema(value), FormParameters = value.ContainsKey("formParameters") ? GetParameters((IDictionary <string, object>)value["formParameters"]) : null, Annotations = AnnotationsBuilder.GetAnnotations(dynamicRaml) }); }
private ApiObject ParseExternal(string key, RamlType ramlType) { if (!string.IsNullOrWhiteSpace(ramlType.External.Schema)) { return (new JsonSchemaParser().Parse(key, ramlType.External.Schema, schemaObjects, warnings, enums, new Dictionary <string, ApiObject>(), new Dictionary <string, ApiObject>())); } if (!string.IsNullOrWhiteSpace(ramlType.External.Xml)) { return(new XmlSchemaParser().Parse(key, ramlType.External.Xml, schemaObjects, targetNamespace)); } throw new InvalidOperationException("Cannot parse external type of " + key); }
private static void SerializeExamples(StringBuilder sb, RamlType ramlType, int indentation) { if (ramlType.Examples == null || !ramlType.Examples.Any()) { return; } sb.Append("examples:".Indent(indentation + 4)); sb.AppendLine(); foreach (var example in ramlType.Examples) { sb.Append("- content: ".Indent(indentation + 8)); sb.AppendLine(); sb.Append(example); sb.AppendLine(); } }
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 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"); }
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) }); }
protected override void ProcessRootType(RamlType ramlType, ConversionOptions options) { var settings = new XmlWriterSettings(); settings.Indent = true; var fileName = System.Char.ToLowerInvariant(ramlType.Name[0]) + ramlType.Name.Substring(1) + "." + FileExtensions.XmlSchema; string fullPath = Path.Combine(options.OutputDirectory, fileName); XmlSchema schema = CreateRootElementSchema(ramlType, options); using (var fileStream = new FileStream(fullPath, FileMode.Create)) { var xmlWriter = XmlWriter.Create(fileStream, settings); schema.Write(xmlWriter); } }
private RamlType GetObject(Type type) { var raml1Type = new RamlType(); raml1Type.Object = new ObjectType(); raml1Type.Type = "object"; if (type.GetTypeInfo().BaseType != null && type.GetTypeInfo().BaseType != typeof(object)) { var parent = GetObject(type.GetTypeInfo().BaseType); AddType(type.GetTypeInfo().BaseType, parent); raml1Type.Type = GetTypeName(type.GetTypeInfo().BaseType); } if (GetClassProperties(type).Count(p => p.CanWrite) > 0) { raml1Type.Object.Properties = GetProperties(type); } return(raml1Type); }
protected override void ProcessRootType(RamlType ramlType, ConversionOptions options) { JSchema jsonSchema = new JSchema(); var refFileName = Path.GetFileNameWithoutExtension(_ramlFile.FullPath) + "." + FileExtensions.JsonSchema; var refDatatype = refFileName + @"#/definitions/" + ramlType.Name; jsonSchema.ExtensionData.Add("$ref", refDatatype); var fileName = System.Char.ToLowerInvariant(ramlType.Name[0]) + ramlType.Name.Substring(1) + "." + FileExtensions.JsonSchema; string fullPath = Path.Combine(options.OutputDirectory, fileName); using (var fileStream = new FileStream(fullPath, FileMode.Create)) using (var streamWriter = new StreamWriter(fileStream)) using (var jsonWriter = new JsonTextWriter(streamWriter)) { jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; jsonSchema.WriteTo(jsonWriter); } }