JsonSchemaGenerator
Generates a JsonSchema object for a given type.
using Namotion.Reflection;
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<JsonSchema> GenerateAsync(Type type)
{
JsonSchema schema = new JsonSchema();
JsonSchemaResolver schemaResolver = new JsonSchemaResolver(schema, Settings);
await GenerateAsync(ContextualTypeExtensions.ToContextualType(type), schema, schemaResolver).ConfigureAwait(false);
return schema;
}
public Task<JsonSchema> GenerateAsync(Type type, JsonSchemaResolver schemaResolver)
{
return GenerateAsync<JsonSchema>(type, schemaResolver);
}
public Task<TSchemaType> GenerateAsync<TSchemaType>(Type type, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
return GenerateAsync<TSchemaType>(ContextualTypeExtensions.ToContextualType(type), schemaResolver);
}
public async Task<JsonSchema> GenerateAsync(ContextualType contextualType, JsonSchemaResolver schemaResolver)
{
return await GenerateAsync<JsonSchema>(contextualType, schemaResolver).ConfigureAwait(false);
}
public async Task<TSchemaType> GenerateAsync<TSchemaType>(ContextualType contextualType, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
TSchemaType schema = new TSchemaType();
await this.GenerateAsync<TSchemaType>(contextualType, schema, schemaResolver).ConfigureAwait(false);
return schema;
}
public Task GenerateAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
return GenerateAsync(ContextualTypeExtensions.ToContextualType(type), schema, schemaResolver);
}
public virtual async Task GenerateAsync<TSchemaType>(ContextualType contextualType, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
Type type = contextualType.get_OriginalType();
JsonSchemaTypeAttribute attribute = contextualType.GetAttribute<JsonSchemaTypeAttribute>();
if (attribute != null)
type = attribute.Type;
this.ApplyExtensionDataAttributes<TSchemaType>(contextualType, schema);
if (await this.TryHandleSpecialTypesAsync<TSchemaType>(contextualType, schema, schemaResolver).ConfigureAwait(false))
await ApplySchemaProcessorsAsync(contextualType, schema, schemaResolver).ConfigureAwait(false);
else {
if (schemaResolver.RootObject == schema)
schema.Title = Settings.SchemaNameGenerator.Generate(type);
JsonTypeDescription description = Settings.ReflectionService.GetDescription(contextualType, Settings);
if (description.Type.HasFlag(JsonObjectType.Object)) {
if (description.IsDictionary) {
description.ApplyType(schema);
await this.GenerateDictionaryAsync<TSchemaType>(schema, contextualType, schemaResolver).ConfigureAwait(false);
} else if (schemaResolver.HasSchema(type, false)) {
((JsonReferenceBase<JsonSchema>)schema).Reference = schemaResolver.GetSchema(type, false);
} else if (schema.GetType() == typeof(JsonSchema)) {
await GenerateObjectAsync(type, description, schema, schemaResolver).ConfigureAwait(false);
} else {
((JsonReferenceBase<JsonSchema>)schema).Reference = await GenerateAsync(contextualType, schemaResolver).ConfigureAwait(false);
}
} else if (description.IsEnum) {
await this.GenerateEnum<TSchemaType>(schema, contextualType, description, schemaResolver).ConfigureAwait(false);
} else if (description.Type.HasFlag(JsonObjectType.Array)) {
await this.GenerateArray<TSchemaType>(schema, contextualType, description, schemaResolver).ConfigureAwait(false);
} else {
description.ApplyType(schema);
}
await ApplySchemaProcessorsAsync(contextualType, schema, schemaResolver).ConfigureAwait(false);
}
}
public async Task<TSchemaType> GenerateWithReferenceAsync<TSchemaType>(ContextualType contextualType, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema, Task> transformation = null) where TSchemaType : JsonSchema, new
{
return await this.GenerateWithReferenceAndNullabilityAsync<TSchemaType>(contextualType, false, schemaResolver, transformation).ConfigureAwait(false);
}
public async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<TSchemaType>(ContextualType contextualType, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema, Task> transformation = null) where TSchemaType : JsonSchema, new
{
JsonTypeDescription description = Settings.ReflectionService.GetDescription(contextualType, Settings);
return await this.GenerateWithReferenceAndNullabilityAsync<TSchemaType>(contextualType, description.IsNullable, schemaResolver, transformation).ConfigureAwait(false);
}
public virtual async Task<TSchemaType> GenerateWithReferenceAndNullabilityAsync<TSchemaType>(ContextualType contextualType, bool isNullable, JsonSchemaResolver schemaResolver, Func<TSchemaType, JsonSchema, Task> transformation = null) where TSchemaType : JsonSchema, new
{
JsonSchema referencedSchema;
if (!Settings.ReflectionService.GetDescription(contextualType, Settings).RequiresSchemaReference(Settings.TypeMappers)) {
TSchemaType schema = await this.GenerateAsync<TSchemaType>(contextualType, schemaResolver).ConfigureAwait(false);
if (!schema.HasReference) {
if (transformation != null)
await transformation(schema, schema).ConfigureAwait(false);
if (isNullable) {
if (Settings.SchemaType == SchemaType.JsonSchema) {
if (schema.Type == JsonObjectType.None) {
schema.OneOf.Add(new JsonSchema {
Type = JsonObjectType.None
});
schema.OneOf.Add(new JsonSchema {
Type = JsonObjectType.Null
});
} else
schema.Type |= JsonObjectType.Null;
} else if (Settings.SchemaType == SchemaType.OpenApi3 || Settings.GenerateCustomNullableProperties) {
schema.IsNullableRaw = isNullable;
}
}
return schema;
}
referencedSchema = schema.ActualSchema;
} else
referencedSchema = await this.GenerateAsync<JsonSchema>(contextualType, schemaResolver).ConfigureAwait(false);
TSchemaType referencingSchema = new TSchemaType();
if (transformation != null)
await transformation(referencingSchema, referencedSchema).ConfigureAwait(false);
if (isNullable) {
if (Settings.SchemaType == SchemaType.JsonSchema)
referencingSchema.OneOf.Add(new JsonSchema {
Type = JsonObjectType.Null
});
else if (Settings.SchemaType == SchemaType.OpenApi3 || Settings.GenerateCustomNullableProperties) {
referencingSchema.IsNullableRaw = true;
}
}
if ((Settings.AllowReferencesWithProperties || !Enumerable.Any<JProperty>(JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(referencingSchema)).Properties())) && referencingSchema.OneOf.Count == 0)
((JsonReferenceBase<JsonSchema>)referencingSchema).Reference = referencedSchema.ActualSchema;
else if (Settings.SchemaType != SchemaType.Swagger2) {
referencingSchema.OneOf.Add(new JsonSchema {
Reference = referencedSchema.ActualSchema
});
} else {
referencingSchema.AllOf.Add(new JsonSchema {
Reference = referencedSchema.ActualSchema
});
}
return referencingSchema;
}
public virtual string GetPropertyName(JsonProperty jsonProperty, MemberInfo memberInfo)
{
try {
string text = (memberInfo != (MemberInfo)null) ? ContextualTypeExtensions.GetContextualPropertiesAndFields(memberInfo.DeclaringType).First((ContextualMemberInfo p) => p.get_Name() == memberInfo.Name).GetName() : jsonProperty.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);
}
}
public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription typeDescription, IEnumerable<Attribute> parentAttributes)
{
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.DisplayAttribute", 1);
dynamic val2 = val != null;
if ((val2 ? false : true) ? val2 : (val2 & (val.Name != null)))
schema.Title = (string)val.Name;
dynamic val3 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DefaultValueAttribute", 1);
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 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.RegularExpressionAttribute", 1);
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 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.MinLengthAttribute", 1);
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 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.MaxLengthAttribute", 1);
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 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.StringLengthAttribute", 1);
if ((val7 != null) && typeDescription.Type == JsonObjectType.String) {
schema.MinLength = (int?)val7.MinimumLength;
schema.MaxLength = (int?)val7.MaximumLength;
}
dynamic val8 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.DataTypeAttribute", 1);
if (val8 != null) {
dynamic val9 = val8.DataType.ToString();
if (DataTypeFormats.ContainsKey(val9))
schema.Format = (string)DataTypeFormats[val9];
}
}
public virtual object ConvertDefaultValue(ContextualType type, object defaultValue)
{
if (defaultValue != null && defaultValue.GetType().GetTypeInfo().IsEnum) {
if (Settings.ReflectionService.IsStringEnum(type, Settings.ActualSerializerSettings))
return defaultValue.ToString();
return (int)defaultValue;
}
return defaultValue;
}
protected virtual async Task GenerateObjectAsync(Type type, JsonTypeDescription typeDescription, JsonSchema schema, JsonSchemaResolver schemaResolver)
{
schemaResolver.AddSchema(type, false, schema);
JsonSchema rootSchema = schema;
JsonSchema jsonSchema = await GenerateInheritanceAsync(type, schema, schemaResolver).ConfigureAwait(false);
if (jsonSchema == null) {
await GeneratePropertiesAsync(type, schema, schemaResolver).ConfigureAwait(false);
await ApplyAdditionalPropertiesAsync(type, schema, schemaResolver).ConfigureAwait(false);
} else
schema = jsonSchema;
if (!schema.Type.HasFlag(JsonObjectType.Array))
typeDescription.ApplyType(schema);
JsonSchema jsonSchema2 = schema;
jsonSchema2.Description = await ContextualTypeExtensions.ToCachedType(type).GetDescriptionAsync(DescriptionAttributeType.Context).ConfigureAwait(false);
if (Settings.GetActualGenerateAbstractSchema(type))
schema.IsAbstract = type.GetTypeInfo().IsAbstract;
GenerateInheritanceDiscriminator(type, rootSchema, schema);
await GenerateKnownTypesAsync(type, schemaResolver).ConfigureAwait(false);
if (Settings.GenerateXmlObjects)
schema.GenerateXmlObjectForType(type);
}
protected virtual string[] GetTypeProperties(Type type)
{
if (type == typeof(Exception))
return new string[4] {
"InnerException",
"Message",
"Source",
"StackTrace"
};
return null;
}
private async Task ApplyAdditionalPropertiesAsync<TSchemaType>(Type type, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
ContextualPropertyInfo val = Enumerable.FirstOrDefault<ContextualPropertyInfo>((IEnumerable<ContextualPropertyInfo>)ContextualTypeExtensions.GetContextualProperties(type), (Func<ContextualPropertyInfo, bool>)((ContextualPropertyInfo p) => p.GetContextAttribute<JsonExtensionDataAttribute>() != null));
if (val != null) {
ContextualType[] genericArguments = val.get_GenericArguments();
ContextualType contextualType = (genericArguments.Length == 2) ? ((object)genericArguments[1]) : ((object)ContextualTypeExtensions.ToContextualType(typeof(object)));
schema.AdditionalPropertiesSchema = await this.GenerateWithReferenceAndNullabilityAsync<JsonSchema>(contextualType, schemaResolver, (Func<JsonSchema, JsonSchema, Task>)null).ConfigureAwait(false);
} else
schema.AllowAdditionalProperties = false;
}
private unsafe async Task ApplySchemaProcessorsAsync(ContextualType contextualType, JsonSchema schema, JsonSchemaResolver schemaResolver)
{
SchemaProcessorContext context = new SchemaProcessorContext(contextualType.get_OriginalType(), schema, schemaResolver, this, Settings);
foreach (ISchemaProcessor schemaProcessor in Settings.SchemaProcessors) {
await schemaProcessor.ProcessAsync(context).ConfigureAwait(false);
}
IEnumerable<Attribute> assignableToTypeName = EnumerableExtensions.GetAssignableToTypeName<Attribute>((IEnumerable<Attribute>)contextualType.get_TypeAttributes(), "JsonSchemaProcessorAttribute", 0);
foreach (dynamic item in assignableToTypeName) {
dynamic val = Activator.CreateInstance(item.Type, item.Parameters).ProcessAsync(context).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 *(<ApplySchemaProcessorsAsync>d__21*));
awaiter2 = null;
} else
asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<ApplySchemaProcessorsAsync>d__21*));
awaiter = null;
;
}
val.GetResult();
}
}
private void ApplyExtensionDataAttributes<TSchemaType>(ContextualType contextualType, TSchemaType schema) where TSchemaType : JsonSchema, new
{
JsonSchemaExtensionDataAttribute[] source = contextualType.GetAttributes<JsonSchemaExtensionDataAttribute>().ToArray();
if (source.Any())
schema.ExtensionData = source.ToDictionary((JsonSchemaExtensionDataAttribute a) => a.Key, (JsonSchemaExtensionDataAttribute a) => a.Value);
else {
source = contextualType.GetAttributes<JsonSchemaExtensionDataAttribute>().ToArray();
if (source.Any())
schema.ExtensionData = source.ToDictionary((JsonSchemaExtensionDataAttribute a) => a.Key, (JsonSchemaExtensionDataAttribute a) => a.Value);
}
}
private async Task<bool> TryHandleSpecialTypesAsync<TSchemaType>(ContextualType contextualType, TSchemaType schema, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
ITypeMapper typeMapper = Enumerable.FirstOrDefault<ITypeMapper>((IEnumerable<ITypeMapper>)Settings.TypeMappers, (Func<ITypeMapper, bool>)((ITypeMapper m) => m.MappedType == contextualType.get_OriginalType()));
if (typeMapper == null && contextualType.get_OriginalType().GetTypeInfo().IsGenericType) {
Type genericType = contextualType.get_OriginalType().GetGenericTypeDefinition();
typeMapper = Enumerable.FirstOrDefault<ITypeMapper>((IEnumerable<ITypeMapper>)Settings.TypeMappers, (Func<ITypeMapper, bool>)((ITypeMapper m) => m.MappedType == genericType));
}
if (typeMapper != null) {
TypeMapperContext context = new TypeMapperContext(contextualType.get_OriginalType(), this, schemaResolver, contextualType.get_ContextAttributes());
await typeMapper.GenerateSchemaAsync(schema, context).ConfigureAwait(false);
return true;
}
if (!TypeExtensions.IsAssignableToTypeName(contextualType.get_OriginalType(), "JArray", 0) && (TypeExtensions.IsAssignableToTypeName(contextualType.get_OriginalType(), "JToken", 0) || contextualType.get_OriginalType() == typeof(object)))
return true;
return false;
}
private async Task GenerateArray<TSchemaType>(TSchemaType schema, ContextualType contextualType, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
typeDescription.ApplyType(schema);
Type type = contextualType.GetTypeAttribute<JsonSchemaAttribute>()?.ArrayItem ?? TypeExtensions.GetEnumerableItemType(contextualType.get_OriginalType());
if (type != (Type)null) {
ContextualType contextualItemType = ContextualTypeExtensions.ToContextualType(type);
bool isNullable = contextualType.GetContextAttribute<ItemsCanBeNullAttribute>() != null || (int)contextualItemType.get_Nullability() == 3;
<>c__DisplayClass24_0<TSchemaType> <>4__this;
schema.Item = await this.GenerateWithReferenceAndNullabilityAsync<JsonSchema>(contextualItemType, isNullable, schemaResolver, (Func<JsonSchema, JsonSchema, Task>)delegate(JsonSchema itemSchema, JsonSchema typeSchema) {
<>c__DisplayClass24_0<TSchemaType>.<<GenerateArray>b__0>d stateMachine = default(<>c__DisplayClass24_0<TSchemaType>.<<GenerateArray>b__0>d);
stateMachine.<>4__this = <>4__this;
stateMachine.itemSchema = itemSchema;
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>1__state = -1;
AsyncTaskMethodBuilder <>t__builder = stateMachine.<>t__builder;
<>t__builder.Start<<>c__DisplayClass24_0<TSchemaType>.<<GenerateArray>b__0>d>(ref stateMachine);
return stateMachine.<>t__builder.Task;
}).ConfigureAwait(false);
if (Settings.GenerateXmlObjects)
schema.GenerateXmlObjectForArrayType();
} else
schema.Item = JsonSchema.CreateAnySchema();
}
private async Task GenerateEnum<TSchemaType>(TSchemaType schema, ContextualType contextualType, JsonTypeDescription typeDescription, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
Type type = contextualType.get_Type();
bool isIntegerEnumeration = typeDescription.Type == JsonObjectType.Integer;
if (schemaResolver.HasSchema(type, isIntegerEnumeration))
((JsonReferenceBase<JsonSchema>)schema).Reference = schemaResolver.GetSchema(type, isIntegerEnumeration);
else if (schema.GetType() == typeof(JsonSchema)) {
typeDescription.ApplyType(schema);
schema.Description = await XmlDocsExtensions.GetXmlDocsSummaryAsync(type).ConfigureAwait(false);
LoadEnumerations(type, schema, typeDescription);
schemaResolver.AddSchema(type, isIntegerEnumeration, schema);
} else {
((JsonReferenceBase<JsonSchema>)schema).Reference = await GenerateAsync(contextualType, schemaResolver).ConfigureAwait(false);
}
}
private async Task GenerateDictionaryAsync<TSchemaType>(TSchemaType schema, ContextualType contextualType, JsonSchemaResolver schemaResolver) where TSchemaType : JsonSchema, new
{
ContextualType[] genericTypeArguments = contextualType.get_GenericArguments();
ContextualType val = (genericTypeArguments.Length == 2) ? ((object)genericTypeArguments[0]) : ((object)ContextualTypeExtensions.ToContextualType(typeof(string)));
if (val.get_OriginalType().GetTypeInfo().IsEnum)
schema.DictionaryKey = await this.GenerateWithReferenceAsync<JsonSchema>(val, schemaResolver, (Func<JsonSchema, JsonSchema, Task>)null).ConfigureAwait(false);
ContextualType val2 = (genericTypeArguments.Length == 2) ? ((object)genericTypeArguments[1]) : ((object)ContextualTypeExtensions.ToContextualType(typeof(object)));
if (val2.get_OriginalType() == typeof(object))
schema.AdditionalPropertiesSchema = JsonSchema.CreateAnySchema();
else
schema.AdditionalPropertiesSchema = await this.GenerateWithReferenceAndNullabilityAsync<JsonSchema>(val2, val2.GetContextAttribute<ItemsCanBeNullAttribute>() != null || val2.get_OriginalType().Name == "Nullable`1", schemaResolver, (Func<JsonSchema, JsonSchema, Task>)null).ConfigureAwait(false);
schema.AllowAdditionalProperties = true;
}
private async Task GeneratePropertiesAsync(Type type, JsonSchema schema, JsonSchemaResolver schemaResolver)
{
List<MemberInfo> source = 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();
IEnumerable<ContextualMemberInfo> contextualMembers = from m in (IEnumerable<MemberInfo>)source
select ContextualTypeExtensions.ToContextualMember(m);
JsonContract jsonContract = Settings.ResolveContract(type);
string[] allowedProperties = GetTypeProperties(type);
JsonObjectContract jsonObjectContract = jsonContract as JsonObjectContract;
if (jsonObjectContract != null && allowedProperties == null) {
foreach (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) {
ContextualMemberInfo val = contextualMembers.FirstOrDefault((Func<ContextualMemberInfo, bool>)((ContextualMemberInfo p) => p.get_Name() == item.UnderlyingName));
ContextualPropertyInfo val2 = val as ContextualPropertyInfo;
if (!Settings.GenerateAbstractProperties && val2 != null && !val2.get_PropertyInfo().DeclaringType.GetTypeInfo().IsInterface) {
MethodInfo getMethod = val2.get_PropertyInfo().GetMethod;
if ((object)getMethod != null && getMethod.IsAbstract)
continue;
MethodInfo setMethod = val2.get_PropertyInfo().SetMethod;
if ((object)setMethod != null && setMethod.IsAbstract)
continue;
}
await LoadPropertyOrFieldAsync(item, val, type, schema, schemaResolver).ConfigureAwait(false);
}
}
} else {
foreach (ContextualMemberInfo item2 in contextualMembers.Where(delegate(ContextualMemberInfo m) {
if (allowedProperties != null)
return allowedProperties.Contains(m.get_Name());
return true;
})) {
JsonPropertyAttribute contextAttribute = item2.GetContextAttribute<JsonPropertyAttribute>();
object obj2 = (object)(item2 as ContextualPropertyInfo);
object obj3 = (obj2 != null) ? obj2.get_PropertyInfo().PropertyType : null;
if (obj3 == null) {
object obj4 = (object)(item2 as ContextualFieldInfo);
obj3 = ((obj4 != null) ? obj4.get_FieldInfo().FieldType : null);
}
object propertyType = obj3;
JsonProperty obj5 = new JsonProperty {
AttributeProvider = new ReflectionAttributeProvider(item2),
PropertyType = propertyType,
Ignored = IsPropertyIgnored(item2, type)
};
if (contextAttribute != null) {
obj5.PropertyName = (contextAttribute.PropertyName ?? item2.get_Name());
obj5.Required = contextAttribute.Required;
obj5.DefaultValueHandling = contextAttribute.DefaultValueHandling;
obj5.TypeNameHandling = contextAttribute.TypeNameHandling;
obj5.NullValueHandling = contextAttribute.NullValueHandling;
obj5.TypeNameHandling = contextAttribute.TypeNameHandling;
} else
obj5.PropertyName = item2.get_Name();
await LoadPropertyOrFieldAsync(obj5, item2, type, schema, schemaResolver).ConfigureAwait(false);
}
}
}
private unsafe async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaResolver)
{
object[] attributes = type.GetTypeInfo().GetCustomAttributes(Settings.GetActualFlattenInheritanceHierarchy(type));
if (Settings.GenerateKnownTypes) {
IEnumerable<Attribute> enumerable = EnumerableExtensions.GetAssignableToTypeName<object>((IEnumerable<object>)attributes, "KnownTypeAttribute", 0).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__28*));
awaiter2 = null;
} else
asyncTaskMethodBuilder.AwaitUnsafeOnCompleted(ref awaiter, ref *(<GenerateKnownTypesAsync>d__28*));
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 = type.GetRuntimeMethod((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) {
await AddKnownTypeAsync(item2, schemaResolver).ConfigureAwait(false);
}
}
}
}
}
}
foreach (object item3 in EnumerableExtensions.GetAssignableToTypeName<object>((IEnumerable<object>)attributes, "JsonInheritanceAttribute", 0)) {
Type type2 = ObjectExtensions.TryGetPropertyValue<Type>(item3, "Type", (Type)null);
if (type2 != (Type)null)
await AddKnownTypeAsync(type2, schemaResolver).ConfigureAwait(false);
}
}
private async Task AddKnownTypeAsync(Type type, JsonSchemaResolver schemaResolver)
{
bool isIntegerEnumeration = Settings.ReflectionService.GetDescription(ContextualTypeExtensions.ToContextualType(type), Settings).Type == JsonObjectType.Integer;
if (!schemaResolver.HasSchema(type, isIntegerEnumeration))
await GenerateAsync(type, schemaResolver).ConfigureAwait(false);
}
private async Task<JsonSchema> GenerateInheritanceAsync(Type type, JsonSchema schema, JsonSchemaResolver schemaResolver)
{
Type baseType = type.GetTypeInfo().BaseType;
if (baseType != (Type)null && baseType != typeof(object) && baseType != typeof(ValueType) && EnumerableExtensions.FirstAssignableToTypeNameOrDefault<object>((IEnumerable<object>)baseType.GetTypeInfo().GetCustomAttributes(false), "JsonSchemaIgnoreAttribute", 0) == null && EnumerableExtensions.FirstAssignableToTypeNameOrDefault<object>((IEnumerable<object>)baseType.GetTypeInfo().GetCustomAttributes(false), "SwaggerIgnoreAttribute", 0) == null) {
string[] excludedTypeNames = Settings.ExcludedTypeNames;
if (excludedTypeNames == null || !excludedTypeNames.Contains(baseType.FullName)) {
if (!Settings.GetActualFlattenInheritanceHierarchy(type)) {
JsonSchema actualSchema = new JsonSchema();
await GeneratePropertiesAsync(type, actualSchema, schemaResolver).ConfigureAwait(false);
await ApplyAdditionalPropertiesAsync(type, actualSchema, schemaResolver).ConfigureAwait(false);
bool requiresSchemaReference = Settings.ReflectionService.GetDescription(ContextualTypeExtensions.ToContextualType(baseType), Settings).RequiresSchemaReference(Settings.TypeMappers);
if (actualSchema.Properties.Any() | requiresSchemaReference) {
JsonSchema jsonSchema = await GenerateAsync(baseType, schemaResolver).ConfigureAwait(false);
if (requiresSchemaReference) {
if (schemaResolver.RootObject != jsonSchema.ActualSchema)
schemaResolver.AppendSchema(jsonSchema.ActualSchema, Settings.SchemaNameGenerator.Generate(baseType));
schema.AllOf.Add(new JsonSchema {
Reference = jsonSchema.ActualSchema
});
} else
schema.AllOf.Add(jsonSchema);
schema.AllOf.Add(actualSchema);
return actualSchema;
}
await GenerateAsync(ContextualTypeExtensions.ToContextualType(baseType), schema, schemaResolver).ConfigureAwait(false);
return schema;
}
if (!Settings.ReflectionService.GetDescription(ContextualTypeExtensions.ToContextualType(baseType), Settings).IsDictionary && !type.IsArray) {
await GeneratePropertiesAsync(baseType, schema, schemaResolver).ConfigureAwait(false);
GenerateInheritanceDiscriminator(baseType, schema, (await GenerateInheritanceAsync(baseType, schema, schemaResolver).ConfigureAwait(false)) ?? schema);
}
}
}
if (Settings.GetActualFlattenInheritanceHierarchy(type) && Settings.GenerateAbstractProperties) {
foreach (Type implementedInterface in type.GetTypeInfo().ImplementedInterfaces) {
if (!Settings.ReflectionService.GetDescription(ContextualTypeExtensions.ToContextualType(implementedInterface), Settings).IsDictionary && !type.IsArray && !IntrospectionExtensions.GetTypeInfo(typeof(IEnumerable)).IsAssignableFrom(implementedInterface.GetTypeInfo())) {
await GeneratePropertiesAsync(implementedInterface, schema, schemaResolver).ConfigureAwait(false);
GenerateInheritanceDiscriminator(implementedInterface, schema, (await GenerateInheritanceAsync(implementedInterface, schema, schemaResolver).ConfigureAwait(false)) ?? schema);
}
}
}
return null;
}
private void GenerateInheritanceDiscriminator(Type type, JsonSchema schema, JsonSchema typeSchema)
{
if (!Settings.GetActualFlattenInheritanceHierarchy(type)) {
object obj = TryGetInheritanceDiscriminatorConverter(type);
if (obj != null) {
string text = TryGetInheritanceDiscriminatorName(obj);
if (typeSchema.Properties.TryGetValue(text, out JsonSchemaProperty 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 = typeSchema.DiscriminatorObject = new OpenApiDiscriminator {
JsonInheritanceConverter = obj,
PropertyName = text
};
typeSchema.Properties[text] = new JsonSchemaProperty {
Type = JsonObjectType.String,
IsRequired = true
};
} else
(schema.ResponsibleDiscriminatorObject ?? schema.ActualTypeSchema.ResponsibleDiscriminatorObject)?.AddMapping(type, schema);
}
}
private object TryGetInheritanceDiscriminatorConverter(Type type)
{
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.GetTypeInfo().GetCustomAttributes(false).OfType<Attribute>(), "JsonConverterAttribute", 0);
if ((val != null) && TypeExtensions.IsAssignableToTypeName((Type)val.ConverterType, "JsonInheritanceConverter", 0)) {
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 (ObjectExtensions.HasProperty(jsonInheritanceConverter, "DiscriminatorName"))
return (string)jsonInheritanceConverter.DiscriminatorName;
return JsonInheritanceConverter.DefaultDiscriminatorName;
}
private void LoadEnumerations(Type type, JsonSchema schema, JsonTypeDescription typeDescription)
{
schema.Type = typeDescription.Type;
schema.Enumeration.Clear();
schema.EnumerationNames.Clear();
schema.IsFlagEnumerable = (ContextualTypeExtensions.ToCachedType(type).GetTypeAttribute<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 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.GetTypeInfo().GetDeclaredField(text).GetCustomAttributes(), "System.Runtime.Serialization.EnumMemberAttribute", 1);
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);
}
if (typeDescription.Type == JsonObjectType.Integer && Settings.GenerateEnumMappingDescription)
schema.Description = (schema.Description + "\n\n" + string.Join("\n", schema.Enumeration.Select((object e, int i) => e + " = " + schema.EnumerationNames[i]))).Trim();
}
private async Task LoadPropertyOrFieldAsync(JsonProperty jsonProperty, ContextualMemberInfo memberInfo, Type parentType, JsonSchema parentSchema, JsonSchemaResolver schemaResolver)
{
JsonTypeDescription propertyTypeDescription = Settings.ReflectionService.GetDescription(memberInfo, Settings);
if (!jsonProperty.Ignored && !IsPropertyIgnoredBySettings(memberInfo)) {
string propertyName = GetPropertyName(jsonProperty, memberInfo.get_MemberInfo());
if (parentSchema.Properties.ContainsKey(propertyName))
throw new InvalidOperationException("The JSON property '" + propertyName + "' is defined multiple times on type '" + parentType.FullName + "'.");
Attribute requiredAttribute = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)memberInfo.get_ContextAttributes(), "System.ComponentModel.DataAnnotations.RequiredAttribute", 1);
bool flag = jsonProperty.Required == Required.Always || jsonProperty.Required == Required.AllowNull;
dynamic val = GetDataMemberAttribute(memberInfo, parentType)?.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) && (jsonProperty.Required == Required.Default || jsonProperty.Required == Required.AllowNull);
Func<JsonSchemaProperty, JsonSchema, Task> transformation = async delegate(JsonSchemaProperty propertySchema, JsonSchema typeSchema) {
if (Settings.GenerateXmlObjects)
propertySchema.GenerateXmlObjectForProperty(memberInfo, propertyName);
if (hasRequiredAttribute && propertyTypeDescription.Type == JsonObjectType.String && !ObjectExtensions.TryGetPropertyValue<bool>((object)requiredAttribute, "AllowEmptyStrings", false))
propertySchema.MinLength = 1;
if (!isNullable && Settings.SchemaType == SchemaType.Swagger2 && !parentSchema.RequiredProperties.Contains(propertyName))
parentSchema.RequiredProperties.Add(propertyName);
object obj = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)memberInfo.get_ContextAttributes(), "System.ComponentModel.ReadOnlyAttribute", 1);
if ((dynamic)obj != null)
propertySchema.IsReadOnly = ((byte)((dynamic)obj).IsReadOnly != 0);
if (propertySchema.Description == null)
propertySchema.Description = await memberInfo.GetDescriptionAsync(DescriptionAttributeType.Context).ConfigureAwait(false);
propertySchema.Default = ConvertDefaultValue(memberInfo, jsonProperty.DefaultValue);
ApplyDataAnnotations(propertySchema, propertyTypeDescription, memberInfo.get_ContextAttributes());
};
JsonSchemaProperty value = await GenerateWithReferenceAndNullabilityAsync(memberInfo, isNullable, schemaResolver, transformation).ConfigureAwait(false);
parentSchema.Properties.Add(propertyName, value);
}
}
private bool IsPropertyIgnored(ContextualMemberInfo property, Type parentType)
{
if (property.GetContextAttribute<JsonIgnoreAttribute>() != null)
return true;
bool flag = property.GetContextAttribute<JsonPropertyAttribute>() == null && HasDataContractAttribute(parentType);
if ((!flag) ? ((object)flag) : (flag & (GetDataMemberAttribute(property, parentType) == null)))
return true;
return IsPropertyIgnoredBySettings(property);
}
private bool IsPropertyIgnoredBySettings(ContextualMemberInfo property)
{
if (Settings.IgnoreObsoleteProperties && property.GetContextAttribute<ObsoleteAttribute>() != null)
return true;
return false;
}
private dynamic GetDataMemberAttribute(ContextualMemberInfo property, Type parentType)
{
if (!HasDataContractAttribute(parentType))
return null;
return EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)property.get_ContextAttributes(), "DataMemberAttribute", 0);
}
private bool HasDataContractAttribute(Type parentType)
{
return EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)ContextualTypeExtensions.ToCachedType(parentType).get_TypeAttributes(), "DataContractAttribute", 0) != null;
}
private void ApplyRangeAttribute(JsonSchema schema, IEnumerable<Attribute> parentAttributes)
{
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(parentAttributes, "System.ComponentModel.DataAnnotations.RangeAttribute", 1);
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;
}
}
}
}
}
}