-
-
Notifications
You must be signed in to change notification settings - Fork 535
JsonSchemaGenerator
- Package: NJsonSchema
- Settings: JsonSchemaGeneratorSettings
The JsonSchemaGenerator
creates a JsonSchema (or Swagger/OpenAPI model schema) from a given C# type via reflection and the Newtonsoft.Json contract resolver:
var settings = new JsonSchemaGeneratorSettings();
var generator = new JsonSchemaGenerator(settings);
var schema = await generator.GenerateAsync<Person>();
The JsonSchema.FromTypeAsync()
method is a shortcut to use the JsonSchemaGenerator
:
var schema = await JsonSchema.FromTypeAsync<Person>();
To generate multiple types into the same schema, you have to reuse the schema resolver:
var settings = new JsonSchemaGeneratorSettings();
var schema = new JsonSchema(); // the schema to write into
var resolver = new JsonSchemaResolver(schema, settings); // used to add and retrieve schemas from the 'definitions'
var generator = new JsonSchemaGenerator(settings);
await generator.GenerateAsync(typeof(MyRootType), null, schema, resolver); // generate root schema
await generator.GenerateAsync(typeof(MyAdditionalType), resolver); // will be added to schema.Definitions
- JsonSchemaAttribute
- JsonSchemaTypeAttribute: Overrides the type of a class, property or parameter (Swagger/OpenAPI) with another type.
- JsonSchemaProcessorAttribute: Defines the schema processor for a given class.
- JsonSchemaExtensionDataAttribute
- JsonSchemaIgnoreAttribute: Ignores a class from being generated by the JSON Schema generator.
- MultipleOfAttribute
- CanBeNullAttribute
- NotNullAttribute
-
JsonSchemaFlattenAttribute: Same as the
FlattenInheritanceHierachry
setting but for a single class.
Newtonsoft.Json.JsonPropertyAttribute
System.Runtime.Serialization.DataMemberAttribute
One of the attributes define the name of the property (JsonPropertyAttribute
wins).
-
System.ComponentModel.DataAnnotations.DisplayAttribute
(Name
property)
The Name
of the DisplayAttribute
attribute is used as property title
.
System.ComponentModel.DescriptionAttribute
-
System.ComponentModel.DataAnnotations.DisplayAttribute
(Description
property) - C# XML Documentation
Sets the description
field of the property. Wins over an existing XML documentation for the property.
System.ComponentModel.DataAnnotations.RequiredAttribute
Adds the property to the list of required properties and defines the property as not nullable.
Newtonsoft.Json.JsonPropertyAttribute
Additionally the NullValueHandling
property of the JsonPropertyAttribute
defines whether the property is required (AllowNull
or Always
) or allows null (AllowNull
).
System.ComponentModel.DataAnnotations.RegularExpressionAttribute
Sets the pattern
regex validation field for the property.
System.ComponentModel.DataAnnotations.RangeAttribute
Sets the minimum
and maximum
validation fields of the property.
System.ComponentModel.DataAnnotations.MinLengthAttribute
System.ComponentModel.DataAnnotations.MaxLengthAttribute
The property must be a string.
The array minItems and maxItems values
System.ComponentModel.DataAnnotations.MinLengthAttribute
System.ComponentModel.DataAnnotations.MaxLengthAttribute
The property must be an array.
The default value of a propery can be defined using the DefaultValueAttribute
. This sets the default
property of the JSON Schema and is then used in the generated DTOs while initializing the objects.
System.ComponentModel.DataAnnotations.DefaultValueAttribute
System.Runtime.Serialization.KnownTypeAttribute
It is also possible to define a static method which returns known types (see PR #292)
Also see Inheritance.
JSON Schema supports integer and string enumerations. In Json.NET, enums are serialized as integer by default. However you can mark a property with the JsonConverterAttribute
so that it is serialized as string. NJsonSchema looks for these attributes and generates the enum JSON Schema respecting this attribute:
// Only this property is serialized as string
[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }
// Always serialized as string
[JsonConverter(typeof(StringEnumConverter))]
public enum Gender
{
...
}
Also see Enums
System.ComponentModel.ReadOnlyAttribute
Sets the IsReadOnly
property on the [JsonSchema] class and serializes to the readonly
JSON Schema property (not serialized when false
).
NotNullAttribute and CanBeNullAttribute
Controls if a property can be null or is not nullable, the default behavior can be changed with the DefaultReferenceTypeNullHandling
setting (default: Null).
System.Object, Newtonsoft.Json.Linq.JObject and ExpandoObject
Handled as "any" type (JSON type 'Object' and AllowAdditionalProperties = true
), results in a dictionary.
Newtonsoft.Json.Linq.JArray
Handled as "any" array type
System.Exception
Only the properties "InnerException", "Message", "Source", "StackTrace" are generated.
System.Type
Handled as "string" (default behavior of Json.NET).
Use the JsonSchemaAttribute
to fully change the type
and format
attributes of the JSON Schema for a C# class:
[JsonSchema(JsonObjectType.String, Format = "point")]
public class Point
{
public decimal X { get; set; }
public decimal Y { get; set; }
}
public class AnnotationClass
{
public Point Point { get; set; }
}
(You should also implement a custom class serializer for the Point
class to reflect the type and format)
The JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"typeName": "AnnotationClass",
"additionalProperties": false,
"type": "object",
"properties": {
"Point": {
"type": [
"null",
"string"
],
"format": "point"
}
}
}
A property is ignored when either
- The property is marked with the
JsonIgnoreAttribute
property - The class has a
DataContractAttribute
attribute and the property has noDataMemberAttribute
and noJsonPropertyAttribute