Configure non-nullable types as required
See original GitHub issueIn my project we do not distinguish between required
and nullable: false
. If a type is non-nullable, it is also required. This is regardless if it is a value type or reference type.
It would be great if one could optionally configure Swashbuckle to set all non-nullable properties to required. E.g. like: setupAction.MarkNonNullableTypesAsRequired();
This way, I could remove all my [Required]
annotations on the non-nullable C# properties.
This is a subsequent request following #1686. As _originally posted by @felschr in https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1686#issuecomment-674074694_ , treating non-nullable as required is the default for nullable contexts in .NET validation.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:50
- Comments:11
Top Results From Across the Web
ASP.NET Core [Require] non-nullable types
Solution working with json requests. You cannot validate an already created model instance, because a non-nullable property has always a ...
Read more >Resolve Non-nullable Property Must Contain a Non-null ...
Initialize non-nullable properties; Change the type to be nullable; Disable the warning; Use the null forgiving operator; Use the required ...
Read more >Working with nullable reference types - EF Core
Collection navigations, which contain references to multiple related entities, should always be non-nullable. An empty collection means that no ...
Read more >Solution - Non-nullable Property Must Contain a Non-null ...
Non -nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Read more >Improving non-nullable reference types handling
Consider declaring as nullable. This rule enforces on us mandatory field initialization to guarantee non-nullable value, which can be satisfied ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
With C# 11.0/.NET 7.0, the
required
keyword is being introduced for properties. Properties with this keyword needs to be initialized through the object initializer when the class/struct/record is initialized from code.But required properties also affects the behavior of System.Text.Json 7.x deserialization and thereby aspnetcore model validation.
The default behavior for System.Text.Json deserialization / aspnetcore model validation in .NET 7.0 is as follows.
Since nullablility and required/not are different things where one does not necessarily imply the other, I think SwasBuckle should use the C# property nullability for swagger property
nullable
, and C# property required/not for the swagger propertyrequired
.If the C# property is annotated with
RequiredAttribute
, I think this should override the above, and set both swagger nullable/required to true.References:
@JosNun I’m using the following workaround:
Result: