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

JsonSchemaGenerator

public class JsonSchemaGenerator
Generates a JsonSchema4 object for a given type.
using Microsoft.Runtime.CompilerServices; 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); 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(); 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; } return (JsonSchema4)val.GetResult(); } 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(); return schema; } public virtual async Task GenerateAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { JsonSchemaTypeAttribute jsonSchemaTypeAttribute = ReflectionExtensions.GetCustomAttribute<JsonSchemaTypeAttribute>(ReflectionExtensions.GetTypeInfo(type)) ?? ((parentAttributes != null) ? Enumerable.SingleOrDefault<JsonSchemaTypeAttribute>(Enumerable.OfType<JsonSchemaTypeAttribute>((IEnumerable)parentAttributes)) : null); if (jsonSchemaTypeAttribute != null) type = jsonSchemaTypeAttribute.Type; this.ApplyExtensionDataAttributes<TSchemaType>(type, schema, parentAttributes); ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<bool>(this.TryHandleSpecialTypesAsync<TSchemaType>(type, schema, schemaResolver, parentAttributes), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } ConfiguredTaskAwaitable val3; ConfiguredTaskAwaiter val4; if (val.GetResult() != null) { val3 = AwaitExtensions.ConfigureAwait(ApplySchemaProcessorsAsync(type, schema, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); val4 = val5; } val4.GetResult(); } else { if (schemaResolver.RootObject == schema) schema.Title = Settings.SchemaNameGenerator.Generate(type); JsonTypeDescription description = Settings.ReflectionService.GetDescription(type, parentAttributes, Settings); ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); if (description.Type.HasFlag(JsonObjectType.Object)) { if (description.IsDictionary) { description.ApplyType(schema); val3 = AwaitExtensions.ConfigureAwait(this.GenerateDictionaryAsync<TSchemaType>(schema, type, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); } else if (schemaResolver.HasSchema(type, false)) { ((JsonReferenceBase<JsonSchema4>)schema).Reference = schemaResolver.GetSchema(type, false); } else if (schema.GetType() == typeof(JsonSchema4)) { val3 = AwaitExtensions.ConfigureAwait(GenerateObjectAsync(type, description, schema, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); } else { ConfiguredTaskAwaiter val6 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(type, parentAttributes, schemaResolver), false).GetAwaiter(); if (!val6.get_IsCompleted()) { await val6; ConfiguredTaskAwaiter val7 = default(ConfiguredTaskAwaiter); val6 = val7; } ((JsonReferenceBase<JsonSchema4>)schema).Reference = (JsonSchema4)val6.GetResult(); } } else if (description.IsEnum) { val3 = AwaitExtensions.ConfigureAwait(this.GenerateEnum<TSchemaType>(schema, type, parentAttributes, description, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); } else if (description.Type.HasFlag(JsonObjectType.Array)) { val3 = AwaitExtensions.ConfigureAwait(this.GenerateArray<TSchemaType>(schema, type, description, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); } else { description.ApplyType(schema); } val3 = AwaitExtensions.ConfigureAwait(ApplySchemaProcessorsAsync(type, schema, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; } val4.GetResult(); } } public async Task<TSchemaType> GenerateWithReferenceAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema4, Task> transformation = null) where TSchemaType : JsonSchema4, new { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<TSchemaType>(this.GenerateWithReferenceAndNullabilityAsync<TSchemaType>(type, parentAttributes, false, schemaResolver, transformation), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } return (TSchemaType)val.GetResult(); } public async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<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); ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<TSchemaType>(this.GenerateWithReferenceAndNullabilityAsync<TSchemaType>(type, parentAttributes, description.IsNullable, schemaResolver, transformation), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } return (TSchemaType)val.GetResult(); } public virtual async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<TSchemaType>(Type type, IEnumerable<Attribute> parentAttributes, bool isNullable, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema4, Task> transformation = null) where TSchemaType : JsonSchema4, new { ConfiguredTaskAwaitable val3; ConfiguredTaskAwaiter val4; JsonSchema4 referencedSchema; if (!Settings.ReflectionService.GetDescription(type, parentAttributes, Settings).RequiresSchemaReference(Settings.TypeMappers)) { ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<TSchemaType>(this.GenerateAsync<TSchemaType>(type, parentAttributes, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } TSchemaType result = (TSchemaType)val.GetResult(); TSchemaType schema = result; if (!schema.HasReference) { if (transformation != null) { val3 = AwaitExtensions.ConfigureAwait(transformation(schema, schema), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); val4 = val5; } val4.GetResult(); } if (isNullable) { if (Settings.SchemaType == SchemaType.JsonSchema) { 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; } else if (Settings.SchemaType == SchemaType.OpenApi3) { schema.IsNullableRaw = isNullable; } } return schema; } referencedSchema = schema.ActualSchema; } else { ConfiguredTaskAwaiter val6 = AwaitExtensions.ConfigureAwait<JsonSchema4>(this.GenerateAsync<JsonSchema4>(type, parentAttributes, schemaResolver), false).GetAwaiter(); if (!val6.get_IsCompleted()) { await val6; ConfiguredTaskAwaiter val7 = default(ConfiguredTaskAwaiter); val6 = val7; } referencedSchema = (JsonSchema4)val6.GetResult(); } TSchemaType referencingSchema = new TSchemaType(); if (transformation != null) { val3 = AwaitExtensions.ConfigureAwait(transformation(referencingSchema, referencedSchema), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); val4 = val5; } val4.GetResult(); } if (isNullable) { if (Settings.SchemaType == SchemaType.JsonSchema) referencingSchema.OneOf.Add(new JsonSchema4 { Type = JsonObjectType.Null }); else if (Settings.SchemaType == SchemaType.OpenApi3) { referencingSchema.IsNullableRaw = true; } } 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 = (memberInfo != (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 '" + ((memberInfo != (MemberInfo)null) ? memberInfo.Name : "n/a") + "' and type '" + ((memberInfo?.DeclaringType != (Type)null) ? memberInfo.DeclaringType.FullName : "n/a") + "'.", innerException); } } protected virtual async Task GenerateObjectAsync(Type type, JsonTypeDescription typeDescription, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { schemaResolver.AddSchema(type, false, schema); JsonSchema4 rootSchema = schema; ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateInheritanceAsync(type, schema, schemaResolver), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } JsonSchema4 result = (JsonSchema4)val.GetResult(); ConfiguredTaskAwaitable val3; ConfiguredTaskAwaiter val4; ConfiguredTaskAwaiter val5 = default(ConfiguredTaskAwaiter); if (result != null) schema = result; else { val3 = AwaitExtensions.ConfigureAwait(GeneratePropertiesAsync(type, schema, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); val3 = AwaitExtensions.ConfigureAwait(ApplyAdditionalPropertiesAsync(type, schema, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; val5 = default(ConfiguredTaskAwaiter); } val4.GetResult(); } typeDescription.ApplyType(schema); JsonSchema4 jsonSchema = schema; ConfiguredTaskAwaiter val6 = AwaitExtensions.ConfigureAwait<string>(ReflectionExtensions.GetTypeInfo(type).GetDescriptionAsync(ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(type), true)), false).GetAwaiter(); if (!val6.get_IsCompleted()) { await val6; ConfiguredTaskAwaiter val7 = default(ConfiguredTaskAwaiter); val6 = val7; } jsonSchema.Description = (string)val6.GetResult(); schema.IsAbstract = ReflectionExtensions.GetTypeInfo(type).IsAbstract; GenerateInheritanceDiscriminator(type, rootSchema); val3 = AwaitExtensions.ConfigureAwait(GenerateKnownTypesAsync(type, schemaResolver), false); val4 = val3.GetAwaiter(); if (!val4.get_IsCompleted()) { await val4; val4 = val5; } val4.GetResult(); if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForType(type); } private async Task ApplyAdditionalPropertiesAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { PropertyInfo propertyInfo = Enumerable.FirstOrDefault<PropertyInfo>((IEnumerable<PropertyInfo>)ReflectionExtensions.GetRuntimeProperties(type), (Func<PropertyInfo, bool>)((PropertyInfo p) => Enumerable.Any<object>((IEnumerable<object>)p.GetCustomAttributes(false), (Func<object, bool>)((object a) => a.GetType().IsAssignableTo("JsonExtensionDataAttribute", TypeNameStyle.Name))))); if (propertyInfo != (PropertyInfo)null) { Type[] genericTypeArguments = propertyInfo.PropertyType.GetGenericTypeArguments(); Type type2 = (genericTypeArguments.Length == 2) ? genericTypeArguments[1] : typeof(object); ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(this.GenerateWithReferenceAndNullabilityAsync<JsonSchema4>(type2, (IEnumerable<Attribute>)null, schemaResolver, (Func<JsonSchema4, JsonSchema4, Task>)null), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } JsonSchema4 jsonSchema = schema.AdditionalPropertiesSchema = (JsonSchema4)val.GetResult(); } else schema.AllowAdditionalProperties = false; } private unsafe async Task ApplySchemaProcessorsAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { SchemaProcessorContext context = new SchemaProcessorContext(type, schema, schemaResolver, this, Settings); foreach (ISchemaProcessor schemaProcessor in Settings.SchemaProcessors) { ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(schemaProcessor.ProcessAsync(context), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); } IEnumerable<Attribute> enumerable = from a in ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(type), true) where a.GetType().IsAssignableTo("JsonSchemaProcessorAttribute", TypeNameStyle.Name) select a; foreach (dynamic item in enumerable) { dynamic val4 = Activator.CreateInstance(item.Type, item.Parameters).ProcessAsync(context).ConfigureAwait(false) .GetAwaiter(); if (!((byte)val4.IsCompleted != 0)) { ICriticalNotifyCompletion awaiter = val4 as ICriticalNotifyCompletion; AsyncTaskMethodBuilder asyncTaskMethodBuilder; if (awaiter == null) { INotifyCompletion awaiter2 = (INotifyCompletion)val4; asyncTaskMethodBuilder.AwaitOnCompleted(ref awaiter2, ref *(<ApplySchemaProcessorsAsync>d__17*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<ApplySchemaProcessorsAsync>d__17*)); awaiter = null; ; } val4.GetResult(); } } 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.Key, (JsonSchemaExtensionDataAttribute a) => a.Value); } else { JsonSchemaExtensionDataAttribute[] source2 = parentAttributes.OfType<JsonSchemaExtensionDataAttribute>().ToArray(); if (source2.Any()) schema.ExtensionData = source2.ToDictionary((JsonSchemaExtensionDataAttribute a) => a.Key, (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 && ReflectionExtensions.GetTypeInfo(type).IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); typeMapper = Enumerable.FirstOrDefault<ITypeMapper>((IEnumerable<ITypeMapper>)Settings.TypeMappers, (Func<ITypeMapper, bool>)((ITypeMapper m) => m.MappedType == genericType)); } if (typeMapper != null) { TypeMapperContext context = new TypeMapperContext(type, this, schemaResolver, parentAttributes); ConfiguredTaskAwaitable val = AwaitExtensions.ConfigureAwait(typeMapper.GenerateSchemaAsync(schema, context), false); ConfiguredTaskAwaiter val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; } val2.GetResult(); return true; } if (!type.IsAssignableTo("JArray", TypeNameStyle.Name) && (type.IsAssignableTo("JToken", TypeNameStyle.Name) || 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 (itemType != (Type)null) { <>c__DisplayClass20_0<TSchemaType> <>4__this; ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<JsonSchema4>(this.GenerateWithReferenceAndNullabilityAsync<JsonSchema4>(itemType, (IEnumerable<Attribute>)null, false, schemaResolver, (Func<JsonSchema4, JsonSchema4, Task>)delegate(JsonSchema4 s, JsonSchema4 r) { <>c__DisplayClass20_0<TSchemaType>.<<GenerateArray>b__0>d stateMachine = default(<>c__DisplayClass20_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__DisplayClass20_0<TSchemaType>.<<GenerateArray>b__0>d>(ref stateMachine); return stateMachine.<>t__builder.Task; }), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } JsonSchema4 jsonSchema = schema.Item = (JsonSchema4)val.GetResult(); if (Settings.GenerateXmlObjects) schema.GenerateXmlObjectForArrayType(type); } 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.Name == "Nullable`1") type = type.GetGenericArguments()[0]; bool isIntegerEnumeration = typeDescription.Type == JsonObjectType.Integer; if (schemaResolver.HasSchema(type, isIntegerEnumeration)) ((JsonReferenceBase<JsonSchema4>)schema).Reference = schemaResolver.GetSchema(type, isIntegerEnumeration); else if (schema.GetType() == typeof(JsonSchema4)) { LoadEnumerations(type, schema, typeDescription); typeDescription.ApplyType(schema); ConfiguredTaskAwaiter val = AwaitExtensions.ConfigureAwait<string>(type.GetXmlSummaryAsync(), false).GetAwaiter(); if (!val.get_IsCompleted()) { await val; ConfiguredTaskAwaiter val2 = default(ConfiguredTaskAwaiter); val = val2; } string text = schema.Description = (string)val.GetResult(); schemaResolver.AddSchema(type, isIntegerEnumeration, schema); } else { ConfiguredTaskAwaiter val3 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(type, parentAttributes, schemaResolver), false).GetAwaiter(); if (!val3.get_IsCompleted()) { await val3; ConfiguredTaskAwaiter val4 = default(ConfiguredTaskAwaiter); val3 = val4; } ((JsonReferenceBase<JsonSchema4>)schema).Reference = (JsonSchema4)val3.GetResult(); } } private async Task GenerateDictionaryAsync<TSchemaType>(TSchemaType schema, Type type, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema4, new { Type[] genericTypeArguments = type.GetGenericTypeArguments(); Type keyType = (genericTypeArguments.Length == 2) ? genericTypeArguments[0] : typeof(string); ConfiguredTaskAwaitable<JsonSchema4> val; ConfiguredTaskAwaiter val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); if (ReflectionExtensions.GetTypeInfo(keyType).IsEnum) { val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(keyType, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } JsonSchema4 result = (JsonSchema4)val2.GetResult(); if (Settings.ReflectionService.GetDescription(keyType, null, Settings).RequiresSchemaReference(Settings.TypeMappers)) schema.DictionaryKey = new JsonSchema4 { Reference = result }; else schema.DictionaryKey = result; } Type valueType = (genericTypeArguments.Length == 2) ? genericTypeArguments[1] : typeof(object); if (valueType == typeof(object)) schema.AdditionalPropertiesSchema = JsonSchema4.CreateAnySchema(); else { val = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(valueType, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; } !0 result2 = val2.GetResult(); if (Settings.ReflectionService.GetDescription(valueType, null, Settings).RequiresSchemaReference(Settings.TypeMappers)) schema.AdditionalPropertiesSchema = new JsonSchema4 { Reference = result2 }; else schema.AdditionalPropertiesSchema = (JsonSchema4)result2; } schema.AllowAdditionalProperties = true; } private async Task GeneratePropertiesAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { List<MemberInfo> propertiesAndFields = ReflectionExtensions.GetTypeInfo(type).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).Where(delegate(FieldInfo f) { if (f.IsPublic) return !f.IsStatic; return false; }) .OfType<MemberInfo>() .Concat(ReflectionExtensions.GetTypeInfo(type).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).Where(delegate(PropertyInfo p) { if (p.GetGetMethod()?.IsPublic ?? false) { MethodInfo getMethod2 = p.GetGetMethod(); if ((object)getMethod2 != null && !getMethod2.IsStatic) return true; } if (p.GetSetMethod()?.IsPublic ?? false) { MethodInfo setMethod2 = p.GetSetMethod(); 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; ConfiguredTaskAwaitable val; ConfiguredTaskAwaiter val2; if (jsonObjectContract != null && allowedProperties == null) { foreach (Newtonsoft.Json.Serialization.JsonProperty item in from p in jsonObjectContract.Properties where 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 && !(propertyInfo == (PropertyInfo)null)) { MethodInfo getMethod = propertyInfo.GetGetMethod(); if ((object)getMethod != null && getMethod.IsAbstract) continue; MethodInfo setMethod = propertyInfo.GetSetMethod(); if ((object)setMethod != null && setMethod.IsAbstract) continue; } val = AwaitExtensions.ConfigureAwait(LoadPropertyOrFieldAsync(item, memberInfo, type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); } } } else { foreach (MemberInfo item2 in propertiesAndFields.Where(delegate(MemberInfo m) { if (allowedProperties != null) return allowedProperties.Contains(m.Name); return true; })) { JsonPropertyAttribute jsonPropertyAttribute = item2.GetCustomAttributes(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, item2.GetCustomAttributes(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; val = AwaitExtensions.ConfigureAwait(LoadPropertyOrFieldAsync(obj2, item2, type, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); } } } protected virtual string[] GetTypeProperties(Type type) { if (type == typeof(Exception)) return new string[4] { "InnerException", "Message", "Source", "StackTrace" }; return null; } private unsafe async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver) { object[] attributes = ReflectionExtensions.GetTypeInfo(type).GetCustomAttributes(Settings.GetActualFlattenInheritanceHierarchy(type)); ConfiguredTaskAwaitable val2; ConfiguredTaskAwaiter val3; ConfiguredTaskAwaiter val4 = default(ConfiguredTaskAwaiter); if (Settings.GenerateKnownTypes) { IEnumerable<Attribute> enumerable = (from a in attributes where a.GetType().IsAssignableTo("KnownTypeAttribute", TypeNameStyle.Name) 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__25*)); awaiter2 = null; } else asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<GenerateKnownTypesAsync>d__25*)); awaiter = null; ; } val.GetResult(); } else { if (!((item.MethodName != null) ? true : false)) throw new ArgumentException("A KnownType attribute on " + type.FullName + " does not specify a type or a method name.", "type"); MethodInfo runtimeMethod = ReflectionExtensions.GetRuntimeMethod(type, (string)item.MethodName, new Type[0]); if (runtimeMethod != (MethodInfo)null) { IEnumerable<Type> enumerable2 = runtimeMethod.Invoke(null, null) as IEnumerable<Type>; if (enumerable2 != null) { foreach (Type item2 in enumerable2) { val2 = AwaitExtensions.ConfigureAwait(AddKnownTypeAsync(item2, schemaResolver), false); val3 = val2.GetAwaiter(); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(ConfiguredTaskAwaiter); } val3.GetResult(); } } } } } } foreach (object item3 in from a in attributes where a.GetType().IsAssignableTo("JsonInheritanceAttribute", TypeNameStyle.Name) select a) { Type type2 = item3.TryGetPropertyValue<Type>("Type", null); if (type2 != (Type)null) { val2 = AwaitExtensions.ConfigureAwait(AddKnownTypeAsync(type2, schemaResolver), false); val3 = val2.GetAwaiter(); if (!val3.get_IsCompleted()) { await val3; val3 = val4; val4 = default(ConfiguredTaskAwaiter); } val3.GetResult(); } } } private async Task AddKnownTypeAsync(Type type, JsonSchemaResolver schemaResolver) { bool isIntegerEnumeration = Settings.ReflectionService.GetDescription(type, null, Settings).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(); } } private async Task<JsonSchema4> GenerateInheritanceAsync(Type type, JsonSchema4 schema, JsonSchemaResolver schemaResolver) { Type baseType = ReflectionExtensions.GetTypeInfo(type).BaseType; ConfiguredTaskAwaitable val; ConfiguredTaskAwaiter val2; ConfiguredTaskAwaitable<JsonSchema4> val4; ConfiguredTaskAwaiter val5; ConfiguredTaskAwaiter val3 = default(ConfiguredTaskAwaiter); ConfiguredTaskAwaiter val6 = default(ConfiguredTaskAwaiter); if (baseType != (Type)null && baseType != typeof(object) && baseType != typeof(ValueType) && ReflectionExtensions.GetTypeInfo(baseType).GetCustomAttributes(false).TryGetIfAssignableTo("JsonSchemaIgnoreAttribute", TypeNameStyle.Name) == null && ReflectionExtensions.GetTypeInfo(baseType).GetCustomAttributes(false).TryGetIfAssignableTo("SwaggerIgnoreAttribute", TypeNameStyle.Name) == null) { string[] excludedTypeNames = Settings.ExcludedTypeNames; if (excludedTypeNames == null || !excludedTypeNames.Contains(baseType.FullName)) { if (!Settings.GetActualFlattenInheritanceHierarchy(type)) { JsonSchema4 actualSchema = new JsonSchema4(); val = AwaitExtensions.ConfigureAwait(GeneratePropertiesAsync(type, actualSchema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val = AwaitExtensions.ConfigureAwait(ApplyAdditionalPropertiesAsync(type, actualSchema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); bool requiresSchemaReference = Settings.ReflectionService.GetDescription(baseType, null, Settings).RequiresSchemaReference(Settings.TypeMappers); if (actualSchema.Properties.Any() | requiresSchemaReference) { val4 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateAsync(baseType, schemaResolver), false); val5 = val4.GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; val5 = val6; } !0 result = val5.GetResult(); if (requiresSchemaReference) { if (schemaResolver.RootObject != ((JsonSchema4)result).ActualSchema) schemaResolver.AppendSchema(((JsonSchema4)result).ActualSchema, Settings.SchemaNameGenerator.Generate(baseType)); schema.AllOf.Add(new JsonSchema4 { Reference = ((JsonSchema4)result).ActualSchema }); } else schema.AllOf.Add((JsonSchema4)result); schema.AllOf.Add(actualSchema); return actualSchema; } val = AwaitExtensions.ConfigureAwait(GenerateAsync(baseType, null, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; } val2.GetResult(); return schema; } if (!Settings.ReflectionService.GetDescription(baseType, null, Settings).IsDictionary && !type.IsArray) { val = AwaitExtensions.ConfigureAwait(GeneratePropertiesAsync(baseType, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val4 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateInheritanceAsync(baseType, schema, schemaResolver), false); val5 = val4.GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; val5 = val6; val6 = default(ConfiguredTaskAwaiter); } val5.GetResult(); GenerateInheritanceDiscriminator(baseType, schema); } } } if (Settings.GetActualFlattenInheritanceHierarchy(type) && Settings.GenerateAbstractProperties) { Type[] interfaces = ReflectionExtensions.GetTypeInfo(type).GetInterfaces(); foreach (Type i in interfaces) { if (!Settings.ReflectionService.GetDescription(i, null, Settings).IsDictionary && !type.IsArray && !ReflectionExtensions.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(ReflectionExtensions.GetTypeInfo(i))) { val = AwaitExtensions.ConfigureAwait(GeneratePropertiesAsync(i, schema, schemaResolver), false); val2 = val.GetAwaiter(); if (!val2.get_IsCompleted()) { await val2; val2 = val3; val3 = default(ConfiguredTaskAwaiter); } val2.GetResult(); val4 = AwaitExtensions.ConfigureAwait<JsonSchema4>(GenerateInheritanceAsync(i, schema, schemaResolver), false); val5 = val4.GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; val5 = val6; val6 = default(ConfiguredTaskAwaiter); } val5.GetResult(); GenerateInheritanceDiscriminator(i, schema); } } } return null; } private void GenerateInheritanceDiscriminator(Type type, JsonSchema4 schema) { if (!Settings.GetActualFlattenInheritanceHierarchy(type)) { object obj = TryGetInheritanceDiscriminatorConverter(type); if (obj != null) { string text = TryGetInheritanceDiscriminatorName(obj); if (schema.Properties.TryGetValue(text, out JsonProperty value) && (value.Type & JsonObjectType.String) == JsonObjectType.None) throw new InvalidOperationException("The JSON discriminator property '" + text + "' must be a string property on type '" + type.FullName + "' (it is recommended to not implement the discriminator property at all)."); OpenApiDiscriminator openApiDiscriminator2 = schema.DiscriminatorObject = new OpenApiDiscriminator { JsonInheritanceConverter = obj, PropertyName = text }; schema.Properties[text] = new JsonProperty { Type = JsonObjectType.String, IsRequired = true }; } else schema.BaseDiscriminator?.AddMapping(type, schema); } } private object TryGetInheritanceDiscriminatorConverter(Type type) { dynamic val = ReflectionExtensions.GetTypeInfo(type).GetCustomAttributes(false).OfType<Attribute>() .TryGetIfAssignableTo("JsonConverterAttribute", TypeNameStyle.Name); if ((val != null) && ((Type)val.ConverterType).IsAssignableTo("JsonInheritanceConverter", TypeNameStyle.Name)) { dynamic val2 = val.ConverterParameters != null; if (!(((val2 ? false : true) ? val2 : (val2 & (val.ConverterParameters.Length > 0))) ? true : false)) return Activator.CreateInstance(val.ConverterType); return Activator.CreateInstance(val.ConverterType, val.ConverterParameters); } return null; } private string TryGetInheritanceDiscriminatorName(dynamic jsonInheritanceConverter) { if (ReflectionExtensions.HasProperty(jsonInheritanceConverter, "DiscriminatorName")) return (string)jsonInheritanceConverter.DiscriminatorName; return JsonInheritanceConverter.DefaultDiscriminatorName; } private void LoadEnumerations(Type type, JsonSchema4 schema, JsonTypeDescription typeDescription) { schema.Type = typeDescription.Type; schema.Enumeration.Clear(); schema.EnumerationNames.Clear(); schema.IsFlagEnumerable = (ReflectionExtensions.GetTypeInfo(type).GetCustomAttribute<FlagsAttribute>() != null); Type underlyingType = Enum.GetUnderlyingType(type); List<JsonConverter> list = Settings.ActualSerializerSettings.Converters.ToList(); if (!list.OfType<StringEnumConverter>().Any()) list.Add(new StringEnumConverter()); 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 = 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 { string value = JsonConvert.SerializeObject(Enum.Parse(type, text), Formatting.None, list.ToArray()); schema.Enumeration.Add(JsonConvert.DeserializeObject<string>(value)); } } schema.EnumerationNames.Add(text); } } private async Task LoadPropertyOrFieldAsync(Newtonsoft.Json.Serialization.JsonProperty property, MemberInfo propertyInfo, Type parentType, JsonSchema4 parentSchema, JsonSchemaResolver schemaResolver) { Type propertyType = property.PropertyType; Attribute[] propertyAttributes = property.AttributeProvider.GetAttributes(true).ToArray(); JsonTypeDescription propertyTypeDescription = Settings.ReflectionService.GetDescription(propertyType, propertyAttributes, Settings); if (!property.Ignored && !IsPropertyIgnoredBySettings(propertyType, parentType, propertyAttributes)) { if (propertyType.Name == "Nullable`1") propertyType = propertyType.GetGenericArguments()[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 requiredAttribute = 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 hasRequiredAttribute = requiredAttribute != null; dynamic val2 = hasRequiredAttribute ? ((object)hasRequiredAttribute) : (hasRequiredAttribute | val); if ((val2) || ((val2 | flag) ? true : false)) parentSchema.RequiredProperties.Add(propertyName); bool isNullable = propertyTypeDescription.IsNullable && !hasRequiredAttribute && !((byte)val != 0) && (property.Required == Required.Default || property.Required == Required.AllowNull); ConfiguredTaskAwaiter val3 = AwaitExtensions.ConfigureAwait<JsonProperty>(GenerateWithReferenceAndNullabilityAsync(propertyType, propertyAttributes, isNullable, schemaResolver, async delegate(JsonProperty p, JsonSchema4 s) { if (Settings.GenerateXmlObjects) p.GenerateXmlObjectForProperty(propertyType, propertyName, propertyAttributes); if (hasRequiredAttribute && propertyTypeDescription.Type == JsonObjectType.String && !requiredAttribute.TryGetPropertyValue("AllowEmptyStrings", false)) p.MinLength = 1; 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) { ConfiguredTaskAwaiter val5 = AwaitExtensions.ConfigureAwait<string>(propertyInfo.GetDescriptionAsync(propertyAttributes), false).GetAwaiter(); if (!val5.get_IsCompleted()) { await val5; ConfiguredTaskAwaiter val6 = default(ConfiguredTaskAwaiter); val5 = val6; } string text = p.Description = (string)val5.GetResult(); } p.Default = ConvertDefaultValue(property); ApplyDataAnnotations(p, propertyTypeDescription, propertyAttributes); }), false).GetAwaiter(); if (!val3.get_IsCompleted()) { await val3; ConfiguredTaskAwaiter val4 = default(ConfiguredTaskAwaiter); val3 = val4; } JsonProperty result = (JsonProperty)val3.GetResult(); parentSchema.Properties.Add(propertyName, result); } } 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 ReflectionExtensions.GetCustomAttributes(ReflectionExtensions.GetTypeInfo(parentType), true).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) { if (typeDescription.IsEnum && typeDescription.Type.HasFlag(JsonObjectType.String)) schema.Default = (object)val3.Value?.ToString(); else 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) { ApplyRangeAttribute(schema, parentAttributes); MultipleOfAttribute multipleOfAttribute = parentAttributes.OfType<MultipleOfAttribute>().SingleOrDefault(); if (multipleOfAttribute != null) schema.MultipleOf = multipleOfAttribute.MultipleOf; } dynamic val5 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MinLengthAttribute", TypeNameStyle.FullName); val2 = (val5 != null); if ((val2 ? false : true) ? val2 : (val2 & (val5.Length != null))) { if (typeDescription.Type == JsonObjectType.String) schema.MinLength = (int?)val5.Length; else if (typeDescription.Type == JsonObjectType.Array) { schema.MinItems = (int)val5.Length; } } dynamic val6 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.MaxLengthAttribute", TypeNameStyle.FullName); val2 = (val6 != null); if ((val2 ? false : true) ? val2 : (val2 & (val6.Length != null))) { if (typeDescription.Type == JsonObjectType.String) schema.MaxLength = (int?)val6.Length; else if (typeDescription.Type == JsonObjectType.Array) { schema.MaxItems = (int)val6.Length; } } dynamic val7 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.StringLengthAttribute", TypeNameStyle.FullName); if ((val7 != null) && typeDescription.Type == JsonObjectType.String) { schema.MinLength = (int?)val7.MinimumLength; schema.MaxLength = (int?)val7.MaximumLength; } dynamic val8 = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.DataTypeAttribute", TypeNameStyle.FullName); if (val8 != null) { dynamic val9 = val8.DataType.ToString(); if (DataTypeFormats.ContainsKey(val9)) schema.Format = (string)DataTypeFormats[val9]; } } private void ApplyRangeAttribute(JsonSchema4 schema, IEnumerable<Attribute> parentAttributes) { dynamic val = parentAttributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RangeAttribute", TypeNameStyle.FullName); if (val != null) { if (val.Minimum != null) { if (val.OperandType == typeof(double)) { double num = (double)Convert.ChangeType(val.Minimum, typeof(double)); if (num > -1.7976931348623157E+308) schema.Minimum = (decimal)num; } else { decimal num2 = (decimal)Convert.ChangeType(val.Minimum, typeof(decimal)); if (num2 > decimal.MinValue) schema.Minimum = num2; } } if (val.Maximum != null) { if (val.OperandType == typeof(double)) { double num3 = (double)Convert.ChangeType(val.Maximum, typeof(double)); if (num3 < 1.7976931348623157E+308) schema.Maximum = (decimal)num3; } else { decimal num4 = (decimal)Convert.ChangeType(val.Maximum, typeof(decimal)); if (num4 < decimal.MaxValue) schema.Maximum = num4; } } } } private object ConvertDefaultValue(Newtonsoft.Json.Serialization.JsonProperty property) { if (property.DefaultValue != null && ReflectionExtensions.GetTypeInfo(property.DefaultValue.GetType()).IsEnum) { Type typeInfo = ReflectionExtensions.GetTypeInfo(typeof(StringEnumConverter)); JsonConverter converter = property.Converter; if (typeInfo.IsAssignableFrom((converter != null) ? ReflectionExtensions.GetTypeInfo(converter.GetType()) : null)) return property.DefaultValue.ToString(); return (int)property.DefaultValue; } return property.DefaultValue; } } }