-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JsonStringEnumConverter always returns a default value #100852
Copy link
Copy link
Closed
Labels
Description
Description
When deserialising JSON containing a string that needs to be converted to an enum via JsonStringEnumConverter, the converter always deserialises with the first value of the enum.
Reproduction Steps
[Test]
public void JsonEnumStringConverterTest() {
const string json =
"""
{
"foo": 0,
"bar": "secondvalue"
}
""";
var opts = new JsonSerializerOptions();
opts.Converters.Add(new JsonStringEnumConverter());
var value = JsonSerializer.Deserialize<SomeType>(json, opts);
value.Bar.Should().Be(Enum.Parse<SomeTypeEnum>("secondvalue", ignoreCase: true));
}
public record SomeType(int Foo, SomeTypeEnum Bar);
public enum SomeTypeEnum {
FirstValue,
SecondValue
}
Expected behavior
Bar should be SecondValue
Actual behavior
Bar is FirstValue
Regression?
No response
Known Workarounds
No response
Configuration
8.0.201
MacOS 14.2.1 (23C71)
Other information
No response
Reactions are currently unavailable