Skip to content

Suppress union ambiguity warning when a global classified is specified at compile time - #131797

Merged
eiriktsarpalis merged 3 commits into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-fix-union-classifier-warning
Aug 5, 2026
Merged

Suppress union ambiguity warning when a global classified is specified at compile time#131797
eiriktsarpalis merged 3 commits into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-fix-union-classifier-warning

Conversation

@eiriktsarpalis

Copy link
Copy Markdown
Member

Options-level type classifiers are emitted into JsonSerializerOptions.TypeClassifiers, but the source generator's union ambiguity diagnostic only checked JsonUnionAttribute.TypeClassifier. Treat a non-empty validated JsonSourceGenerationOptionsAttribute.TypeClassifiers list as configured classifier support so SYSLIB1227 isn't emitted in that case.

The existing source-generator diagnostic tests now cover both per-union and options-level classifier registration.

Validated with build.cmd clr+libs -rc release and all System.Text.Json test projects (130,518 executions, 0 failures; 17 existing skips).

Note

This pull request description was generated by GitHub Copilot.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 63fffa05-9788-4529-b41f-983227e77428
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the System.Text.Json source generator’s union ambiguity warning logic so that global (options-level) type classifiers are treated as configured classifier support, and adjusts diagnostics tests accordingly.

Changes:

  • Treat non-empty JsonSourceGenerationOptionsAttribute.TypeClassifiers as satisfying union classifier configuration (suppressing SYSLIB1227).
  • Update the union ambiguity diagnostic unit test to validate no SYSLIB1227 is produced when options-level classifiers are present.

Reviewed changes

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

File Description
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs Initializes the “classifier configured” flag from options-level TypeClassifiers so union ambiguity diagnostics aren’t emitted when global classifiers are registered.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorDiagnosticsTests.cs Updates/renames the relevant diagnostic test to assert SYSLIB1227 is not produced for options-level classifier registration.

Comment thread src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 63fffa05-9788-4529-b41f-983227e77428
Copilot AI review requested due to automatic review settings August 4, 2026 13:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Suppressed comments (1)

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorDiagnosticsTests.cs:819

  • Now that this test expects the generator to produce no diagnostics, leaving disableDiagnosticValidation: true weakens the test by skipping compilation+generator severity validation in CompilationHelper.RunJsonSourceGenerator. Consider removing the flag so the helper asserts there are no warnings/errors anywhere, matching other "CompilesWithout..." tests in this file.
            Compilation compilation = CompilationHelper.CreateCompilation(source);
            JsonSourceGeneratorResult result = CompilationHelper.RunJsonSourceGenerator(compilation, disableDiagnosticValidation: true);

            Assert.Empty(result.Diagnostics);

Comment thread src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs Outdated
Copilot AI review requested due to automatic review settings August 4, 2026 15:06
@eiriktsarpalis eiriktsarpalis changed the title Suppress union ambiguity warning for global classifiers Suppress union ambiguity warning when a global classified is specified at compile time Aug 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Suppressed comments (2)

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs:991

  • hasUnionTypeClassifierSpecified is now initialized from options-level TypeClassifiers, but the name can be misread as only reflecting JsonUnionAttribute.TypeClassifier. Adding an inline comment here would make it clear that a non-empty context/options classifier list also opts a union into runtime classification and therefore suppresses ambiguity diagnostics.
                bool hasUnionTypeClassifierSpecified = options?.TypeClassifiers is { Count: > 0 };

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorDiagnosticsTests.cs:819

  • Assert.Empty(result.Diagnostics) makes this test brittle and inconsistent with the neighboring union-ambiguity tests, which only assert that SYSLIB1227 is (not) produced. If any unrelated source-generator diagnostic is added in the future, this test will start failing even though the behavior under test (suppressing SYSLIB1227 when options-level TypeClassifiers are present) still works.
            Assert.Empty(result.Diagnostics);

@eiriktsarpalis

Copy link
Copy Markdown
Member Author

/ba-g test failures are unrelated

@eiriktsarpalis
eiriktsarpalis merged commit 0b242ac into dotnet:main Aug 5, 2026
81 of 84 checks passed
@eiriktsarpalis
eiriktsarpalis deleted the eiriktsarpalis-fix-union-classifier-warning branch August 5, 2026 09:32
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 6, 2026
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.

3 participants