JsonSchemaGeneratorSettings
The JSON Schema generator settings.
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NJsonSchema.Generation.TypeMappers;
using NJsonSchema.Infrastructure;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
namespace NJsonSchema.Generation
{
public class JsonSchemaGeneratorSettings
{
public ReferenceTypeNullHandling DefaultReferenceTypeNullHandling { get; set; }
public bool GenerateAbstractProperties { get; set; }
public bool FlattenInheritanceHierarchy { get; set; }
public bool GenerateKnownTypes { get; set; } = true;
public bool GenerateXmlObjects { get; set; }
public bool IgnoreObsoleteProperties { get; set; }
public bool AllowReferencesWithProperties { get; set; }
public SchemaType SchemaType { get; set; }
[JsonIgnore]
public JsonSerializerSettings SerializerSettings { get; set; }
public string[] ExcludedTypeNames { get; set; }
[JsonIgnore]
public ITypeNameGenerator TypeNameGenerator { get; set; }
[JsonIgnore]
public ISchemaNameGenerator SchemaNameGenerator { get; set; }
[JsonIgnore]
public IReflectionService ReflectionService { get; set; }
[JsonIgnore]
public ICollection<ITypeMapper> TypeMappers { get; set; } = new Collection<ITypeMapper>();
[JsonIgnore]
public ICollection<ISchemaProcessor> SchemaProcessors { get; } = new Collection<ISchemaProcessor>();
[JsonIgnore]
[Obsolete("Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.")]
public IContractResolver ContractResolver { get; set; }
[Obsolete("Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.")]
public PropertyNameHandling DefaultPropertyNameHandling { get; set; }
[Obsolete("Use SerializerSettings directly instead. In NSwag.AspNetCore the property is set automatically.")]
public EnumHandling DefaultEnumHandling { get; set; }
[JsonIgnore]
public IContractResolver ActualContractResolver {
get {
if (SerializerSettings != null) {
if (DefaultPropertyNameHandling != 0)
throw new InvalidOperationException("The setting DefaultPropertyNameHandling cannot be used when ContractResolver or SerializerSettings is set.");
if (ContractResolver != null)
throw new InvalidOperationException("The setting ContractResolver cannot be used when SerializerSettings is set.");
return SerializerSettings.ContractResolver;
}
if (ContractResolver != null) {
if (DefaultPropertyNameHandling != 0)
throw new InvalidOperationException("The setting DefaultPropertyNameHandling cannot be used when ContractResolver or SerializerSettings is set.");
return ContractResolver;
}
if (DefaultPropertyNameHandling == PropertyNameHandling.CamelCase)
return new CamelCasePropertyNamesContractResolver();
if (DefaultPropertyNameHandling == PropertyNameHandling.SnakeCase)
return new DefaultContractResolver {
NamingStrategy = new SnakeCaseNamingStrategy()
};
return new DefaultContractResolver();
}
}
[JsonIgnore]
public JsonSerializerSettings ActualSerializerSettings {
get {
if (SerializerSettings != null) {
if (DefaultPropertyNameHandling != 0)
throw new InvalidOperationException("The setting DefaultPropertyNameHandling cannot be used when ContractResolver or SerializerSettings is set.");
if (ContractResolver != null)
throw new InvalidOperationException("The setting ContractResolver cannot be used when SerializerSettings is set.");
if (DefaultEnumHandling != 0)
throw new InvalidOperationException("The setting DefaultEnumHandling cannot be used when SerializerSettings is set.");
return SerializerSettings;
}
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.ContractResolver = ActualContractResolver;
if (DefaultEnumHandling == EnumHandling.String)
jsonSerializerSettings.Converters.Add(new StringEnumConverter());
else if (DefaultEnumHandling == EnumHandling.CamelCaseString) {
jsonSerializerSettings.Converters.Add(new StringEnumConverter(true));
}
return jsonSerializerSettings;
}
}
public JsonSchemaGeneratorSettings()
{
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.Null;
SchemaType = SchemaType.JsonSchema;
DefaultEnumHandling = EnumHandling.Integer;
DefaultPropertyNameHandling = PropertyNameHandling.Default;
ContractResolver = null;
TypeNameGenerator = new DefaultTypeNameGenerator();
SchemaNameGenerator = new DefaultSchemaNameGenerator();
ReflectionService = new DefaultReflectionService();
ExcludedTypeNames = new string[0];
}
public JsonContract ResolveContract(Type type)
{
if (type.GetTypeInfo().IsGenericTypeDefinition)
return null;
return ActualContractResolver.ResolveContract(type);
}
public bool GetActualFlattenInheritanceHierarchy(Type type)
{
if (!FlattenInheritanceHierarchy)
return type.GetTypeInfo().GetCustomAttributes(false).Any((object a) => a.GetType().IsAssignableTo("JsonSchemaFlattenAttribute", TypeNameStyle.Name));
return true;
}
}
}