Stewart
Stewart

Reputation: 153

How to get a JSON.NET JSchema generator to render the additionalProperties attribute in a JSON schema

I am using JSON.NET JSchema Generator to create schemas based on classes decorated with Data Annotation Attributes. I'm using the generator like this:

var generator = new JSchemaGenerator();
generator.ContractResolver = new CamelCasePropertyNamesContractResolver();
generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
var schema = generator.Generate(typeof(myType));
string jsonSchema = schema.ToString();

This generates an example schema like:

{
     "$id": "myType",
     "definitions": {
          "mySubType" : {
              "$id": "mySubType",
              "type": [
                   "object",
                   "null"
              ],
              "properties": {
                  "name": {
                     "type: "string"
                  }
              },
              "required": [
                  "name"
              ]
          }
     },
     "type": "object",
     "properties": {
         "name": {
            "type": "string"
         },
         "details": {
            "$ref": "mySubType"
         }
      },
      "required": [
          "name",
          "details"
      ]
}

I want to be able to generate a schema that includes the additional properties attribute for both myType and mySubType, like this:

{
     "$id": "myType",
     "definitions": {
          "mySubType" : {
              "$id": "mySubType",
              "type": [
                   "object",
                   "null"
              ],
              "properties": {
                  "name": {
                     "type: "string"
                  }
              },
              "required": [
                  "name"
              ],
              "additionalProperties": false
          }
     },
     "type": "object",
     "properties": {
         "name": {
            "type": "string"
         },
         "details": {
            "$ref": "mySubClass"
         }
      },
      "required": [
          "name",
          "details"
      ],
      "additionalProperties": false
}

How can I generate a schema like this using a JSchema generator?

Is there a class level data annotation attribute that does this?

Upvotes: 5

Views: 1868

Answers (2)

seagulledge
seagulledge

Reputation: 344

My Json schema has nested arrays and objects. Adding on to the answer that @semera gave:

    static void RejectAdditionalProperties(JSchema schema)
    {
        if(schema.Type == JSchemaType.Object)
        {
            schema.AllowAdditionalProperties = false;
            foreach (var v in schema.Properties.Values) RejectAdditionalProperties(v);
        }

        if(schema.Type == JSchemaType.Array) 
        { 
            foreach (var i in schema.Items) RejectAdditionalProperties(i);
        }
    }

Upvotes: 0

semera
semera

Reputation: 41

A little bit late, but I struggled with that today..

void Main()
{
    var generator = new JSchemaGenerator();
    generator.ContractResolver = new CamelCasePropertyNamesContractResolver();
    generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
    var schema = generator.Generate(typeof(myType));
    RejectAdditionalProperties(schema);
    string jsonSchema = schema.ToString();
}

static void RejectAdditionalProperties(JSchema schema)
{
    schema.AllowAdditionalProperties = false;
    foreach(var s in schema.Properties.Values) RejectAdditionalProperties(s);
}

Upvotes: 4

Related Questions