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

JsonSchemaGenerator

public class JsonSchemaGenerator
Generates a JsonSchema4 object for a given type.
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 NJsonSchema.References; using System; using System.Collections; 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 { this.ApplyExtensionDataAttributes<TSchemaType>(type, schema, parentAttributes); if (await this.TryHandleSpecialTypesAsync<TSchemaType>(type, schema, schemaResolver, parentAttributes).ConfigureAwait(false)) await ApplySchemaProcessorsAsync(type, schema, schemaResolver).ConfigureAwait(false); else { if (schemaResolver.RootObject == schema) schema.Title = Settings.SchemaNameGenerator.Generate(type); JsonTypeDescription typeDescription = Settings.ReflectionService.GetDescription(type, parentAttributes, Settings); if (typeDescription.Type.HasFlag(JsonObjectType.Object)) { if (typeDescription.IsDictionary) { typeDescription.ApplyType(schema); await this.GenerateDictionaryAsync<TSchemaType>(schema, type, schemaResolver).ConfigureAwait(false); } else if (schemaResolver.HasSchema(type, false)) { ((JsonReferenceBase<JsonSchema4>)schema).Reference = schemaResolver.GetSchema(type, false); } else if ((object)schema.GetType() == typeof(JsonSchema4)) { typeDescription.ApplyType(schema); schema.Description = await type.GetTypeInfo().GetDescriptionAsync(type.GetTypeInfo().GetCustomAttributes()).ConfigureAwait(false); await this.GenerateObjectAsync<TSchemaType>(type, schema, schemaResolver).ConfigureAwait(false); } else { ((JsonReferenceBase<JsonSchema4>)schema).Reference = await GenerateAsync(type, parentAttributes, schemaResolver).ConfigureAwait(false); } } else if (typeDescription.IsEnum) { await this.GenerateEnum<TSchemaType>(schema, type, parentAttributes, typeDescription, schemaResolver).ConfigureAwait(false); } else if (typeDescription.Type.HasFlag(JsonObjectType.Array)) { await this.GenerateArray<TSchemaType>(schema, type, typeDescription, schemaResolver).ConfigureAwait(false); } else { typeDescription.ApplyType(schema); } await ApplySchemaProcessorsAsync(type, schema, schemaResolver).ConfigureAwait(false); } } public async Task<TSchemaType> GenerateWithReference<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema4, Task> transformation = null) where TSchemaType : JsonSchema4, new { return await this.GenerateWithReferenceAndNullability<TSchemaType>(type, parentAttributes, false, schemaResolver, transformation).ConfigureAwait(false); } public async Task<TSchemaType> GenerateWithReferenceAndNullability<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema4, Task> transformation = null) where TSchemaType : JsonSchema4, new { JsonTypeDescription description = Settings.ReflectionService.GetDescription(type, parentAttributes, Settings); return await this.GenerateWithReferenceAndNullability<TSchemaType>(type, parentAttributes, description.IsNullable, schemaResolver, transformation).ConfigureAwait(false); } public virtual async Task<TSchemaType> GenerateWithReferenceAndNullability<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, bool isNullable, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema4, Task> transformation = null) where TSchemaType : JsonSchema4, new { JsonSchema4 referencedSchema; if (!Settings.ReflectionService.GetDescription(type, parentAttributes, Settings).RequiresSchemaReference(Settings.TypeMappers)) { TSchemaType schema = await this.GenerateAsync<TSchemaType>(type, parentAttributes, schemaResolver).ConfigureAwait(false); if (!schema.HasReference) { if (transformation != null) await transformation(schema, schema).ConfigureAwait(false); if (isNullable && Settings.SchemaType != SchemaType.Swagger2) { if (schema.Type == JsonObjectType.None) { schema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.None }); schema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null }); } else schema.Type |= JsonObjectType.Null; } return schema; } referencedSchema = schema.ActualSchema; } else referencedSchema = await this.GenerateAsync<JsonSchema4>(type, parentAttributes, schemaResolver).ConfigureAwait(false); TSchemaType referencingSchema = new TSchemaType(); if (transformation != null) await transformation(referencingSchema, referencedSchema).ConfigureAwait(false); if (isNullable && Settings.SchemaType != SchemaType.Swagger2) referencingSchema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null }); if ((Settings.AllowReferencesWithProperties || !Enumerable.Any<JProperty>(JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(referencingSchema)).Properties())) && referencingSchema.OneOf.Count == 0) ((JsonReferenceBase<JsonSchema4>)referencingSchema).Reference = referencedSchema.ActualSchema; else if (Settings.SchemaType != SchemaType.Swagger2) { referencingSchema.OneOf.Add(new JsonSchema4 { Reference = referencedSchema.ActualSchema }); } else { referencingSchema.AllOf.Add(new JsonSchema4 { Reference = referencedSchema.ActualSchema }); } return referencingSchema; } public virtual string GetPropertyName(Newtonsoft.Json.Serialization.JsonProperty property, MemberInfo memberInfo) { try { string text = ((object)memberInfo != null) ? ReflectionCache.GetPropertiesAndFields(memberInfo.DeclaringType).First((ReflectionCache.PropertyOrField p) => p.MemberInfo.Name == memberInfo.Name).GetName() : property.PropertyName; DefaultContractResolver defaultContractResolver = Settings.ActualContractResolver as DefaultContractResolver; return (defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName(text) : text; } catch (Exception innerException) { throw new InvalidOperationException("Could not get JSON property name of property '" + (((object)memberInfo != null) ? memberInfo.Name : "n/a") + "' and type '" + (((object)memberInfo?.DeclaringType != null) ? memberInfo.DeclaringType.FullName : "n/a") + "'.", innerException); } } protected virtual async Task GenerateObjectAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { schemaResolver.AddSchema(type, false, schema); schema.AllowAdditionalProperties = false; schema.IsAbstract = type.GetTypeInfo().get_IsAbstract(); await GeneratePropertiesAndInheritanceAsync(type, schema, schemaResolver).ConfigureAwait(false); if (Settings.GenerateKnownTypes) await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false); if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForType(type); } private async Task ApplySchemaProcessorsAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { SchemaProcessorContext context = new SchemaProcessorContext(type, schema, schemaResolver, this); foreach (ISchemaProcessor schemaProcessor in Settings.SchemaProcessors) { await schemaProcessor.ProcessAsync(context).ConfigureAwait(false); } } 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) => (object)m.MappedType == type)); if (typeMapper == null && type.GetTypeInfo().get_IsGenericType()) { Type genericType = type.GetGenericTypeDefinition(); typeMapper = Enumerable.FirstOrDefault<ITypeMapper>((IEnumerable<ITypeMapper>)Settings.TypeMappers, (Func<ITypeMapper, bool>)((ITypeMapper m) => (object)m.MappedType == genericType)); } if (typeMapper != null) { TypeMapperContext context = new TypeMapperContext(type, this, schemaResolver, parentAttributes); await typeMapper.GenerateSchemaAsync(schema, context).ConfigureAwait(false); return true; } if ((object)type == typeof(JObject) || (object)type == typeof(JToken) || (object)type == typeof(object)) return true; return false; } private async Task GenerateArray<TSchemaType>(TSchemaType schema, Type type, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { typeDescription.ApplyType(schema); Type itemType = type.GetEnumerableItemType(); if ((object)itemType != null) { <>c__DisplayClass19_0<TSchemaType> <>4__this; schema.Item = await this.GenerateWithReferenceAndNullability<JsonSchema4>(itemType, (IEnumerable<Attribute>)null, false, schemaResolver, (Func<JsonSchema4, JsonSchema4, Task>)delegate(JsonSchema4 s, JsonSchema4 r) { <>c__DisplayClass19_0<TSchemaType>.<<GenerateArray>b__0>d stateMachine = default(<>c__DisplayClass19_0<TSchemaType>.<<GenerateArray>b__0>d); stateMachine.<>4__this = <>4__this; stateMachine.s = s; stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create(); stateMachine.<>1__state = -1; AsyncTaskMethodBuilder <>t__builder = stateMachine.<>t__builder; <>t__builder.Start<<>c__DisplayClass19_0<TSchemaType>.<<GenerateArray>b__0>d>(ref stateMachine); return stateMachine.<>t__builder.Task; }).ConfigureAwait(false); } else schema.Item = JsonSchema4.CreateAnySchema(); } private async Task GenerateEnum<TSchemaType>(TSchemaType schema, Type type, IEnumerable<Attribute> parentAttributes, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { if (type.get_Name() == "Nullable`1") type = type.GenericTypeArguments[0]; bool isIntegerEnumeration = typeDescription.Type == JsonObjectType.Integer; if (schemaResolver.HasSchema(type, isIntegerEnumeration)) ((JsonReferenceBase<JsonSchema4>)schema).Reference = schemaResolver.GetSchema(type, isIntegerEnumeration); else if ((object)schema.GetType() == typeof(JsonSchema4)) { LoadEnumerations(type, schema, typeDescription); typeDescription.ApplyType(schema); schema.Description = await type.GetXmlSummaryAsync().ConfigureAwait(false); schemaResolver.AddSchema(type, isIntegerEnumeration, schema); } else { ((JsonReferenceBase<JsonSchema4>)schema).Reference = await GenerateAsync(type, parentAttributes, schemaResolver).ConfigureAwait(false); } } private async Task GenerateDictionaryAsync<TSchemaType>(TSchemaType schema, Type type, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { Type[] genericTypeArguments = type.GetGenericTypeArguments(); Type valueType = (genericTypeArguments.Length == 2) ? genericTypeArguments[1] : typeof(object); if ((object)valueType == typeof(object)) schema.AdditionalPropertiesSchema = JsonSchema4.CreateAnySchema(); else { JsonSchema4 jsonSchema = await GenerateAsync(valueType, schemaResolver).ConfigureAwait(false); if (Settings.ReflectionService.GetDescription(valueType, null, Settings).RequiresSchemaReference(Settings.TypeMappers)) schema.AdditionalPropertiesSchema = new JsonSchema4 { Reference = jsonSchema }; else schema.AdditionalPropertiesSchema = jsonSchema; } schema.AllowAdditionalProperties = true; } private async Task GeneratePropertiesAndInheritanceAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { List<MemberInfo> propertiesAndFields = type.GetTypeInfo().DeclaredFields.Where(delegate(FieldInfo f) { if (f.IsPublic) return !f.IsStatic; return false; }).OfType<MemberInfo>().Concat(type.GetTypeInfo().DeclaredProperties.Where(delegate(PropertyInfo p) { if (p.GetMethod?.IsPublic ?? false) { MethodInfo getMethod2 = p.GetMethod; if ((object)getMethod2 != null && !getMethod2.IsStatic) return true; } if (p.SetMethod?.IsPublic ?? false) { MethodInfo setMethod2 = p.SetMethod; if ((object)setMethod2 == null) return false; return !setMethod2.IsStatic; } return false; })) .ToList(); JsonContract jsonContract = Settings.ResolveContract(type); string[] allowedProperties = GetTypeProperties(type); JsonObjectContract jsonObjectContract = jsonContract as JsonObjectContract; if (jsonObjectContract != null && allowedProperties == null) { foreach (Newtonsoft.Json.Serialization.JsonProperty item in from p in jsonObjectContract.Properties where (object)p.DeclaringType == type select p) { bool flag; try { flag = (item.ShouldSerialize?.Invoke(null) ?? true); } catch { flag = true; } if (flag) { MemberInfo memberInfo = propertiesAndFields.FirstOrDefault((MemberInfo p) => p.Name == item.UnderlyingName); PropertyInfo propertyInfo = memberInfo as PropertyInfo; if (!Settings.GenerateAbstractProperties && (object)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, schema, schemaResolver).ConfigureAwait(false); } } } else { foreach (MemberInfo item2 in propertiesAndFields.Where(delegate(MemberInfo m) { if (allowedProperties != null) return allowedProperties.Contains(m.Name); return true; })) { JsonPropertyAttribute jsonPropertyAttribute = CustomAttributeExtensions.GetCustomAttributes(item2, true).OfType<JsonPropertyAttribute>().SingleOrDefault(); Type propertyType = (item2 as PropertyInfo)?.PropertyType ?? ((FieldInfo)item2).FieldType; Newtonsoft.Json.Serialization.JsonProperty obj2 = new Newtonsoft.Json.Serialization.JsonProperty { AttributeProvider = new ReflectionAttributeProvider(item2), PropertyType = propertyType, Ignored = IsPropertyIgnored(propertyType, type, CustomAttributeExtensions.GetCustomAttributes(item2, true).OfType<Attribute>().ToArray()) }; if (jsonPropertyAttribute != null) { obj2.PropertyName = (jsonPropertyAttribute.PropertyName ?? item2.Name); obj2.Required = jsonPropertyAttribute.Required; obj2.DefaultValueHandling = jsonPropertyAttribute.DefaultValueHandling; obj2.TypeNameHandling = jsonPropertyAttribute.TypeNameHandling; obj2.NullValueHandling = jsonPropertyAttribute.NullValueHandling; obj2.TypeNameHandling = jsonPropertyAttribute.TypeNameHandling; } else obj2.PropertyName = item2.Name; await LoadPropertyOrFieldAsync(obj2, item2, type, schema, schemaResolver).ConfigureAwait(false); } } await GenerateInheritanceAsync(type, schema, schemaResolver).ConfigureAwait(false); } protected virtual string[] GetTypeProperties(Type type) { if ((object)type == typeof(Exception)) return new string[4] { "InnerException", "Message", "Source", "StackTrace" }; return null; } private unsafe async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver) { IEnumerable<Attribute> enumerable = (from a in CustomAttributeExtensions.GetCustomAttributes(type.GetTypeInfo(), Settings.FlattenInheritanceHierarchy) where a.GetType().get_Name() == "KnownTypeAttribute" select a).OfType<Attribute>(); foreach (dynamic item in enumerable) { if (item.Type != null) { dynamic val = this.AddKnownTypeAsync(item.Type, schemaResolver).ConfigureAwait(false).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__24*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<GenerateKnownTypesAsync>d__24*)); awaiter = null; ; } val.GetResult(); } else { if (!((item.MethodName != null) ? true : false)) throw new ArgumentException($"""{new object[1] { type.FullName }}""", "type"); MethodInfo runtimeMethod = type.GetRuntimeMethod((string)item.MethodName, new Type[0]); if ((object)runtimeMethod != null) { Type[] array = runtimeMethod.Invoke(null, null) as Type[]; if (array != null) { Type[] array2 = array; foreach (Type type2 in array2) { await AddKnownTypeAsync(type2, schemaResolver).ConfigureAwait(false); } } } } } } private async Task AddKnownTypeAsync(Type type, JsonSchemaResolver schemaResolver) { bool isIntegerEnumeration = Settings.ReflectionService.GetDescription(type, null, Settings).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().get_BaseType(); if ((object)baseType != null && (object)baseType != typeof(object)) { if (Settings.FlattenInheritanceHierarchy) { if (!Settings.ReflectionService.GetDescription(baseType, null, Settings).IsDictionary && !type.IsArray) await GeneratePropertiesAndInheritanceAsync(baseType, schema, schemaResolver).ConfigureAwait(false); } else { JsonSchema4 jsonSchema = await GenerateAsync(baseType, schemaResolver).ConfigureAwait(false); if (Settings.ReflectionService.GetDescription(baseType, null, Settings).RequiresSchemaReference(Settings.TypeMappers)) { if (schemaResolver.RootObject != jsonSchema.ActualSchema) schemaResolver.AppendSchema(jsonSchema.ActualSchema, Settings.SchemaNameGenerator.Generate(baseType)); schema.AllOf.Add(new JsonSchema4 { Reference = jsonSchema.ActualSchema }); } else schema.AllOf.Add(jsonSchema); } } if (Settings.FlattenInheritanceHierarchy && Settings.GenerateAbstractProperties) { foreach (Type implementedInterface in type.GetTypeInfo().ImplementedInterfaces) { if (!Settings.ReflectionService.GetDescription(implementedInterface, null, Settings).IsDictionary && !type.IsArray && !typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(implementedInterface.GetTypeInfo())) await GeneratePropertiesAndInheritanceAsync(implementedInterface, schema, schemaResolver).ConfigureAwait(false); } } } private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) { if (!Settings.FlattenInheritanceHierarchy) { string text = TryGetInheritanceDiscriminator(type); 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(Type type) { dynamic val = CustomAttributeExtensions.GetCustomAttributes(type.GetTypeInfo(), false).OfType<Attribute>().TryGetIfAssignableTo("JsonConverterAttribute", TypeNameStyle.Name); if ((val != null) && ((Type)val.ConverterType).get_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; } private void LoadEnumerations(Type type, JsonSchema4 schema, JsonTypeDescription typeDescription) { schema.Type = typeDescription.Type; schema.Enumeration.Clear(); schema.EnumerationNames.Clear(); Type underlyingType = Enum.GetUnderlyingType(type); string[] names = Enum.GetNames(type); foreach (string text in names) { if (typeDescription.Type == JsonObjectType.Integer) { object item = Convert.ChangeType(Enum.Parse(type, text), underlyingType); 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, JsonSchema4 parentSchema, JsonSchemaResolver schemaResolver) { Type type = property.PropertyType; Attribute[] propertyAttributes = property.AttributeProvider.GetAttributes(true).ToArray(); JsonTypeDescription propertyTypeDescription = Settings.ReflectionService.GetDescription(type, propertyAttributes, Settings); if (!property.Ignored && !IsPropertyIgnoredBySettings(type, parentType, propertyAttributes)) { if (type.get_Name() == "Nullable`1") type = type.GenericTypeArguments[0]; string propertyName = GetPropertyName(property, propertyInfo); if (parentSchema.Properties.ContainsKey(propertyName)) throw new InvalidOperationException("The JSON property '" + propertyName + "' is defined multiple times on type '" + parentType.FullName + "'."); Attribute attribute = propertyAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RequiredAttribute", TypeNameStyle.FullName); bool flag = property.Required == Required.Always || property.Required == Required.AllowNull; dynamic val = GetDataMemberAttribute(parentType, propertyAttributes)?.IsRequired == true; bool flag2 = attribute != null; dynamic val2 = flag2 ? ((object)flag2) : (flag2 | val); if ((val2) || ((val2 | flag) ? true : false)) parentSchema.RequiredProperties.Add(propertyName); bool isNullable = propertyTypeDescription.IsNullable && !flag2 && !((byte)val != 0) && (property.Required == Required.Default || property.Required == Required.AllowNull); JsonProperty value = await GenerateWithReferenceAndNullability(type, propertyAttributes, isNullable, schemaResolver, async delegate(JsonProperty p, JsonSchema4 s) { if (Settings.GenerateXmlObjects) p.GenerateXmlObjectForProperty(parentType, propertyName, propertyAttributes); if (!isNullable && Settings.SchemaType == SchemaType.Swagger2 && !parentSchema.RequiredProperties.Contains(propertyName)) parentSchema.RequiredProperties.Add(propertyName); object obj = propertyAttributes.TryGetIfAssignableTo("System.ComponentModel.ReadOnlyAttribute", TypeNameStyle.FullName); if ((dynamic)obj != null) p.IsReadOnly = ((byte)((dynamic)obj).IsReadOnly != 0); if (p.Description == null) p.Description = await propertyInfo.GetDescriptionAsync(propertyAttributes).ConfigureAwait(false); p.Default = ConvertDefaultValue(property); ApplyDataAnnotations(p, propertyTypeDescription, propertyAttributes); }).ConfigureAwait(false); parentSchema.Properties.Add(propertyName, value); } } private bool IsPropertyIgnored(Type propertyType, 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 IsPropertyIgnoredBySettings(propertyType, parentType, propertyAttributes); } private bool IsPropertyIgnoredBySettings(Type propertyType, Type parentType, Attribute[] propertyAttributes) { if (Settings.IgnoreObsoleteProperties && propertyAttributes.Any((Attribute a) => a is ObsoleteAttribute)) return true; return false; } private static dynamic GetDataMemberAttribute(Type parentType, Attribute[] propertyAttributes) { if (!HasDataContractAttribute(parentType)) return null; return propertyAttributes.TryGetIfAssignableTo("DataMemberAttribute", TypeNameStyle.Name); } private static bool HasDataContractAttribute(Type parentType) { return parentType.GetTypeInfo().GetCustomAttributes().TryGetIfAssignableTo("DataContractAttribute", TypeNameStyle.Name) != null; } public virtual void ApplyDataAnnotations(JsonSchema4 schema, JsonTypeDescription typeDescription, IEnumerable<Attribute> parentAttributes) { dynamic val = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DisplayAttribute", TypeNameStyle.FullName); dynamic val2 = val != null; if ((val2 ? false : true) ? val2 : (val2 & (val.Name != null))) schema.Title = (string)val.Name; dynamic val3 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DefaultValueAttribute", TypeNameStyle.FullName); if (val3 != null) schema.Default = (object)val3.Value; dynamic val4 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RegularExpressionAttribute", TypeNameStyle.FullName); if (val4 != null) { if (typeDescription.IsDictionary) schema.AdditionalPropertiesSchema.Pattern = (string)val4.Pattern; else schema.Pattern = (string)val4.Pattern; } if (typeDescription.Type == JsonObjectType.Number || typeDescription.Type == JsonObjectType.Integer) { dynamic val5 = parentAttributes.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))) schema.Minimum = (decimal)(double)val5.Minimum; val2 = (val5.Maximum != null); if ((val2 ? false : true) ? val2 : (val2 & (val5.Maximum < 1.7976931348623157E+308))) schema.Maximum = (decimal)(double)val5.Maximum; } MultipleOfAttribute multipleOfAttribute = parentAttributes.OfType<MultipleOfAttribute>().SingleOrDefault(); if (multipleOfAttribute != null) schema.MultipleOf = multipleOfAttribute.MultipleOf; } dynamic val6 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MinLengthAttribute", TypeNameStyle.FullName); val2 = (val6 != null); if ((val2 ? false : true) ? val2 : (val2 & (val6.Length != null))) { if (typeDescription.Type == JsonObjectType.String) schema.MinLength = (int?)val6.Length; else if (typeDescription.Type == JsonObjectType.Array) { schema.MinItems = (int)val6.Length; } } dynamic val7 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MaxLengthAttribute", TypeNameStyle.FullName); val2 = (val7 != null); if ((val2 ? false : true) ? val2 : (val2 & (val7.Length != null))) { if (typeDescription.Type == JsonObjectType.String) schema.MaxLength = (int?)val7.Length; else if (typeDescription.Type == JsonObjectType.Array) { schema.MaxItems = (int)val7.Length; } } dynamic val8 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.StringLengthAttribute", TypeNameStyle.FullName); if ((val8 != null) && typeDescription.Type == JsonObjectType.String) { schema.MinLength = (int?)val8.MinimumLength; schema.MaxLength = (int?)val8.MaximumLength; } dynamic val9 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DataTypeAttribute", TypeNameStyle.FullName); if (val9 != null) { dynamic val10 = val9.DataType.ToString(); if (DataTypeFormats.ContainsKey(val10)) schema.Format = (string)DataTypeFormats[val10]; } } private object ConvertDefaultValue(Newtonsoft.Json.Serialization.JsonProperty property) { if (property.DefaultValue != null && property.DefaultValue.GetType().GetTypeInfo().get_IsEnum()) { TypeInfo typeInfo = typeof(StringEnumConverter).GetTypeInfo(); JsonConverter converter = property.Converter; if (typeInfo.IsAssignableFrom((converter != null) ? converter.GetType().GetTypeInfo() : null)) return property.DefaultValue.ToString(); return (int)property.DefaultValue; } return property.DefaultValue; } } }