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

JsonSchemaGenerator

public class JsonSchemaGenerator
Generates a JsonSchema4 object for a given type.
using Microsoft.Runtime.CompilerServices; using Newtonsoft.Json; using Newtonsoft.Json.Linq; 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); ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(GenerateAsync(type, null, schema, schemaResolver), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); 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) { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync<JsonSchema4>(type, parentAttributes, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } !0 result = val.GetResult(); val = default(ConfiguredTaskAwaiter); return (JsonSchema4)result; } public async Task<TSchemaType> GenerateAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { TSchemaType schema = new TSchemaType(); ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(this.GenerateAsync<TSchemaType>(type, parentAttributes, schema, schemaResolver), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); return schema; } public virtual async Task GenerateAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { if (!this.TryHandleSpecialTypes<TSchemaType>(type, schema, schemaResolver)) { if (schemaResolver.RootObject == schema) schema.Title = Settings.SchemaNameGenerator.Generate(type); this.ApplyExtensionDataAttributes<TSchemaType>(type, schema, parentAttributes); JsonObjectTypeDescription typeDescription = JsonObjectTypeDescription.FromType(type, parentAttributes, Settings.DefaultEnumHandling); ConfiguredTaskAwaitable<string> val4; ConfiguredTaskAwaiter val5; ConfiguredTaskAwaitable<JsonSchema4> val7; ConfiguredTaskAwaiter val8; if (typeDescription.Type.HasFlag(JsonObjectType.Object)) { ConfiguredTaskAwaitable val; ConfiguredTaskAwaiter val2; if (typeDescription.IsDictionary) { typeDescription.ApplyType(schema); val = AwaitExtensions.ConfigureAwait(this.GenerateDictionaryAsync<TSchemaType>(type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } else if (schemaResolver.HasSchema(type, false)) { schema.SchemaReference = schemaResolver.GetSchema(type, false); } else if (schema.GetType() == typeof(JsonSchema4)) { typeDescription.ApplyType(schema); val4 = AwaitExtensions.ConfigureAwait<string>(GetDescriptionAsync(ReflectionExtensions.GetTypeInfo(type), ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(type), true)), false); val5 = val4.GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; ConfiguredTaskAwaiter val6 = default(ConfiguredTaskAwaiter); val5 = val6; } !0 result = val5.GetResult(); val5 = default(ConfiguredTaskAwaiter); schema.Description = (string)result; val = AwaitExtensions.ConfigureAwait(this.GenerateObjectAsync<TSchemaType>(type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } else { val7 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(type, parentAttributes, schemaResolver), false); val8 = val7.GetAwaiter(); if (!val8.get_IsCompleted()) { await val8; ConfiguredTaskAwaiter val9 = default(ConfiguredTaskAwaiter); val8 = val9; } !0 result2 = val8.GetResult(); val8 = default(ConfiguredTaskAwaiter); schema.SchemaReference = (JsonSchema4)result2; } } else if (ReflectionExtensions.GetTypeInfo(type).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); val4 = AwaitExtensions.ConfigureAwait<string>(type.GetXmlSummaryAsync(), false); val5 = val4.GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; ConfiguredTaskAwaiter val6 = default(ConfiguredTaskAwaiter); val5 = val6; } !0 result3 = val5.GetResult(); val5 = default(ConfiguredTaskAwaiter); schema.Description = (string)result3; schemaResolver.AddSchema(type, isIntegerEnumeration, schema); } else { val7 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(type, parentAttributes, schemaResolver), false); val8 = val7.GetAwaiter(); if (!val8.get_IsCompleted()) { await val8; ConfiguredTaskAwaiter val9 = default(ConfiguredTaskAwaiter); val8 = val9; } !0 result4 = val8.GetResult(); val8 = default(ConfiguredTaskAwaiter); schema.SchemaReference = (JsonSchema4)result4; } } else if (typeDescription.Type.HasFlag(JsonObjectType.Array)) { typeDescription.ApplyType(schema); Type itemType = type.GetEnumerableItemType(); if (itemType == (Type)null) { if (ReflectionExtensions.GetCustomAttribute<JsonSchemaAttribute>(ReflectionExtensions.GetTypeInfo(type))?.ArrayItem != (Type)null) { val7 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateWithReferenceAsync(schemaResolver, itemType), false); val8 = val7.GetAwaiter(); if (!val8.get_IsCompleted()) { await val8; ConfiguredTaskAwaiter val9 = default(ConfiguredTaskAwaiter); val8 = val9; } !0 result5 = val8.GetResult(); val8 = default(ConfiguredTaskAwaiter); schema.Item = (JsonSchema4)result5; } else schema.Item = JsonSchema4.CreateAnySchema(); } else { val7 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateWithReferenceAsync(schemaResolver, itemType), false); val8 = val7.GetAwaiter(); if (!val8.get_IsCompleted()) { await val8; ConfiguredTaskAwaiter val9 = default(ConfiguredTaskAwaiter); val8 = val9; } !0 result6 = val8.GetResult(); val8 = default(ConfiguredTaskAwaiter); schema.Item = (JsonSchema4)result6; } } else { typeDescription.ApplyType(schema); } } } private async Task<JsonSchema4> GenerateWithReferenceAsync(JsonSchemaResolver schemaResolver, Type itemType) { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(itemType, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } !0 result = val.GetResult(); val = default(ConfiguredTaskAwaiter); JsonSchema4 jsonSchema = (JsonSchema4)result; 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 = ReflectionExtensions.GetTypeInfo(type).GetCustomAttributes<JsonSchemaExtensionDataAttribute>(true).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 bool TryHandleSpecialTypes<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { ITypeMapper typeMapper = Settings.TypeMappers.FirstOrDefault((ITypeMapper m) => m.MappedType == type); if (typeMapper != null) { typeMapper.GenerateSchema(schema, this, schemaResolver); 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 { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(valueType, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } !0 result = val.GetResult(); val = default(ConfiguredTaskAwaiter); JsonSchema4 jsonSchema = (JsonSchema4)result; 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, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { schemaResolver.AddSchema(type, false, schema); schema.AllowAdditionalProperties = false; ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(GeneratePropertiesAndInheritanceAsync(type, schema, schemaResolver), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); if (Settings.GenerateKnownTypes) { val = AwaitExtensions.ConfigureAwait(GenerateKnownTypesAsync(type, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForType(type); } private async Task GeneratePropertiesAndInheritanceAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { string[] properties = GetTypeProperties(type); FieldInfo[] declaredFields = ReflectionExtensions.GetTypeInfo(type).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); IEnumerable<PropertyInfo> source = ReflectionExtensions.GetTypeInfo(type).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).Where(delegate(PropertyInfo p) { if (!(p.GetGetMethod()?.IsPublic ?? false)) return p.GetSetMethod()?.IsPublic ?? false; return true; }); ConfiguredTaskAwaitable val; ConfiguredTaskAwaiter val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); foreach (PropertyInfo item in source.Where(delegate(PropertyInfo p) { if (properties != null) return properties.Contains(p.Name); return true; })) { val = AwaitExtensions.ConfigureAwait(LoadPropertyOrFieldAsync(item, item.PropertyType, type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } foreach (FieldInfo item2 in declaredFields.Where(delegate(FieldInfo p) { if (properties != null) return properties.Contains(p.Name); return true; })) { val = AwaitExtensions.ConfigureAwait(LoadPropertyOrFieldAsync(item2, item2.FieldType, type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } val = AwaitExtensions.ConfigureAwait(GenerateInheritanceAsync(type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } private unsafe async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver) { foreach (dynamic item in from a in ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(type), true) 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 = ReflectionExtensions.GetRuntimeMethod(type, (string)item.MethodName, new Type[0]).Invoke(null, null) as Type[]; if (array != null) { Type[] array2 = array; foreach (Type type2 in array2) { TaskAwaiter val2 = AwaitExtensions.GetAwaiter(AddKnownTypeAsync(type2, schemaResolver)); if (!val2.get_IsCompleted()) { await val2; TaskAwaiter val3 = default(TaskAwaiter); val2 = val3; val3 = default(TaskAwaiter); } val2.GetResult(); val2 = default(TaskAwaiter); } } } } } private async Task AddKnownTypeAsync(Type type, JsonSchemaResolver schemaResolver) { bool isIntegerEnumeration = JsonObjectTypeDescription.FromType(type, null, Settings.DefaultEnumHandling).Type == JsonObjectType.Integer; if (!schemaResolver.HasSchema(type, isIntegerEnumeration)) { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(type, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } val.GetResult(); val = default(ConfiguredTaskAwaiter); } } private async Task GenerateInheritanceAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { GenerateInheritanceDiscriminator(type, schema); Type baseType = ReflectionExtensions.GetTypeInfo(type).BaseType; if (baseType != (Type)null && baseType != typeof(object)) { if (Settings.FlattenInheritanceHierarchy) { ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(GeneratePropertiesAndInheritanceAsync(baseType, schema, schemaResolver), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); val2 = default(ConfiguredTaskAwaiter); } else { ConfiguredTaskAwaiter val4 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(baseType, schemaResolver), false).GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); val4 = val5; } !0 result = val4.GetResult(); val4 = default(ConfiguredTaskAwaiter); schema.AllOf.Add((JsonSchema4)result); } } } private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) { if (!Settings.FlattenInheritanceHierarchy) { string text = TryGetInheritanceDiscriminator(ReflectionExtensions.GetTypeInfo(type).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 = ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(type).GetDeclaredField(text), true).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(MemberInfo property, Type propertyType, Type parentType, JsonSchema4 parentSchema, JsonSchemaResolver schemaResolver) { Attribute[] attributes = property.GetCustomAttributes(true).OfType<Attribute>().ToArray(); JsonObjectTypeDescription propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType, attributes, Settings.DefaultEnumHandling); if (!IsPropertyIgnored(parentType, attributes)) { if (propertyType.Name == "Nullable`1") propertyType = propertyType.GetGenericArguments()[0]; bool requiresSchemaReference = RequiresSchemaReference(propertyType, attributes); JsonProperty jsonProperty; if (requiresSchemaReference) { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(propertyType, attributes, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } !0 result = val.GetResult(); val = default(ConfiguredTaskAwaiter); JsonSchema4 jsonSchema = (JsonSchema4)result; jsonProperty = ((Settings.NullHandling != 0) ? new JsonProperty { SchemaReference = jsonSchema.ActualSchema } : new JsonProperty { OneOf = { new JsonSchema4 { SchemaReference = jsonSchema.ActualSchema } } }); } else { ConfiguredTaskAwaiter val3 = AwaitExtensions.ConfigureAwait<JsonProperty>(GenerateAsync<JsonProperty>(propertyType, attributes, schemaResolver), false).GetAwaiter(); if (!val3.get_IsCompleted()) { await val3; ConfiguredTaskAwaiter val4 = default(ConfiguredTaskAwaiter); val3 = val4; } !0 result2 = val3.GetResult(); val3 = default(ConfiguredTaskAwaiter); jsonProperty = (JsonProperty)result2; } string propertyName = JsonReflectionUtilities.GetPropertyName(property, Settings.DefaultPropertyNameHandling); if (parentSchema.Properties.ContainsKey(propertyName)) throw new InvalidOperationException("The JSON property '" + propertyName + "' is defined multiple times on type '" + parentType.FullName + "'."); if (Settings.GenerateXmlObjects) jsonProperty.GenerateXmlObjectForProperty(parentType, propertyName, attributes); parentSchema.Properties.Add(propertyName, jsonProperty); Attribute attribute = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RequiredAttribute", TypeNameStyle.FullName); JsonPropertyAttribute jsonPropertyAttribute = attributes.OfType<JsonPropertyAttribute>().SingleOrDefault(); bool flag = jsonPropertyAttribute != null && (jsonPropertyAttribute.Required == Required.Always || jsonPropertyAttribute.Required == Required.AllowNull); dynamic val5 = GetDataMemberAttribute(parentType, attributes)?.IsRequired == true; bool flag2 = attribute != null; dynamic val6 = flag2 ? ((object)flag2) : (flag2 | val5); if ((val6) || ((val6 | flag) ? true : false)) parentSchema.RequiredProperties.Add(propertyName); bool flag3 = jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.AllowNull; bool flag4 = !flag2; dynamic val7 = (!flag4) ? ((object)flag4) : (flag4 & !val5); dynamic val8 = (val7 ? false : true) ? val7 : (val7 & (propertyTypeDescription.IsNullable | flag3)); if (val8) { 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(propertyName)) { parentSchema.RequiredProperties.Add(propertyName); } dynamic val9 = attributes.TryGetIfAssignableTo("System.ComponentModel.ReadOnlyAttribute", TypeNameStyle.FullName); if (val9 != null) jsonProperty.IsReadOnly = ((byte)val9.IsReadOnly != 0); JsonProperty jsonProperty2 = jsonProperty; ConfiguredTaskAwaiter val10 = AwaitExtensions.ConfigureAwait<string>(GetDescriptionAsync(property, attributes), false).GetAwaiter(); if (!val10.get_IsCompleted()) { await val10; ConfiguredTaskAwaiter val11 = default(ConfiguredTaskAwaiter); val10 = val11; } !0 result3 = val10.GetResult(); val10 = default(ConfiguredTaskAwaiter); jsonProperty2.Description = (string)result3; ApplyPropertyAnnotations(jsonProperty, parentType, attributes, propertyTypeDescription); } } private bool RequiresSchemaReference(Type type, IEnumerable<Attribute> parentAttributes) { JsonObjectTypeDescription jsonObjectTypeDescription = JsonObjectTypeDescription.FromType(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 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 ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(parentType), true).Any((Attribute a) => a.GetType().Name == "DataContractAttribute"); } public void ApplyPropertyAnnotations(JsonSchema4 jsonProperty, Type parentType, IList<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 = (object)this.ConvertDefaultValue(parentType, attributes, val3); 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(Type parentType, IEnumerable<Attribute> propertyAttributes, dynamic defaultValueAttribute) { if (ReflectionExtensions.GetTypeInfo((Type)defaultValueAttribute.Value.GetType()).IsEnum) { if (JsonObjectTypeDescription.IsStringEnum(parentType, propertyAttributes, Settings.DefaultEnumHandling)) return defaultValueAttribute.Value.ToString(); return (int)defaultValueAttribute.Value; } return defaultValueAttribute.Value; } private async Task<string> GetDescriptionAsync(MemberInfo memberInfo, IEnumerable<Attribute> attributes) { dynamic val = attributes.TryGetIfAssignableTo("System.ComponentModel.DescriptionAttribute", TypeNameStyle.FullName); dynamic val2 = val != null; if ((val2 ? false : true) ? val2 : (val2 & (val.Description != null))) return (string)val.Description; dynamic val3 = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DisplayAttribute", TypeNameStyle.FullName); val2 = (val3 != null); if ((val2 ? false : true) ? val2 : (val2 & (val3.Description != null))) return (string)val3.Description; ConfiguredTaskAwaiter val4 = AwaitExtensions.ConfigureAwait<string>(memberInfo.GetXmlSummaryAsync(), false).GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); val4 = val5; } !0 result = val4.GetResult(); val4 = default(ConfiguredTaskAwaiter); string text = (string)result; if (text != string.Empty) return text; return null; } } }