-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json.Serialization.JsonStringEnumConverter handles duplicate enum members inconsistently #107296
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
and that the bug only occurs in System.Text, not Newtonsoft dotnet/runtime#107296
(My apologies, i deleted my previous comment as it was pointless due to me not having understood your problem. Sorry about that...) You cannot rely on the order of the enum member in the enum declaration governing which name an enum value is being converted into. As per the documentation for Enum.ToString() (https://learn.microsoft.com/en-us/dotnet/api/system.enum.tostring):
I noticed that in a PR in which you linked to this issue report, you stated that Newtonsoft.Json works. Because here is the kicker: It also works with STJ, for example in dotnetfiddle: http://dotnetfiddle.net/gzXM4o. Given that regarding STJ there are environments where the resolution of duplicate enum members matches your expectations and environments where they don't, it is reasonable to assume the same is true for resolution of duplicate enum members when Newtonsoft.Json is at play. And as it so happens, dotnetfiddle is also an environment where using Newtonsoft.Json - unlike STJ there - won't meet your expectations: http://dotnetfiddle.net/Z3KaCm You are basically right now deep in "It works here but not there" territory if you try relying on undefined behavior like this... |
Thanks @elgonzo ! At least I learned something :) |
Description
When there is an enum and multiple enum members have the same underlying integer value, then the serialization behaviour is not consistent.
In the below examples the members marked as obsolete are there for legacy compatability.
In case a JSON has to be deserialized, it should still be possible to use the obsolete members, but internally I want to use the non-obsolete ones.
Reproduction Steps
Expected behavior
For
MyEnumA
both non-obsolete members occur first:FOO
beforeF_OO
BAR
beforeB_AR
For
MyEnumB
both obsolete members occur first:F_OO
beforeFOO
B_AR
beforeBAR
My expectations are:
EnumMemberA
is serialized asF_OO
, then other properties with typeEnumMemberA
should also useB_AR
instead ofBAR
because there's no reason for different behaviour.EnumMemberA
is serialized asFOO
, then other properties with typeEnumMemberA
should also useBAR
instead ofB_AR
because there's no reason for different behaviour.Actual behavior
The behaviour does not depend on the ordering of the members with same underlying integer but on something else.
The behaviour is not self-consistent.
Please be aware: I'm not asking for either
FOO
orF_OO
to be used by the serializer. It's a problem of my code that it uses two possible serializations for the same integer value. I'm fine with that and both as the serializer cannot guess which one I prefer.But I'm expecting consistency: No
F_OO
when at other placesBAR
occurs for properties of the same type.Regression?
No response
Known Workarounds
I don't know what exactly I'm expecting. But at least the behaviour should be self-consistent.
Configuration
.NET8
Other information
No response
The text was updated successfully, but these errors were encountered: