<PackageReference Include="NJsonSchema" Version="9.1.0" />

JsonSchemaGenerator

public class JsonSchemaGenerator
using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using NJsonSchema.Annotations; using NJsonSchema.Converters; using NJsonSchema.Generation.TypeMappers; using NJsonSchema.Infrastructure; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace NJsonSchema.Generation { public class JsonSchemaGenerator { private static readonly Dictionary<string, string> DataTypeFormats = new Dictionary<string, string> { { "DateTime", "date-time" }, { "Date", "date" }, { "Time", "time" }, { "EmailAddress", "email" }, { "PhoneNumber", "phone" }, { "Url", "uri" } }; public JsonSchemaGeneratorSettings Settings { get; } public JsonSchemaGenerator(JsonSchemaGeneratorSettings settings) { Settings = settings; } public async Task<JsonSchema4> GenerateAsync(Type type) { JsonSchema4 schema = new JsonSchema4(); JsonSchemaResolver schemaResolver = new JsonSchemaResolver(schema, Settings); await GenerateAsync(type, null, schema, schemaResolver).ConfigureAwait(false); return schema; } public Task<JsonSchema4> GenerateAsync(Type type, JsonSchemaResolver schemaResolver) { return GenerateAsync<JsonSchema4>(type, schemaResolver); } public Task<TSchemaType> GenerateAsync<TSchemaType>(Type type, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { return GenerateAsync<TSchemaType>(type, null, schemaResolver); } public async Task<JsonSchema4> GenerateAsync(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver) { return await GenerateAsync<JsonSchema4>(type, parentAttributes, schemaResolver).ConfigureAwait(false); } public async Task<TSchemaType> GenerateAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { TSchemaType schema = new TSchemaType(); await this.GenerateAsync<TSchemaType>(type, parentAttributes, schema, schemaResolver).ConfigureAwait(false); return schema; } public virtual async Task GenerateAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { if (!(await this.TryHandleSpecialTypesAsync<TSchemaType>(type, schema, schemaResolver, parentAttributes))) { if (schemaResolver.RootObject == schema) schema.Title = Settings.SchemaNameGenerator.Generate(type); this.ApplyExtensionDataAttributes<TSchemaType>(type, schema, parentAttributes); JsonContract contract = ResolveContract(type); JsonObjectTypeDescription typeDescription = JsonObjectTypeDescription.FromType(type, contract, parentAttributes, Settings.DefaultEnumHandling); if (typeDescription.Type.HasFlag(JsonObjectType.Object)) { if (typeDescription.IsDictionary) { typeDescription.ApplyType(schema); await this.GenerateDictionaryAsync<TSchemaType>(type, schema, schemaResolver).ConfigureAwait(false); } else if (schemaResolver.HasSchema(type, false)) { schema.SchemaReference = schemaResolver.GetSchema(type, false); } else if (schema.GetType() == typeof(JsonSchema4)) { typeDescription.ApplyType(schema); schema.Description = await type.GetTypeInfo().GetDescriptionAsync(type.GetTypeInfo().GetCustomAttributes()).ConfigureAwait(false); await this.GenerateObjectAsync<TSchemaType>(type, contract as JsonObjectContract, schema, schemaResolver).ConfigureAwait(false); } else { schema.SchemaReference = await GenerateAsync(type, parentAttributes, schemaResolver).ConfigureAwait(false); } } else if (type.GetTypeInfo().IsEnum) { bool isIntegerEnumeration = typeDescription.Type == JsonObjectType.Integer; if (schemaResolver.HasSchema(type, isIntegerEnumeration)) schema.SchemaReference = schemaResolver.GetSchema(type, isIntegerEnumeration); else if (schema.GetType() == typeof(JsonSchema4)) { LoadEnumerations(type, schema, typeDescription); typeDescription.ApplyType(schema); schema.Description = await type.GetXmlSummaryAsync().ConfigureAwait(false); schemaResolver.AddSchema(type, isIntegerEnumeration, schema); } else { schema.SchemaReference = await GenerateAsync(type, parentAttributes, schemaResolver).ConfigureAwait(false); } } else if (typeDescription.Type.HasFlag(JsonObjectType.Array)) { typeDescription.ApplyType(schema); Type itemType = type.GetEnumerableItemType(); if (itemType == (Type)null) { if (CustomAttributeExtensions.GetCustomAttribute<JsonSchemaAttribute>((MemberInfo)type.GetTypeInfo())?.ArrayItem != (Type)null) schema.Item = await GenerateWithReferenceAsync(schemaResolver, itemType).ConfigureAwait(false); else schema.Item = JsonSchema4.CreateAnySchema(); } else schema.Item = await GenerateWithReferenceAsync(schemaResolver, itemType).ConfigureAwait(false); } else { typeDescription.ApplyType(schema); } } } private async Task<JsonSchema4> GenerateWithReferenceAsync(JsonSchemaResolver schemaResolver, Type itemType) { JsonSchema4 jsonSchema = await GenerateAsync(itemType, schemaResolver).ConfigureAwait(false); if (Settings.GenerateXmlObjects) jsonSchema.GenerateXmlObjectForItemType(itemType); if (RequiresSchemaReference(itemType, null)) return new JsonSchema4 { SchemaReference = jsonSchema }; return jsonSchema; } private void ApplyExtensionDataAttributes<TSchemaType>(Type type, TSchemaType schema, IEnumerable<Attribute> parentAttributes) where TSchemaType : JsonSchema4, new { if (parentAttributes == null) { JsonSchemaExtensionDataAttribute[] source = type.GetTypeInfo().GetCustomAttributes<JsonSchemaExtensionDataAttribute>().ToArray(); if (source.Any()) schema.ExtensionData = source.ToDictionary((JsonSchemaExtensionDataAttribute a) => a.Property, (JsonSchemaExtensionDataAttribute a) => a.Value); } else { JsonSchemaExtensionDataAttribute[] source2 = parentAttributes.OfType<JsonSchemaExtensionDataAttribute>().ToArray(); if (source2.Any()) schema.ExtensionData = source2.ToDictionary((JsonSchemaExtensionDataAttribute a) => a.Property, (JsonSchemaExtensionDataAttribute a) => a.Value); } } private async Task<bool> TryHandleSpecialTypesAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver, IEnumerable<Attribute> parentAttributes) where TSchemaType : JsonSchema4, new { ITypeMapper typeMapper = Enumerable.FirstOrDefault<ITypeMapper>((IEnumerable<ITypeMapper>)Settings.TypeMappers, (Func<ITypeMapper, bool>)((ITypeMapper m) => m.MappedType == type)); if (typeMapper != null) { await typeMapper.GenerateSchemaAsync<TSchemaType>(schema, this, schemaResolver, parentAttributes); return true; } if (type == typeof(JObject) || type == typeof(JToken) || type == typeof(object)) return true; return false; } private async Task GenerateDictionaryAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { Type[] genericTypeArguments = type.GetGenericTypeArguments(); Type valueType = (genericTypeArguments.Length == 2) ? genericTypeArguments[1] : typeof(object); if (valueType == typeof(object)) schema.AdditionalPropertiesSchema = JsonSchema4.CreateAnySchema(); else { JsonSchema4 jsonSchema = await GenerateAsync(valueType, schemaResolver).ConfigureAwait(false); if (RequiresSchemaReference(valueType, null)) schema.AdditionalPropertiesSchema = new JsonSchema4 { SchemaReference = jsonSchema }; else schema.AdditionalPropertiesSchema = jsonSchema; } schema.AllowAdditionalProperties = true; } protected virtual async Task GenerateObjectAsync<TSchemaType>(Type type, JsonObjectContract objectContract, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { schemaResolver.AddSchema(type, false, schema); schema.AllowAdditionalProperties = false; await GeneratePropertiesAndInheritanceAsync(type, objectContract, schema, schemaResolver).ConfigureAwait(false); if (Settings.GenerateKnownTypes) await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false); if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForType(type); } private async Task GeneratePropertiesAndInheritanceAsync(Type type, JsonObjectContract objectContract, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { List<MemberInfo> propertiesAndFields = (from f in type.GetTypeInfo().DeclaredFields where f.IsPublic select f).OfType<MemberInfo>().Concat(type.GetTypeInfo().DeclaredProperties.Where(delegate(PropertyInfo p) { if (!(p.GetMethod?.IsPublic ?? false)) return p.SetMethod?.IsPublic ?? false; return true; })).ToList(); if (objectContract != null) { foreach (Newtonsoft.Json.Serialization.JsonProperty item in objectContract.Properties.Where(delegate(Newtonsoft.Json.Serialization.JsonProperty p) { if (p.DeclaringType == type) return p.ShouldSerialize?.Invoke(null) ?? true; return false; })) { MemberInfo memberInfo = propertiesAndFields.FirstOrDefault((MemberInfo p) => p.Name == item.UnderlyingName); PropertyInfo propertyInfo = memberInfo as PropertyInfo; if (!(propertyInfo == (PropertyInfo)null)) { MethodInfo getMethod = propertyInfo.GetMethod; if ((object)getMethod != null && getMethod.IsAbstract) continue; MethodInfo setMethod = propertyInfo.SetMethod; if ((object)setMethod != null && setMethod.IsAbstract) continue; } await LoadPropertyOrFieldAsync(item, memberInfo, type, objectContract, schema, schemaResolver).ConfigureAwait(false); } } await GenerateInheritanceAsync(type, schema, schemaResolver).ConfigureAwait(false); } private unsafe async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver) { foreach (dynamic item in from a in type.GetTypeInfo().GetCustomAttributes(false) where a.GetType().Name == "KnownTypeAttribute" select a) { if (item.Type != null) { dynamic val = this.AddKnownTypeAsync(item.Type, schemaResolver).GetAwaiter(); if (!((byte)val.IsCompleted != 0)) { ICriticalNotifyCompletion awaiter = val as ICriticalNotifyCompletion; AsyncTaskMethodBuilder asyncTaskMethodBuilder; if (awaiter == null) { INotifyCompletion awaiter2 = (INotifyCompletion)val; asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<GenerateKnownTypesAsync>d__17*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<GenerateKnownTypesAsync>d__17*)); awaiter = null; ; } val.GetResult(); } else { if (!((!string.IsNullOrWhiteSpace(item.MethodName)) ? true : false)) throw new ArgumentException($"""{type.FullName}""", "type"); Type[] array = type.GetRuntimeMethod((string)item.MethodName, new Type[0]).Invoke(null, null) as Type[]; if (array != null) { Type[] array2 = array; foreach (Type type2 in array2) { await AddKnownTypeAsync(type2, schemaResolver); } } } } } private async Task AddKnownTypeAsync(Type type, JsonSchemaResolver schemaResolver) { bool isIntegerEnumeration = JsonObjectTypeDescription.FromType(type, ResolveContract(type), null, Settings.DefaultEnumHandling).Type == JsonObjectType.Integer; if (!schemaResolver.HasSchema(type, isIntegerEnumeration)) await GenerateAsync(type, schemaResolver).ConfigureAwait(false); } private async Task GenerateInheritanceAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { GenerateInheritanceDiscriminator(type, schema); Type baseType = type.GetTypeInfo().BaseType; if (baseType != (Type)null && baseType != typeof(object)) { if (Settings.FlattenInheritanceHierarchy) await GeneratePropertiesAndInheritanceAsync(baseType, (JsonObjectContract)ResolveContract(baseType), schema, schemaResolver).ConfigureAwait(false); else { JsonSchema4 jsonSchema = await GenerateAsync(baseType, schemaResolver).ConfigureAwait(false); if (RequiresSchemaReference(baseType, null)) { if (schemaResolver.RootObject != jsonSchema.ActualSchema) schemaResolver.AppendSchema(jsonSchema.ActualSchema, Settings.SchemaNameGenerator.Generate(baseType)); schema.AllOf.Add(new JsonSchema4 { SchemaReference = jsonSchema.ActualSchema }); } else schema.AllOf.Add(jsonSchema); } } } private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) { if (!Settings.FlattenInheritanceHierarchy) { string text = TryGetInheritanceDiscriminator(type.GetTypeInfo().GetCustomAttributes(false).OfType<Attribute>()); if (!string.IsNullOrEmpty(text)) { if (schema.Properties.ContainsKey(text)) throw new InvalidOperationException("The JSON property '" + text + "' is defined multiple times on type '" + type.FullName + "'."); schema.Discriminator = text; schema.Properties[text] = new JsonProperty { Type = JsonObjectType.String, IsRequired = true }; } } } private string TryGetInheritanceDiscriminator(IEnumerable<Attribute> typeAttributes) { dynamic val = (typeAttributes != null) ? typeAttributes.FirstOrDefault((Attribute a) => a.GetType().Name == "JsonConverterAttribute") : null; if ((val != null) && ((Type)val.ConverterType).Name == "JsonInheritanceConverter") { dynamic val2 = val.ConverterParameters != null; if ((val2 ? false : true) ? val2 : (val2 & (val.ConverterParameters.Length > 0))) return (string)val.ConverterParameters[0]; return JsonInheritanceConverter.DefaultDiscriminatorName; } return null; } protected virtual string[] GetTypeProperties(Type type) { if (type == typeof(Exception)) return new string[4] { "InnerException", "Message", "Source", "StackTrace" }; return null; } private void LoadEnumerations(Type type, JsonSchema4 schema, JsonObjectTypeDescription typeDescription) { schema.Type = typeDescription.Type; schema.Enumeration.Clear(); schema.EnumerationNames.Clear(); string[] names = Enum.GetNames(type); foreach (string text in names) { if (typeDescription.Type == JsonObjectType.Integer) { object item = Convert.ChangeType(Enum.Parse(type, text), Enum.GetUnderlyingType(type)); schema.Enumeration.Add(item); } else { dynamic val = type.GetTypeInfo().GetDeclaredField(text).GetCustomAttributes() .TryGetIfAssignableTo("System.Runtime.Serialization.EnumMemberAttribute", TypeNameStyle.FullName); dynamic val2 = val != null; if ((val2 ? false : true) ? val2 : (val2 & !string.IsNullOrEmpty(val.Value))) schema.Enumeration.Add((string)val.Value); else schema.Enumeration.Add(text); } schema.EnumerationNames.Add(text); } } private async Task LoadPropertyOrFieldAsync(Newtonsoft.Json.Serialization.JsonProperty property, MemberInfo propertyInfo, Type parentType, JsonObjectContract parentContract, JsonSchema4 parentSchema, JsonSchemaResolver schemaResolver) { Type propertyType = property.PropertyType; Attribute[] attributes = property.AttributeProvider.GetAttributes(true).ToArray(); JsonObjectTypeDescription propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType, ResolveContract(propertyType), null, Settings.DefaultEnumHandling); if (!property.Ignored) { if (propertyType.Name == "Nullable`1") propertyType = propertyType.GenericTypeArguments[0]; bool requiresSchemaReference = RequiresSchemaReference(propertyType, attributes); JsonProperty jsonProperty; if (requiresSchemaReference) { JsonSchema4 jsonSchema = await GenerateAsync(propertyType, attributes, schemaResolver).ConfigureAwait(false); jsonProperty = ((Settings.NullHandling != 0) ? new JsonProperty { SchemaReference = jsonSchema.ActualSchema } : new JsonProperty { OneOf = { new JsonSchema4 { SchemaReference = jsonSchema.ActualSchema } } }); } else jsonProperty = await GenerateAsync<JsonProperty>(propertyType, attributes, schemaResolver).ConfigureAwait(false); DefaultContractResolver defaultContractResolver = Settings.ActualContractResolver as DefaultContractResolver; string text = (defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName(property.PropertyName) : property.PropertyName; if (parentSchema.Properties.ContainsKey(text)) throw new InvalidOperationException("The JSON property '" + text + "' is defined multiple times on type '" + parentType.FullName + "'."); if (Settings.GenerateXmlObjects) jsonProperty.GenerateXmlObjectForProperty(parentType, text, attributes); parentSchema.Properties.Add(text, jsonProperty); Attribute attribute = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RequiredAttribute", TypeNameStyle.FullName); bool flag = property.Required == Required.Always || property.Required == Required.AllowNull; dynamic val = GetDataMemberAttribute(parentType, attributes)?.IsRequired == true; bool flag2 = attribute != null; dynamic val2 = flag2 ? ((object)flag2) : (flag2 | val); if ((val2) || ((val2 | flag) ? true : false)) parentSchema.RequiredProperties.Add(text); bool flag3 = propertyTypeDescription.IsNullable && !flag2; dynamic val3 = (!flag3) ? ((object)flag3) : (flag3 & (val == false)); dynamic val4 = (val3 ? false : true) ? val3 : (val3 & (property.Required == Required.Default || property.Required == Required.AllowNull)); if (val4) { if (Settings.NullHandling == NullHandling.JsonSchema) { if (requiresSchemaReference) jsonProperty.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null }); else if (jsonProperty.Type == JsonObjectType.None) { jsonProperty.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.None }); jsonProperty.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null }); } else { jsonProperty.Type |= JsonObjectType.Null; } } } else if (Settings.NullHandling == NullHandling.Swagger && !parentSchema.RequiredProperties.Contains(text)) { parentSchema.RequiredProperties.Add(text); } dynamic val5 = attributes.TryGetIfAssignableTo("System.ComponentModel.ReadOnlyAttribute", TypeNameStyle.FullName); if (val5 != null) jsonProperty.IsReadOnly = ((byte)val5.IsReadOnly != 0); JsonProperty jsonProperty2 = jsonProperty; jsonProperty2.Description = await propertyInfo.GetDescriptionAsync(attributes).ConfigureAwait(false); ApplyPropertyAnnotations(jsonProperty, property, parentType, attributes, propertyTypeDescription); } } private bool RequiresSchemaReference(Type type, IEnumerable<Attribute> parentAttributes) { JsonObjectTypeDescription jsonObjectTypeDescription = JsonObjectTypeDescription.FromType(type, ResolveContract(type), parentAttributes, Settings.DefaultEnumHandling); ITypeMapper typeMapper = Settings.TypeMappers.FirstOrDefault((ITypeMapper m) => m.MappedType == type); if (typeMapper != null) return typeMapper.UseReference; if (!jsonObjectTypeDescription.IsDictionary) { if (!jsonObjectTypeDescription.Type.HasFlag(JsonObjectType.Object)) return jsonObjectTypeDescription.IsEnum; return true; } return false; } private JsonContract ResolveContract(Type type) { return Settings.ActualContractResolver.ResolveContract(type); } private static bool IsPropertyIgnored(Type parentType, Attribute[] propertyAttributes) { if (propertyAttributes.Any((Attribute a) => a is JsonIgnoreAttribute)) return true; bool flag = HasDataContractAttribute(parentType); dynamic val = (!flag) ? ((object)flag) : (flag & (GetDataMemberAttribute(parentType, propertyAttributes) == null)); if ((val ? false : true) ? val : (val & !propertyAttributes.Any((Attribute a) => a is JsonPropertyAttribute))) return true; return false; } private static dynamic GetDataMemberAttribute(Type parentType, Attribute[] propertyAttributes) { if (!HasDataContractAttribute(parentType)) return null; return propertyAttributes.FirstOrDefault((Attribute a) => a.GetType().Name == "DataMemberAttribute"); } private static bool HasDataContractAttribute(Type parentType) { return parentType.GetTypeInfo().GetCustomAttributes().Any((Attribute a) => a.GetType().Name == "DataContractAttribute"); } public void ApplyPropertyAnnotations(JsonSchema4 jsonProperty, Newtonsoft.Json.Serialization.JsonProperty property, Type parentType, IEnumerable<Attribute> attributes, JsonObjectTypeDescription propertyTypeDescription) { dynamic val = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DisplayAttribute", TypeNameStyle.FullName); dynamic val2 = val != null; if ((val2 ? false : true) ? val2 : (val2 & (val.Name != null))) jsonProperty.Title = (string)val.Name; dynamic val3 = attributes.TryGetIfAssignableTo("System.ComponentModel.DefaultValueAttribute", TypeNameStyle.FullName); val2 = (val3 != null); if ((val2 ? false : true) ? val2 : (val2 & (val3.Value != null))) jsonProperty.Default = ConvertDefaultValue(property); dynamic val4 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RegularExpressionAttribute", TypeNameStyle.FullName); if (val4 != null) jsonProperty.Pattern = (string)val4.Pattern; if (propertyTypeDescription.Type == JsonObjectType.Number || propertyTypeDescription.Type == JsonObjectType.Integer) { dynamic val5 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RangeAttribute", TypeNameStyle.FullName); if (val5 != null) { val2 = (val5.Minimum != null); if ((val2 ? false : true) ? val2 : (val2 & (val5.Minimum > -1.7976931348623157E+308))) jsonProperty.Minimum = (decimal)(double)val5.Minimum; val2 = (val5.Maximum != null); if ((val2 ? false : true) ? val2 : (val2 & (val5.Maximum < 1.7976931348623157E+308))) jsonProperty.Maximum = (decimal)(double)val5.Maximum; } MultipleOfAttribute multipleOfAttribute = attributes.OfType<MultipleOfAttribute>().SingleOrDefault(); if (multipleOfAttribute != null) jsonProperty.MultipleOf = multipleOfAttribute.MultipleOf; } dynamic val6 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MinLengthAttribute", TypeNameStyle.FullName); val2 = (val6 != null); if ((val2 ? false : true) ? val2 : (val2 & (val6.Length != null))) { if (propertyTypeDescription.Type == JsonObjectType.String) jsonProperty.MinLength = (int?)val6.Length; else if (propertyTypeDescription.Type == JsonObjectType.Array) { jsonProperty.MinItems = (int)val6.Length; } } dynamic val7 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MaxLengthAttribute", TypeNameStyle.FullName); val2 = (val7 != null); if ((val2 ? false : true) ? val2 : (val2 & (val7.Length != null))) { if (propertyTypeDescription.Type == JsonObjectType.String) jsonProperty.MaxLength = (int?)val7.Length; else if (propertyTypeDescription.Type == JsonObjectType.Array) { jsonProperty.MaxItems = (int)val7.Length; } } dynamic val8 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.StringLengthAttribute", TypeNameStyle.FullName); if ((val8 != null) && propertyTypeDescription.Type == JsonObjectType.String) { jsonProperty.MinLength = (int?)val8.MinimumLength; jsonProperty.MaxLength = (int?)val8.MaximumLength; } dynamic val9 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DataTypeAttribute", TypeNameStyle.FullName); if (val9 != null) { dynamic val10 = val9.DataType.ToString(); if (DataTypeFormats.ContainsKey(val10)) jsonProperty.Format = (string)DataTypeFormats[val10]; } } private object ConvertDefaultValue(Newtonsoft.Json.Serialization.JsonProperty property) { if (property.DefaultValue.GetType().GetTypeInfo().IsEnum) { TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(typeof(StringEnumConverter)); JsonConverter converter = property.Converter; if (typeInfo.IsAssignableFrom((converter != null) ? converter.GetType().GetTypeInfo() : null)) return property.DefaultValue.ToString(); return (int)property.DefaultValue; } return property.DefaultValue; } } }