Fail on missing required non-nullable value type in request body #1254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This proof-of-concept PR fails deserialization of an incoming request body at a POST resource endpoint when the value is omitted for a non-nullable value type property decorated with
[Attr] [Required]
.This works around the limitation in ASP.NET ModelState validation described at https://www.jsonapi.net/usage/resources/nullability.html#value-types.
However, I wonder if we should actually take this approach. It feels like a dirty hack that mixes model binding with validation, which are distinct steps in ASP.NET. Also, it prevents any other ModelState violations from being returned in the response. A better approach would be to still detect it during deserialization, but instead of producing an error, somehow make it result in a ModelState validation error via request-scoped shared state. This would only work properly when ModelState validation is turned on. That approach would potentially apply to various other errors that currently cause a deserialization failure, such as "missing ID", "property is read-only" etc.
And this is just one approach to dealing with the situation. Alternatives:
QUALITY CHECKLIST