Skip to content

Suppress SYSLIB1227 for unions with custom converters - #133244

Merged
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-fix-json-union-converter-warning
Sep 9, 2026
Merged

eiriktsarpalis merged 1 commit into
dotnet:mainfrom
eiriktsarpalis:eiriktsarpalis-fix-json-union-converter-warning

Conversation

@eiriktsarpalis

Copy link
Copy Markdown
Member

A type-level [JsonConverter] makes generated metadata use the custom-converter path, but the source generator still analyzed the union cases first and emitted SYSLIB1227.

Skip union case discovery and ambiguity diagnostics when a type-level converter owns the contract. Add source-generator coverage for an otherwise ambiguous union handled by a custom converter.

Fixes #133156.

Validated with:

  • build.cmd clr+libs -rc release
  • all five System.Text.Json test projects (131,786 test executions, 0 failures; 17 existing skips)

Note

This pull request description was generated by GitHub Copilot.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 45c8108d-e1a0-493a-b9d3-6ffc8bd752d4
@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.

@eiriktsarpalis eiriktsarpalis added this to the 12.0.0 milestone Sep 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.

🟡 Changes recommended

The new test can pass without actually exercising the intended scenario if the in-memory compilation fails, and it should assert compilation success (and/or align the test name with what is asserted).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates the System.Text.Json source generator’s union analysis so that when a type has a type-level [JsonConverter] (i.e., metadata will use the custom-converter contract path), the generator no longer discovers union case types and no longer emits the union ambiguity warning SYSLIB1227. It also adds a unit test covering the scenario.

Changes:

  • Skip union case-type enqueueing/ambiguity diagnostics when a type-level JsonConverterAttribute is present.
  • Add source-generator diagnostic test coverage ensuring SYSLIB1227 is not produced for an otherwise-ambiguous union when a custom converter is applied.
File summaries
File Description
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs Gated union case discovery/ambiguity diagnostics behind “no type-level JsonConverterAttribute” so custom-converter-owned contracts don’t trigger SYSLIB1227.
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorDiagnosticsTests.cs Adds a regression test for unions with a type-level custom converter to ensure SYSLIB1227 is not emitted.
Review details

Suppressed comments (1)

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

  • This test can pass vacuously if the in-memory compilation fails (since diagnostic validation is disabled and the test doesn't assert compilation success). Add a compilation diagnostic check so the test always exercises the generator with valid source.
            Compilation compilation = CompilationHelper.CreateCompilation(source);
            JsonSourceGeneratorResult result = CompilationHelper.RunJsonSourceGenerator(compilation, disableDiagnosticValidation: true);

            Assert.DoesNotContain(result.Diagnostics, diagnostic => diagnostic.Id == "SYSLIB1227");
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

@eiriktsarpalis
eiriktsarpalis merged commit f8119e0 into dotnet:main Sep 9, 2026
80 of 83 checks passed
@eiriktsarpalis

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

artl93 pushed a commit that referenced this pull request Sep 10, 2026
…133475)

Backport of #133244 to release/11.0

/cc @eiriktsarpalis

## Customer Impact

- [x] Customer reported
- [ ] Found internally

Customer reported issue where the source generator will report a
diagnostic about a union type requiring a type classifier even though it
has been annotated with a `JsonConverterAttribute` which overrides all
other serialization mechanisms.

## Regression

- [ ] Yes
- [x] No

## Testing

Added unit testing covering the impacted scenario.

## Risk

Low. Targeted product code fix adding a missing check.

Co-authored-by: Eirik Tsarpalis <[email protected]>
Copilot-Session: 45c8108d-e1a0-493a-b9d3-6ffc8bd752d4
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Text.Json source generator emits warning SYSLIB1227 even when the union is directly covered by a custom converter

3 participants