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)

Most upvoted comments

I also would like JsonStringEnumMemberName to support empty and null strings.

There’s nothing technically preventing the use of empty strings (even in the case of flags, where you might be able to write something like y, , n). It got added intentionally principally to avoid pathological outputs (like the one mentioned above) and discourage it from being used.

We might consider relaxing that restriction in the future, provided there is sufficient demand for it.

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.Serialization is 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.Serialization supposed to be able to support the serialization and deserialization of any well-formed JSON?

If yes, then it seems like this behavior is incorrect. JsonStringEnumMemberName is 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.

We might consider relaxing that restriction in the future, provided there is sufficient demand for it.

Let’s hope that my 11th 👍 will help reach the demand threshold. 🤞

Is preventing the consumer of the library from producing “pathological outputs” one of the opinionated design goals for the System.Text.Json library?

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.