-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
JsonStringEnumConverter ignores 'allowIntegerValues' when deserializing quoted numeric values #58247
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue Details.NET 5
|
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 |
Thank you for your quick response! |
I see; while the issue is unrelated to using System;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
var options = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter(allowIntegerValues: false) } };
Console.WriteLine(JsonSerializer.Deserialize<MyEnum>("\"42\"", options)); // does not fail
Console.WriteLine(JsonSerializer.Deserialize<MyEnum>("42", options)); // fails as expected
}
public enum MyEnum
{
Value = 1
}
} I would say we should fix this. |
Back to the original question, I think allowing integers encoded as strings violates the spirit of this parameter. For example, developers may use I understand that accepting both the string and integral representations has precedence with use cases like |
Moving to Future, as we won't have time to work on this in the .NET 7 timeframe. |
Up for grabs. I believe the fix here would be to enforce that only named constants are permitted when reading enum values as strings and The relevant code would need to be added to this method. |
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 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) |
workaround for now https://stackoverflow.com/a/74890774/929401 |
Added
Tagging @dotnet/compat for awareness of the breaking change. |
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. |
.NET 5
JsonStringEnumConverter ignores JsonNumberHandling.Strict on deserialization.
Example: https://dotnetfiddle.net/IsP6Dn
The text was updated successfully, but these errors were encountered: