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
Open
Conversation
…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
changed the title
[WIP] Fix JsonStringEnumMemberNameAttribute to support empty strings
Allow empty string identifiers on non-flags enums in JsonStringEnumConverter
May 16, 2026
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e6c2eed7-d610-4d2a-ba79-80c4760a113d Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot
AI
requested review from
Copilot and
eiriktsarpalis
and removed request for
Copilot
May 18, 2026 13:35
eiriktsarpalis
approved these changes
May 18, 2026
Contributor
There was a problem hiding this comment.
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
This was referenced May 18, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[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.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.nulland leading/trailing whitespace remain rejected for both — input isTrim()-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: updatedUnsupportedEnumIdentifierto 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.").EnumWithInvalidMemberName2(empty identifier) is now marked[Flags]so it continues to validate the remaining rejection path.EnumWithEmptyStringMemberName_NonFlags_RoundtripsAsExpectedcovering serialize/deserialize for"y","n", and""againstYesOrNoOrEmpty.EnumWithEmptyStringMemberName_NonFlags_DoesNotParseCommaSeparatedValuesto assert non-flags rejection of comma-separated inputs ("y, , n",",","y,n").EnumWithEmptyStringMemberName_Flags_Throwswith a companion[Flags]YesOrNoOrEmptyFlagsenum, 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.