runtime: JsonStringEnumMemberNameAttribute does not support empty strings.
Description
For my use case I have the following third party XSD (simplified) for which I generate an enum:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="YesOrNoOrEmpty">
<xs:restriction base="xs:string">
<xs:enumeration value="y" />
<xs:enumeration value="n" />
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
I want the generated enum to be serializable in both XML and JSON, so I annotate the members with both XmlEnumAttribute as well as the new JsonStringEnumMemberNameAttribute from System.Text.Json v9-preview.
For XML this works fine, but with JSON I get an exception, because empty strings are not allowed. I assume this might have something to do with support for Flags enums? But for enums without FlagsAttribute this restriction should not apply.
Reproduction Steps
using System.Text.Json.Serialization;
using System.Xml.Serialization;
public enum YesOrNoOrEmpty
{
[JsonStringEnumMemberName("y")]
[XmlEnum("y")]
Yes,
[JsonStringEnumMemberName("n")]
[XmlEnum("n")]
No,
[JsonStringEnumMemberName("")]
[XmlEnum("")]
Empty,
}
Expected behavior
JsonStringEnumConverter is able to serialize and deserialize enums where the string representation of one of the enum members is an empty string. This matches behaviour of System.Xml’s XmlEnumAttribute, and EnumMemberAttribute which is used by Newtonsoft.Json.
Actual behavior
JsonStringEnumConverter (using EnumConverter internally) does a string.IsNullOrEmpty check and throws an InvalidOperationException: Enum type '{0}' uses unsupported identifier '{1}'. It must not be null, empty, or containing leading or trailing whitespace. Flags enums must additionally not contain commas.
Regression?
No response
Known Workarounds
Implementing a custom JsonConverter for enums
Configuration
No response
Other information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 15
- Comments: 15 (7 by maintainers)
I also would like
JsonStringEnumMemberNameto support empty and null strings.This feels like a bug to me, or at least a defect in the requirements specification, not an enhancement.
In a perfect world, I understand the desire to discourage cases like the one you outlined. But one of the most common use cases for
System.Text.Json.Serializationis writing .NET applications and libraries that interact with restful APIs. Typically, the developer or organization responsible for this task has no control over the JSON schema defined by the API, or it cannot be easily changed.Isn’t
System.Text.Json.Serializationsupposed to be able to support the serialization and deserialization of any well-formed JSON?If yes, then it seems like this behavior is incorrect.
JsonStringEnumMemberNameis imposing an opinionated limitation on serializing and deserializing well-formed JSON with no technical basis or rationale.The Docker Engine API has many empty strings which naturally map onto enums.
For example, the SystemInfo Isolation is documented as Enum:
"default""hyperv""process"""where the empty string is listed as a legitimate value.Let’s hope that my 11th 👍 will help reach the demand threshold. 🤞
Yes. There are a number of things that the library explicitly does not permit out of the box in the interest of performance, security, promoting good practices, and adhering to the principle of least surprise. For example, it doesn’t allow unconstrained polymorphism nor does it do implicit type coercion. The reason why whitespace isn’t allowed in enum identifiers is because of the ambiguity that it introduces in the context of flag enum serialization.
That does not mean they’re not possible to achieve using customization, and we make sure that the library exposes all the necessary extensibility points in the likely event that the built-in features don’t support a specific scenario.