As you can see in the code example below, I'm trying to generate a json schema for the MotionConfigSchema class. It contains 2 fields (Yaw and Roll) that are of type Channel. I want to add a Description attribute on those 2 fields (Yaw and Roll) but the issue I have is that the Description attribute cause the schema generator to create 2 definitions for the Channel class.
The easiest to understand is to look at the example here : https://dotnetfiddle.net/u3IsXC
Desired Result
I don't want to have 2 definitions ("Channel" and "Channel-1") in the resulting schema, I want the Description attribute on the Yaw and Roll field to appear in the Yaw et Roll properties of the json generated schema.
Code sample
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
using System.ComponentModel;
using System;
public class Program
{
public static void Main()
{
JSchemaGenerator generator = new JSchemaGenerator();
//generator.SchemaReferenceHandling = SchemaReferenceHandling.None;
generator.DefaultRequired = Required.DisallowNull;
JSchema schema = generator.Generate(typeof(MotionConfigSchema));
Console.WriteLine(schema.ToString());
}
}
public class Channel
{
public string DataPath { get; private set; }
public float Gain { get; private set; } = 1.0f;
}
[Description("Motion Configuration file fields description.")]
public class MotionConfigSchema
{
[Description("Yaw in °/sec")]
public Channel? Yaw {get; set;}
[Description("Roll in °")]
public Channel? Roll {get; set;}
}
Which lead to the following generated json schema :
{
"description": "Motion Configuration file fields description.",
"definitions": {
"Channel": {
"description": "Yaw in °/sec",
"type": "object",
"properties": {
"DataPath": {
"type": "string"
},
"Gain": {
"type": "number"
}
}
},
"Channel-1": {
"description": "Roll in °",
"type": "object",
"properties": {
"DataPath": {
"type": "string"
},
"Gain": {
"type": "number"
}
}
}
},
"type": "object",
"properties": {
"Yaw": {
"$ref": "#/definitions/Channel"
},
"Roll": {
"$ref": "#/definitions/Channel-1"
}
}
}