Are there any plans to add support for Nullable Reference Types? It would be nice, and with NRT we would no longer need to annotate required properties with the [Required] attribute.
I am also looking for a way how to set all non-nullable properties in schemas required by default. Is there any other way except manually adding Required attribute everywhere?
Creating a pull request ;)
@alesdvorakcz
I agree, it would be nice if Swashbuckle behaves the same as the ASP.NET Core validation system:
The validation system in .NET Core 3.0 and later treats non-nullable parameters or bound properties as if they had a [Required] attribute.
— https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-3.1#required-attribute
Waiting for the same. With Nullable feature enabled it's still required to add Required attribute.
@domaindrivendev I would be willing to give it a try if you think you would accept a PR for it.
Presumably there would be a need to "opt-out" like there is with the validation in aspnetcore.
Also, i expect the Nullable type detection would more or less be equivalent to what is done in the asp.net core repo:
https://github.com/dotnet/aspnetcore/blob/0517745c08d1bc85009f1ca41a41d77176eeea60/src/Mvc/Mvc.DataAnnotations/src/DataAnnotationsMetadataProvider.cs#L357
Which is not exactly pretty - but I doubt I can come up with something better.
Would that approach be acceptable?
@esbenbach - sounds reasonable. I'll keep an eye out for your PR and will try my best to provide prompt feedback
Any news here?
Unfortunately no - due to Covid19, lots of stuff has prevented me from looking into this, and the timeframe is pretty grim. Someone else should feel free to give it a try :)
... with NRT we would no longer need to annotate required properties with the [Required] attribute
@Compusa , were you able to remove [Required] attributes from your properties?
When I add setupAction.SupportNonNullableReferenceTypes(); it correctly removes "nullable": true from the swagger property, but it is still not listed as _required_.
Do you still need to mark all properties with [Required]? Is there another feature request issue to also make them required by default?
... with NRT we would no longer need to annotate required properties with the [Required] attribute
@Compusa , were you able to remove
[Required]attributes from your properties?When I add
setupAction.SupportNonNullableReferenceTypes();it correctly removes"nullable": truefrom the swagger property, but it is still not listed as _required_.Do you still need to mark all properties with
[Required]? Is there another feature request issue to also make them required by default?
I haven't been able to test this yet unfortunately.
I just tested this out and it looks like my fields are correctly marked as nullable or not nullable. Non-nullable fields are not marked as required unless I annotate with [Required].
I currently believe this should be the correct behaviour as nullable and required in the JSON schema standard are two different things. The first is basically a promise that says that if a key is present in a JSON document, the value will or will not be null. The second one is basically a promise that it will be in the JSON document.
@hannahchan , thank you for verifying that this issue only affects nullable.
I have posted a new issue https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036 regarding optionally treating all non-nullable types as required.
Most helpful comment
@domaindrivendev I would be willing to give it a try if you think you would accept a PR for it.
Presumably there would be a need to "opt-out" like there is with the validation in aspnetcore.
Also, i expect the Nullable type detection would more or less be equivalent to what is done in the asp.net core repo:
https://github.com/dotnet/aspnetcore/blob/0517745c08d1bc85009f1ca41a41d77176eeea60/src/Mvc/Mvc.DataAnnotations/src/DataAnnotationsMetadataProvider.cs#L357
Which is not exactly pretty - but I doubt I can come up with something better.
Would that approach be acceptable?