private void LoadProperty <TSchemaType>(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new() { var propertyType = property.PropertyType; var propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType); var attributes = property.GetCustomAttributes().ToArray(); var jsonProperty = Generate <JsonProperty>(propertyType, schemaResolver); var propertyName = JsonPathUtilities.GetPropertyName(property); parentSchema.Properties.Add(propertyName, jsonProperty); var requiredAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RequiredAttribute"); if (propertyTypeDescription.IsAlwaysRequired || requiredAttribute != null) { parentSchema.RequiredProperties.Add(property.Name); } jsonProperty.Description = GetDescription(property, attributes); dynamic regexAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RegularExpressionAttribute"); if (regexAttribute != null) { jsonProperty.Pattern = regexAttribute.Pattern; } dynamic rangeAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RangeAttribute"); if (rangeAttribute != null) { if (rangeAttribute.Minimum != null) { jsonProperty.Minimum = rangeAttribute.Minimum; } if (rangeAttribute.Maximum != null) { jsonProperty.Maximum = rangeAttribute.Maximum; } } }
private void LoadProperty <TSchemaType>(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new() { var propertyType = property.PropertyType; var propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType); var attributes = property.GetCustomAttributes().ToArray(); if (attributes.All(a => !(a is JsonIgnoreAttribute))) { var jsonProperty = Generate <JsonProperty>(propertyType, property.GetCustomAttributes(), schemaResolver); var propertyName = JsonPathUtilities.GetPropertyName(property); parentSchema.Properties.Add(propertyName, jsonProperty); var jsonPropertyAttribute = property.GetCustomAttribute <JsonPropertyAttribute>(); var isJsonPropertyRequired = jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.Always; var requiredAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RequiredAttribute"); if (propertyTypeDescription.IsAlwaysRequired || requiredAttribute != null || isJsonPropertyRequired) { parentSchema.RequiredProperties.Add(propertyName); } dynamic displayAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.DisplayAttribute"); if (displayAttribute != null && displayAttribute.Name != null) { jsonProperty.Title = displayAttribute.Name; } jsonProperty.Description = GetDescription(property, attributes); dynamic regexAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RegularExpressionAttribute"); if (regexAttribute != null) { jsonProperty.Pattern = regexAttribute.Pattern; } if (propertyTypeDescription.Type == JsonObjectType.Number || propertyTypeDescription.Type == JsonObjectType.Integer) { dynamic rangeAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RangeAttribute"); if (rangeAttribute != null) { if (rangeAttribute.Minimum != null) { jsonProperty.Minimum = rangeAttribute.Minimum; } if (rangeAttribute.Maximum != null) { jsonProperty.Maximum = rangeAttribute.Maximum; } } } dynamic minLengthAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.MinLengthAttribute"); if (minLengthAttribute != null && minLengthAttribute.Length != null) { if (propertyTypeDescription.Type == JsonObjectType.String) { jsonProperty.MinLength = minLengthAttribute.Length; } else if (propertyTypeDescription.Type == JsonObjectType.Array) { jsonProperty.MinItems = minLengthAttribute.Length; } } dynamic maxLengthAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.MaxLengthAttribute"); if (maxLengthAttribute != null && maxLengthAttribute.Length != null) { if (propertyTypeDescription.Type == JsonObjectType.String) { jsonProperty.MaxLength = maxLengthAttribute.Length; } else if (propertyTypeDescription.Type == JsonObjectType.Array) { jsonProperty.MaxItems = maxLengthAttribute.Length; } } } }