protected override string Map(Type type) { if (type == typeof(DateTime)) { return("date"); } return(SchemaTypeMapper.Map(type)); }
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 static Parameter GetParameterFromProperty(ApiParameterDescription apiParam, PropertyInfo property) { var parameter = new Parameter { Default = IsNullable(property.PropertyType) ? "null" : null, Required = !IsNullable(property.PropertyType), Type = SchemaTypeMapper.Map(property.PropertyType) }; return(parameter); }
protected string GetNestedArraySchemaPrimitiveType(int pad, Type elementType) { var arraySchema = "{\r\n".Indent(pad) + " \"type\": \"array\",\r\n".Indent(pad) + " \"items\": \r\n".Indent(pad) + " {\r\n".Indent(pad) + (" \"type\": " + SchemaTypeMapper.GetAttribute(elementType) + "\r\n").Indent(pad) + " }\r\n".Indent(pad) + "}\r\n".Indent(pad); return(arraySchema); }
private static Parameter GetPrimitiveParameter(ApiParameterDescription apiParam) { var parameter = new Parameter { Default = apiParam.ParameterDescriptor.DefaultValue == null ? (apiParam.ParameterDescriptor.IsOptional ? "null" : null) : apiParam.ParameterDescriptor.DefaultValue.ToString(), Required = !apiParam.ParameterDescriptor.IsOptional, Type = SchemaTypeMapper.Map(apiParam.ParameterDescriptor.ParameterType), Description = apiParam.Documentation }; return(parameter); }
protected string GetProperty(int pad, PropertyInfo prop, string schema, IEnumerable <PropertyInfo> props, IEnumerable <CustomAttributeData> customAttributes) { if (prop.PropertyType.IsEnum) { schema = HandleEnumProperty(pad, prop, props, schema); } else if (SchemaTypeMapper.Map(prop.PropertyType) != null) { schema = HandlePrimitiveTypeProperty(pad, prop, props, schema, customAttributes); } else { schema = HandleNestedTypeProperty(pad, prop, schema, props, customAttributes); } return(schema); }
protected string GetRecursively(Type type, int pad, IEnumerable <CustomAttributeData> customAttributes) { if (type.IsGenericType && TypeBuilderHelper.IsGenericWebResult(type)) { type = type.GetGenericArguments()[0]; } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { return(null); } if (TypeBuilderHelper.IsArrayOrEnumerable(type)) { var elementType = type.GetElementType() ?? type.GetGenericArguments()[0]; if (SchemaTypeMapper.Map(elementType) != null) { return(GetNestedArraySchemaPrimitiveType(pad, elementType)); } if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>)) { return(GetNestedArraySchema(pad, elementType)); } if (elementType.GetProperties().Count(p => p.CanRead || p.CanWrite) == 0) { return(string.Empty); } return(GetNestedArraySchema(pad, elementType)); } if (type.GetProperties().Count(p => p.CanRead || p.CanWrite) == 0) { return(string.Empty); } return(GetNestedObjectSchema(type, pad, customAttributes)); }
protected string GetNestedArraySchema(int pad, Type elementType) { var arraySchema = "{\r\n".Indent(pad) + " \"type\": \"array\",\r\n".Indent(pad) + " \"items\": \r\n".Indent(pad); if (Types.Contains(elementType)) { arraySchema += ("{ \"$ref\": \"" + elementType.Name + "\" }").Indent(pad + 4) + Environment.NewLine; } else { arraySchema += " {\r\n".Indent(pad) + " \"type\": \"object\",\r\n".Indent(pad) + " \"properties\": \r\n".Indent(pad) + " {\r\n".Indent(pad); if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>)) { var keyType = SchemaTypeMapper.Map(elementType.GetGenericArguments()[0]) ?? "object"; var valueType = SchemaTypeMapper.Map(elementType.GetGenericArguments()[1]) ?? "object"; arraySchema += ("\"Key\": { \"type\": \"" + keyType + "\"},\r\n").Indent(pad + 4) + ("\"Value\" : { \"type\": \"" + valueType + "\"}\r\n").Indent(pad + 4); } else { Types.Add(elementType); arraySchema += GetProperties(elementType, pad + 4); } arraySchema += " }\r\n".Indent(pad); arraySchema += " }\r\n".Indent(pad); } arraySchema += "}\r\n".Indent(pad); return(arraySchema); }
protected override string MapParam(Type type) { return(SchemaTypeMapper.Map(type)); }
private bool IsPrimitiveType(Type parameterType) { return(SchemaTypeMapper.Map(parameterType) != null); }