I'm using a JsonSchemaGenerator
from JSON.NET
against a series of models to output the respective JSON schemas into a dictionary like below.
JsonSchemaGenerator generator = new JsonSchemaGenerator()
{
UndefinedSchemaIdHandling = UndefinedSchemaIdHandling.UseTypeName,
};
List<Type> modelTypes = Assembly.GetExecutingAssembly()
.GetTypes()
.ToList()
.Where(t => t.Namespace == "MyApp.Models")
.ToList();
foreach (Type type in modelTypes)
{
JsonSchema schema = generator.Generate(type, jsonSchemaResolver, false);
schemaDictionary.Add(type, schema);
}
This is working properly with the exception of the vales set for the required
attribute. No matter how I decorate the model properties, the fields always are shown to be "required":true
as shown below:
"FirstName": {
"required": true,
"type": "string"
}
However, in code my model is decorated as such:
[JsonProperty(Required = Required.Default)]
public string FirstName { get; set; }
Looking at the Json.Net documentation, setting to Required.Default
should result in the property not being required in the schema:
"Default - 0 - The property is not required. The default state."
Any idea on what I'm doing incorrectly and need to change so that FirstName
property is output in the schema as "required": false
? I do not want to have to generate and hand massage all of these schemas.