-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonSerializer does not require that value of an enum is defined #35900
Comments
Tagging subscribers to this area: @safern, @ViktorHofer |
Tagging subscribers to this area: @jozkee |
@jasonycw neither of the two serializers (S.T.Json \ Newtonsoft.Json) that MVC supports enforce that an enum value is defined. MVC's model binding does which is why you see a validation error appear for model bound values. The Moving the issue to the runtime repo in the event they want to consider enforcing this using a converter. |
As mentioned, there's a pattern for validating enum values in MVC using |
Although this issue is older, I'll close it as a dup of #42093 which has more discussion. |
Consider the following:
The serializer does not complain when an out-of-range enum value is presented.
Original text
Let's say we have the following as our request model
In the controller, if we have
and we
POST /test?currency=3
, we will have an invalid model state.If we have
and we
POST /test/3
, we will also have an invalid model state.However, if we have
and we
POST /test
with a JSON bodyThe model state will be valid and the controller will get
request.Currency = 3
which does not make sence.If we add and attribute to the enum property like the following
(thanks to this very old StackOverflow answer)
And
POST /test
with the same JSON body again, we can get invalid model state, which is expected behavior.Further technical details
dotnet --info
:The text was updated successfully, but these errors were encountered: