Skip to content

[STJ] Fix formatting of flag enums when values are overlapping. #117730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2025

Conversation

eiriktsarpalis
Copy link
Member

Fix #114770.

Copy link
Contributor

@Copilot 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 fixes the formatting of flag enums when values are overlapping by implementing a topological sort to ensure proper ordering of enum values during serialization. The issue occurs when flag enums have values that are supersets of other values (e.g., BITS01 = 3 which combines BIT0 = 1 and BIT1 = 2).

Key Changes:

  • Added topological sorting for flag enums to ensure superset values are processed before their constituent parts
  • Implemented Kahn's algorithm to handle the ordering dependencies between overlapping enum values
  • Added comprehensive tests to verify the fix works for different scenarios of overlapping flag enum values

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
EnumConverter.cs Implements topological sorting logic using Kahn's algorithm for flag enums to ensure proper ordering of overlapping values
EnumConverterTests.cs Adds test cases for flag enums with overlapping bit values to verify serialization produces expected output

Copy link
Contributor

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

@eiriktsarpalis
Copy link
Member Author

/ba-g test failures unrelated to change.

@eiriktsarpalis eiriktsarpalis merged commit d55770b into dotnet:main Jul 18, 2025
82 of 87 checks passed
@eiriktsarpalis eiriktsarpalis deleted the fix/stj-enum-parsing branch July 18, 2025 10:38
@eiriktsarpalis
Copy link
Member Author

/backport to release/9.0-staging

Copy link
Contributor

Started backporting to release/9.0-staging: https://github.com/dotnet/runtime/actions/runs/16368520024

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.

System.Text.Json 9 Serialization Issue rg. Flags-Enums when using JsonStringEnumConverter
2 participants