Generate Custom JSON Schema using JsonSchema.Net.Generation

29 Views Asked by At

We are using JsonSchema.Net.Generation (C#, dotNet 5.0) and I would like generate a custom JSON schema based on the properties and its attributes of a class which has category attribute set. How do I achieve this?

Example:

[Json.Schema.Generation.Title("Custom JSON Schema generation.")]
class School
{
    [DisplayName("Student")]
    [Category("Student")]
    [Json.Schema.Generation.Description("School Schema generation test")]
    [Const(true)]
    public string StudentName { get; set; } = "XYZ";

    [Editable(true)]
    [Range(0, 1000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [Category("Student")]
    public int Id { get; set; } = 10;

    [Editable(false)]
    [DisplayName("ParentName")]
    [Display(Name ="PPhone", Description ="Testing", GroupName = "Student")]
    [Category("Student")]
    public string ParentName { get; set; }


    [DisplayName("ParentPhone")]
    [Category("Student")]
    public string ParentPhone{ get; set; }

    [Editable(true)]
    [Category("Staff")]
    public string TeacherName { get; set; }

    [Editable(true)]
    [Range(0, 10000, ErrorMessage = "Value for {0} must be between {1} and {2}.")]
    [DefaultValue(5000)]
    [Category("Staff")]
    public int Salary { get; set; }

    [Json.Schema.Generation.ReadOnly(true)]
    [Category("Student")]
    public bool AvailingTransport { get; set; } = true;

    [Json.Schema.Generation.ReadOnly(true)]
    [Category("Common")]
    public string Address { get; set; }

    [Json.Schema.Generation.ReadOnly(true)]
    [Category("Common")]
    public string MobileNumber { get; set; }
}

From the class above, I get a schema like:

{
"type":"object",
"properties":{
    "StudentName":{
        "type":"string",
        "description":"School Schema generation test",
        "const":true    },
        "Id":{
        "type":"integer"
    },
    "ParentName":{
        "type":"string"
    },
    "ParentPhone":{
        "type":"string"
    },
    "TeacherName":{
        "type":"string"
    },
    "Salary":{
        "type":"integer"
    },
    "AvailingTransport":{
        "type":"boolean",
        "readOnly":true
    },
    "Address":{
        "$ref":"#/$defs/string"
    },
    "MobileNumber":{
        "$ref":"#/$defs/string"}
    },
    "title":"Custom JSON Schema generation.",
    "$defs":{
        "string":{
        "type":"string",
        "readOnly":true
        }
    },
    "additionalProperties":false
}

But, I would like to generate JSON schema as below:

{
"type":"object",
"properties":{
    "Student":{
        "StudentName":{
            "type":"string",
            "description":"School Schema generation test",
            "const":true    },
            "Id":{
            "type":"integer"
        },
        "ParentName":{
            "type":"string"
        },
        "ParentPhone":{
            "type":"string"
        },
        "AvailingTransport":{
            "type":"boolean",
            "readOnly":true
        },
    },
    "Staff":{
        "TeacherName":{
            "type":"string"
        },
        "Salary":{
            "type":"integer"
        },
    },
    "Address":{
        "$ref":"#/$defs/string"
    },
    "MobileNumber":{
        "$ref":"#/$defs/string"}
    },
    "title":"Custom JSON Schema generation.",
    "$defs":{
        "string":{
        "type":"string",
        "readOnly":true
        }
    },
    "additionalProperties":false
}

I have generated the schema using the below code using JsonSchema:

Json.Schema.JsonSchema schema = new JsonSchemaBuilder().FromType<School>().AdditionalProperties(false).Build();
Console.WriteLine(JsonSerializer.Serialize(schema));

I believe during serialization, we need to modify to categorize the schema to have separate nodes for "Student" and "Staff" and the rest of the properties which are common to both to be in the main itself.

0

There are 0 best solutions below