RicoSuter / NJsonSchema

JSON Schema reader, generator and validator for .NET
http://NJsonSchema.org
MIT License
1.37k stars 529 forks source link

AllowAdditionalProperties - not working #1689

Open careless6666 opened 4 months ago

careless6666 commented 4 months ago

i have a sample model

public class RootConfig
{
    /// <summary>
    /// Configure s2s options
    /// </summary>
    public S2SOptions S2SAuth { get; set; }    
    }

and code for generation

public class Program
{
    public static void Main(string[] args)
    {
        // var generator = new JsonSchemaGenerator(new SystemTextJsonSchemaGeneratorSettings());
        // var schema = generator.Generate(typeof(S2SOptions));
        // Console.WriteLine(schema.ToJson());

        var settings = new SystemTextJsonSchemaGeneratorSettings()
        {

        };
        var schema = new JsonSchema
        {
            AllowAdditionalProperties = true
        }; 
        var resolver = new JsonSchemaResolver(schema, settings); // used to add and retrieve schemas from the 'definitions'
        var generator = new JsonSchemaGenerator(settings);

        generator.Generate(schema, typeof(RootConfig), resolver); // generate root schema

        Console.WriteLine(schema.ToJson());
        File.WriteAllText("platformSchema.json", schema.ToJson());
    }
}

It is generate json schema with additionalProperties:false on root, but i expected that it will be true

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "RootConfig",
  "type": "object",
  "description": "Common class with contains all json configurable options",
  "additionalProperties": false,
  "properties": {
    "S2SAuth": {
      "description": "Configure s2s options",
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/definitions/S2SOptions"
        }
      ]
    },
...

How set up true for additionalProperties on top level?