RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

OpenAPI generation ignores validation attributes on constructor parameters #4182

Open thorhj opened 1 year ago

thorhj commented 1 year ago

For positional records, validation attributes are supposed to be applied to the constructor parameters like this:

public record TestRequest([Required] string Foo);

NSwag does not support this syntax. The generated OpenAPI definition for the above request object is not including "foo" as a required property:

"TestRequest": {
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "foo": {
      "type": "string"
    }
  }
}

Note that changing the validation attribute to apply to the property will generate the desired definition:

public record TestRequest([property:Required] string Foo);
"TestRequest": {
  "type": "object",
  "additionalProperties": false,
  "required": [
    "foo"
  ],
  "properties": {
    "foo": {
      "type": "string",
      "minLength": 1
    }
  }
}

But this syntax is not supported in ASP.NET; see this issue.

CommanderAlchemy commented 11 months ago

This seems to still be an issue in 13.20.0 To that [property:Required] stopped working, not sure if its .net change or nswag change.

However [property:BindRequired] works if you are okay with the side effects.