Model validation doesn't work well for properties with the required
keyword
#61511
Labels
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
feature-validation
Issues related to model validation in minimal and controller-based APIs
Consider this code:
If I send a request to this endpoint with no value for the
Bar
property, I get the following response, as expected:But if I change
Foo
to use therequired
keyword instead of the[Required]
attribute:I get this response instead:
This isn't very helpful.
First, the name
foo
means nothing to the client, because it's just the name of the parameter in the controller action. It's not exposed in the OpenApi documentation or anywhere else. To the client, it's just the request body, it doesn't have a name.Second, the response contains no error with
bar
as the key to indicate that there's an error for this field.Third, it's very surprising that the
required
keyword doesn't have the same effect as the[Required]
attribute. I think I understand the reason (System.Text.Json takes therequired
keyword into account when deserializing, so if the property is missing, it just throws a deserialization exception, so there's never a chance to actually validate the deserialized model), but it's not a great experience.The workaround is easy (don't use the
required
keyword, use the[Required]
attribute instead), but it's not great, because now I have a nullability warning on my property, and I need to initialize it with a dummy value to fix it. I like the keyword because it's a clean way to suppress the warning for non-nullable properties, but it completely breaks this scenario.I'm not sure if it's actually a bug, or just a known limitation, but it's annoying, and it would be nice to fix it. Is there maybe a way to make the JSON serializer ignore the
required
keyword, so that model validation can do its work? (I couldn't find it).Sorry if that issue has already been reported. I searched for it but couldn't find anything, which I found surprising, because I'm probably not the first one to notice this.
The text was updated successfully, but these errors were encountered: