You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values #58247
Priority:3Work that is nice to havearea-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.bughelp wanted[up-for-grabs] Good issue for external contributorsneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
This by design, number handling only applies to numeric types and not enums (this includes the default enum converter and not just JsonStringEnumConverter).
Out of curiosity, what would be your expected behavior in that snippet? Assuming strict number handling was respected, it would directly contradict what JsonStringEnumConverter is supposed to do (that is, express enums as strings as opposed to their underlying numeric values).
Thank you for your quick response!
I expected that these options will result in the following:
{"Value" : 0} - not valid
{"Value" : "0"} - not valid
{"Value" : "On"} - valid
{"Value" : "Off"} - valid
But the example shows that these options give the following:
{"Value" : 0} - not valid
{"Value" : "0"} - valid
{"Value" : "On"} - valid
{"Value" : "Off"} - valid
I see; while the issue is unrelated to JsonNumberHandling, the allowIntegerValues parameter is not honored when the undefined enum is encoded as a string. Minimal reproduction:
usingSystem;usingSystem.Text.Json;usingSystem.Text.Json.Serialization;publicclassProgram{publicstaticvoidMain(){varoptions=newJsonSerializerOptions{Converters={newJsonStringEnumConverter(allowIntegerValues:false)}};Console.WriteLine(JsonSerializer.Deserialize<MyEnum>("\"42\"",options));// does not failConsole.WriteLine(JsonSerializer.Deserialize<MyEnum>("42",options));// fails as expected}publicenumMyEnum{Value=1}}
I would say we should fix this.
Reacted by DenisAloner, Jacob Boland, Vince Zalamea, Michael Deutsch, davisnw and Shkar T. Noori
changed the title [-]JsonStringEnumConverter ignores JsonNumberHandling.Strict on deserialization[/-][+]JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values[/+]on Aug 27, 2021
Back to the original question, I think allowing integers encoded as strings violates the spirit of this parameter. For example, developers may use JsonStringEnumConverter in their ASP.NET Core projects such their users can specify enumeration names in the request body. This is great because the names are much more description than using numbers. However, I think developers may also assume that they've also decoupled their enum integral values from their web APIs, but this isn't the case. A user could still pass the integral values as strings and successfully bind to the model.
I understand that accepting both the string and integral representations has precedence with use cases like Enum.TryParse, but personally I would like the option to impose more strictness on my JSON parsing. It would be a welcomed addition if this was provided out-of-the-box.
needs-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
The fix is technically a breaking change since disabling integers no longer accepts strings containing integers. The workaround is trivial (just enable integers) but we should still document it.
ghost locked as resolved and limited conversation to collaborators on Nov 5, 2023
Priority:3Work that is nice to havearea-System.Text.Jsonbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.bughelp wanted[up-for-grabs] Good issue for external contributorsneeds-breaking-change-doc-createdBreaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Activity
eiriktsarpalis commentedon Aug 27, 2021
This by design, number handling only applies to numeric types and not enums (this includes the default enum converter and not just JsonStringEnumConverter).
Out of curiosity, what would be your expected behavior in that snippet? Assuming strict number handling was respected, it would directly contradict what
JsonStringEnumConverteris supposed to do (that is, express enums as strings as opposed to their underlying numeric values).DenisAloner commentedon Aug 27, 2021
Thank you for your quick response!
I expected that these options will result in the following:
{"Value" : 0} - not valid
{"Value" : "0"} - not valid
{"Value" : "On"} - valid
{"Value" : "Off"} - valid
But the example shows that these options give the following:
{"Value" : 0} - not valid
{"Value" : "0"} - valid
{"Value" : "On"} - valid
{"Value" : "Off"} - valid
eiriktsarpalis commentedon Aug 27, 2021
I see; while the issue is unrelated to
JsonNumberHandling, theallowIntegerValuesparameter is not honored when the undefined enum is encoded as a string. Minimal reproduction:I would say we should fix this.
[-]JsonStringEnumConverter ignores JsonNumberHandling.Strict on deserialization[/-][+]JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values[/+]wsugarman commentedon Sep 27, 2021
Back to the original question, I think allowing integers encoded as strings violates the spirit of this parameter. For example, developers may use
JsonStringEnumConverterin their ASP.NET Core projects such their users can specify enumeration names in the request body. This is great because the names are much more description than using numbers. However, I think developers may also assume that they've also decoupled their enum integral values from their web APIs, but this isn't the case. A user could still pass the integral values as strings and successfully bind to the model.I understand that accepting both the string and integral representations has precedence with use cases like
Enum.TryParse, but personally I would like the option to impose more strictness on my JSON parsing. It would be a welcomed addition if this was provided out-of-the-box.eiriktsarpalis commentedon Apr 18, 2022
Moving to Future, as we won't have time to work on this in the .NET 7 timeframe.
1 remaining item
davisnw commentedon Dec 20, 2022
As of 2022-12-19, the documentation at https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter.-ctor?view=net-7.0#system-text-json-serialization-jsonstringenumconverter-ctor(system-text-json-jsonnamingpolicy-system-boolean) states:
This clearly implies that you should never receive undefined enum values when parsing with
allowIntegerValues: false.I could see this discrepancy leading to security vulnerabilities in some api usage contexts.
(Note: for some reason the links to file feedback on the documentation via github is not available for that page)
ckarcz commentedon Dec 22, 2022
workaround for now https://stackoverflow.com/a/74890774/929401
eiriktsarpalis commentedon Oct 6, 2023
The fix is technically a breaking change since disabling integers no longer accepts strings containing integers. The workaround is trivial (just enable integers) but we should still document it.