private void CreateSwaggerDefinition(string name, Type definitionType, Dictionary <string, SwaggerDefinition> definitions) { if (definitions.ContainsKey(name)) { return; } SwaggerDefinition definition = new SwaggerDefinition { Type = "object", Properties = new Dictionary <string, SwaggerPropertyDefinition>() }; var properties = definitionType.GetProperties(); properties.ForEach(p => { SwaggerPropertyDefinition pd = new SwaggerPropertyDefinition { Description = this._resolver.GetMemberInfoComment(p) }; if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType) { pd.Type = GetSwaggerType(p.PropertyType); } else if (p.PropertyType.IsArray && p.PropertyType.HasElementType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GetElementType(), definitions); } else { pd.Ref = "#/definitions/" + p.PropertyType.Name; CreateSwaggerDefinition(p.PropertyType.Name, p.PropertyType, definitions); } definition.Properties.Add(p.Name.ToCamelCase(), pd); }); definitions.Add(name, definition); }
private void CreateSwaggerDefinition(string name, Type definitionType, Dictionary <string, SwaggerDefinition> definitions, SwaggerConfig config) { if (definitions.ContainsKey(name)) { return; } if ("Type".Equals(name)) { return; } SwaggerDefinition definition = new SwaggerDefinition { Type = "object", Properties = new Dictionary <string, SwaggerPropertyDefinition>() }; definitions.Add(name, definition); var properties = definitionType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); properties.ForEach(p => { SwaggerPropertyDefinition pd = new SwaggerPropertyDefinition { Description = this._resolver.GetMemberInfoComment(p) }; if (p.PropertyType == typeof(string) || p.PropertyType.IsValueType) { pd.Type = GetSwaggerType(p.PropertyType); } else if (p.PropertyType.IsArray && p.PropertyType.HasElementType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GetElementType(), definitions, config); } else if ( typeof(System.Collections.ICollection).IsAssignableFrom(p.PropertyType) || typeof(System.Collections.IEnumerable).IsAssignableFrom(p.PropertyType) ) { if (p.PropertyType.IsGenericType) { pd.Type = "array"; pd.Items = GetSwaggerItemSchema(p.PropertyType.GenericTypeArguments[0], definitions, config); } else { pd.Type = "array"; } } else { //Console.WriteLine(p.PropertyType.Name); pd.Ref = "#/definitions/" + p.PropertyType.Name; CreateSwaggerDefinition(p.PropertyType.Name, p.PropertyType, definitions, config); } definition.Properties.Add(p.Name.ToCamelCase(), pd); }); }