RicoSuter / NJsonSchema

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

C# Record classes don't generate optional constructor parameters for non-required properties #1664

Open adamjones2 opened 8 months ago

adamjones2 commented 8 months ago

Eg. a request body schema of:

{
  "required": [ "inputDate" ],
  "type": "object",
  "properties": {
    "inputDate": { "type": "string", "format": "date" },
    "ids": {
      "type": "array",
      "items": { "type": "string" },
      "nullable": true
    }
  },
  "additionalProperties": false
}

will render, if classStyle is set to Record and generateNullableReferenceTypes is true, a constructor like:

public MyType(DateOnly inputDate, string[]? ids)
{
    ...
}

The ids parameter is correct to be nullable, but it should not be required for the caller to provide the value given that it's marked as non-required in the spec. The parameter should be string[]? ids = null.