-
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 parses numbers when the token is a String even when allowIntegerValues is false #1301
Comments
Could you please update into a standalone example, such as can be pasted into a console app? The above code is incomplete. |
Update: I see I missed pasting in the |
The gist is that I expected the deserialization into B to fail like it does in the try/catch block because 100 is not a defined enum value but because the Enum.Parse() method can convert it to an integer (I'm guessing that's what's going on anyway) the value is deserialized and results in an undefined enum value. |
cc @JeremyKuhne |
That said, I didn't really internalize that If I could do it again I would have this mean:
I don't know how tenable that sort of change (particularly bullet 2) is now given that anyone parsing enums as string numeric values would now be broken. |
Personally I think the ENum parsing is incorrect and have had an extension method since extension methods were introduced. That shipped sailed long ago. If you wanted to introduce this behavior you would need (sigh) another setting like "TreatIntegerStringsAsIntegers" with a default value of false. Anyone could opt-in to the new behavior if they wanted it. I'd like to see an option that just says "allowDefinedEnumsOnly" and apply the parsing behavior to ENum type fields only. I realize you have to consider a bunch more things. |
@DonFrazier Is there anything else you think is missing? |
For enums with the [Flags] Attribute you must allow parsing any combination of defined flags. [Flags] |
Closing as duplicate of #58247. |
It would be nice if the
allowIntegerValues
parameter recognized strings that are numbers and failed the conversion. Parsing any numeric into an enum can be hard to track down as a data problem and closing this loophole would help identify data errors closer to the their source.The text was updated successfully, but these errors were encountered: