.NET 8,
Nuget Package JsonSchema.Net.Generation
According to the docs, the [Required]
attribute can be used to make a property ‘required’ when generating a Json Schema from a C# class.
However, for de-serialization using System.Json, one has to use the [JsonRequired]
attribute.
I’m lead to the conclusion that to ensure proper deserialization and schema generation, I need to add both attributes.
Is there a way I can tell the SchemaBuilder to look for just [JsonRequired]
instead?
I tried executing the following code, but the schema did not mark the property as required.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Json.Schema;
using Json.Schema.Generation;
public class Program
{
public static void Main()
{
var config = new SchemaGeneratorConfiguration();
config.Nullability = Json.Schema.Generation.Nullability.AllowForNullableValueTypes;
var schemaBuilder = new JsonSchemaBuilder();
var schema = schemaBuilder.FromType<Test1>(config).Build();
Console.WriteLine(JsonSerializer.Serialize(schema));
}
}
public class Test1
{
[JsonRequired]
public int? Test {get;set;}
public string Test2 {get;set;}
}