Skip to content

Allow empty string identifiers on non-flags enums in JsonStringEnumConverter and disallow non-flags comma-splitting#128285

Open
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-jsonstringenummembernameattribute
Open

Allow empty string identifiers on non-flags enums in JsonStringEnumConverter and disallow non-flags comma-splitting#128285
Copilot wants to merge 4 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 was intended for [Flags] enums. 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.
  • EnumConverter.TryParseNamedEnum: non-flags enums now parse named values as a single identifier (no comma tokenization). Comma-separated inputs remain supported only for [Flags] enums, removing cases where inputs like "y, , n" or "," could previously match an empty-name member.
  • 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 YesOrNoOrEmpty.
    • Added EnumWithEmptyStringMemberName_NonFlags_DoesNotParseCommaSeparatedValues to assert non-flags rejection of comma-separated inputs ("y, , n", ",", "y,n").
    • 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
…nverter

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/b77c051c-b5f7-4943-9c20-6e4fe34c3b37

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
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.

Copilot AI requested review from Copilot and eiriktsarpalis and removed request for Copilot May 18, 2026 13:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR relaxes JsonStringEnumConverter validation so empty custom enum identifiers can be used on non-flags enums while preserving rejection for flags enums where comma-separated parsing can be ambiguous.

Changes:

  • Updates enum identifier validation to allow "" only for non-flags enums.
  • Revises the unsupported identifier resource string.
  • Adds regression coverage for non-flags empty names and flags rejection.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs Adjusts enum name validation rules for empty identifiers.
src/libraries/System.Text.Json/src/Resources/Strings.resx Updates the validation error message.
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/EnumConverterTests.cs Adds and updates enum converter tests for empty identifiers.

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/b5919880-8024-4aea-b0d6-2bdb16133c1d

Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot AI changed the title Allow empty string identifiers on non-flags enums in JsonStringEnumConverter Allow empty string identifiers on non-flags enums in JsonStringEnumConverter and disallow non-flags comma-splitting May 18, 2026
Copilot AI requested a review from eiriktsarpalis May 18, 2026 17:25
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.

3 participants