Skip to content

Allow empty string identifiers on non-flags enums in JsonStringEnumConverter#128285

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-jsonstringenummembernameattribute
Draft

Allow empty string identifiers on non-flags enums in JsonStringEnumConverter#128285
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-jsonstringenummembernameattribute

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

[JsonStringEnumMemberName("")] was rejected at runtime for all enums, even though the empty-string ambiguity it was guarding against only applies to [Flags] enums (e.g. "y, , n"). This blocked common interop scenarios where an XSD-derived enum has a member whose XML/JSON representation is the empty string.

public enum YesOrNoOrEmpty
{
    [JsonStringEnumMemberName("y")] Yes,
    [JsonStringEnumMemberName("n")] No,
    [JsonStringEnumMemberName("")]  Empty, // previously threw InvalidOperationException
}

Description

  • EnumConverter.ResolveAndValidateJsonName: relaxed the guard so empty strings are permitted on non-flags enums. Flags enums continue to reject empty identifiers (parsing ambiguity) and commas. null and leading/trailing whitespace remain rejected for both — input is Trim()-normalized before lookup, so allowing those would silently break round-tripping.
  • Strings.resx: updated UnsupportedEnumIdentifier to describe the new rule ("It must not be null nor contain leading or trailing whitespace. Flags enums must additionally not be empty nor contain commas.").
  • Tests:
    • EnumWithInvalidMemberName2 (empty identifier) is now marked [Flags] so it continues to validate the remaining rejection path.
    • Added EnumWithEmptyStringMemberName_NonFlags_RoundtripsAsExpected covering serialize/deserialize for "y", "n", and "" against the issue's YesOrNoOrEmpty enum.
    • Added EnumWithEmptyStringMemberName_Flags_Throws with a companion [Flags] YesOrNoOrEmptyFlags enum, asserting that the empty-string identifier is still rejected on both serialize and deserialize for flags enums.

Note

This pull request was created by an AI agent (GitHub Copilot Coding Agent) on behalf of the user.

Copilot AI requested review from Copilot and removed request for Copilot May 16, 2026 13:03
Copilot AI linked an issue May 16, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 16, 2026 13:26
Copilot AI changed the title [WIP] Fix JsonStringEnumMemberNameAttribute to support empty strings Allow empty string identifiers on non-flags enums in JsonStringEnumConverter May 16, 2026
Copilot AI requested a review from eiriktsarpalis May 16, 2026 13:27
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

[JsonStringEnumMemberName("n")]
No,

[JsonStringEnumMemberName("")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot unless it already exists, please add test coverage for a similar flags enum.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added EnumWithEmptyStringMemberName_Flags_Throws with a YesOrNoOrEmptyFlags companion to YesOrNoOrEmpty, asserting that the empty-string identifier is still rejected for [Flags] enums on both serialize and deserialize. Commit 36a53ea.

Copilot AI requested review from Copilot and eiriktsarpalis and removed request for Copilot May 18, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonStringEnumMemberNameAttribute does not support empty strings.

2 participants