private string BuildPropertyCommon(PropertyInfo prop, IEnumerable <CustomAttributeData> customAttributes) { var name = TypeBuilderHelper.GetPropertyName(prop); var res = "\"" + name + "\": { \"type\": " + SchemaTypeMapper.GetAttribute(prop.PropertyType); res += HandleRequiredAttribute(prop, customAttributes); res += HandleValidationAttributes(customAttributes); return(res); }
private IDictionary <string, RamlType> GetProperties(Type type) { var props = GetClassProperties(type).ToArray(); var dic = new Dictionary <string, RamlType>(); foreach (var prop in props) { var key = TypeBuilderHelper.GetPropertyName(prop); var ramlType = GetProperty(prop); if (ramlType != null) { ramlType.Required = !IsOptionalProperty(prop, prop.CustomAttributes); dic.Add(key, ramlType); } } return(dic); }
private string GetOneOfProperty(PropertyInfo prop, IEnumerable <Type> subclasses, int pad) { var name = TypeBuilderHelper.GetPropertyName(prop); var oneOf = ("\"" + name + "\": {").Indent(pad) + Environment.NewLine; oneOf += "\"type\": \"object\",".Indent(pad + 2) + Environment.NewLine; oneOf += "\"oneOf\": [".Indent(pad + 2) + Environment.NewLine; foreach (var subclass in subclasses.Distinct()) { var className = TypeBuilderHelper.GetClassName(subclass); oneOf += ("{ \"$ref\": \"#/definitions/" + className + "\" },").Indent(pad + 4) + Environment.NewLine; if (!definitions.ContainsKey(className)) { definitions.Add(className, subclass); } } oneOf = oneOf.Substring(0, oneOf.Length - Environment.NewLine.Length - 1) + Environment.NewLine; oneOf += "]".Indent(pad + 2) + Environment.NewLine; oneOf += "}".Indent(pad) + Environment.NewLine; return(oneOf); }
protected string HandleNestedTypeProperty(int pad, PropertyInfo prop, string schema, IEnumerable <PropertyInfo> props, IEnumerable <CustomAttributeData> customAttributes) { var nestedType = GetRecursively(prop.PropertyType, pad + 2, customAttributes); if (nestedType == null) { return(string.Empty); } if (!string.IsNullOrWhiteSpace(nestedType)) { var name = TypeBuilderHelper.GetPropertyName(prop); schema += ("\"" + name + "\":").Indent(pad) + Environment.NewLine; schema += nestedType; } else { // is it one of ? var subclasses = prop.PropertyType.Assembly.GetTypes() .Where(type => type.IsSubclassOf(prop.PropertyType)) .ToArray(); if (!subclasses.Any()) { return(string.Empty); } schema += GetOneOfProperty(prop, subclasses, pad); } if (prop != props.Last() && !String.IsNullOrWhiteSpace(schema) && schema.Length > "\r\n".Length) { schema = schema.Substring(0, schema.Length - "\r\n".Length) + ",\r\n"; } return(schema); }
private static string GetEnumProperty(PropertyInfo prop, int pad) { return(("\"" + TypeBuilderHelper.GetPropertyName(prop) + "\": { ").Indent(pad) + Environment.NewLine + (" \"enum\": [" + string.Join(", ", Enum.GetNames(prop.PropertyType).Select(v => "\"" + v + "\"")) + "]").Indent(pad) + Environment.NewLine + "}".Indent(pad)); }