Skip to content

NET11 csharp unions schema support - #4103

Merged
martincostello merged 2 commits into
domaindrivendev:dotnet-vnextfrom
DeagleGross:deaglegross-csharp-unions-schema-support
Aug 5, 2026
Merged

martincostello merged 2 commits into
domaindrivendev:dotnet-vnextfrom
DeagleGross:deaglegross-csharp-unions-schema-support

Conversation

@DeagleGross

Copy link
Copy Markdown
Contributor

.NET 11 introduces C# union types, which System.Text.Json represents transparently as an anyOf of the union's case types. This PR adds mirroring behavior compared to STJ and the in-box Microsoft.AspNetCore.OpenApi.

All union code and tests are guarded by #if NET11_0_OR_GREATER and the net11 TFM is added additively and gated on SDK support; no public API changes and no new dependencies. net8/9/10 behavior is unchanged.

Is a follow-up of #4100.
Related dotnet/aspnetcore#66544, #2743.

@DeagleGross DeagleGross changed the title Deaglegross csharp unions schema support NET11 csharp unions schema support Aug 5, 2026
@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.20%. Comparing base (e3eab0c) to head (eb64a70).

Additional details and impacted files
@@               Coverage Diff                @@
##           dotnet-vnext    #4103      +/-   ##
================================================
+ Coverage         95.17%   95.20%   +0.02%     
================================================
  Files               111      111              
  Lines              4127     4149      +22     
  Branches            844      847       +3     
================================================
+ Hits               3928     3950      +22     
  Misses              199      199              
Flag Coverage Δ
Linux 95.20% <100.00%> (+0.02%) ⬆️
Windows 95.20% <100.00%> (+0.02%) ⬆️
macOS 95.20% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@DeagleGross DeagleGross mentioned this pull request Aug 5, 2026
1 of 10 tasks
@martincostello

Copy link
Copy Markdown
Collaborator

@DeagleGross If you update the merge base to dotnet-vnext which has already got the net11.0 SDK and TFMs wired-up, this PR should be in a good state to verify unions support.

Mirror System.Text.Json by emitting union types as an OpenAPI anyOf of
their case-type schemas. Detection via JsonTypeInfoKind.Union / UnionCases
in JsonSerializerDataContractResolver (internal, #if NET11_0_OR_GREATER);
emission via TryGenerateUnionSchema hooked at the SchemaGenerator entry
point, producing $refs to the reusable component schemas (shared case types
deduplicate naturally). No public API changes, no new dependencies; net10
unaffected (union code compiled out). Rebased onto dotnet-vnext, which
provides the net11 TFM; test project sets LangVersion=preview on net11 only
so union test types compile. Includes schema-level and document-level
(anyOf->$ref + single-representation dedup) tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7722de8a-3ea5-40fd-999a-4f647ce8f4f7
@DeagleGross
DeagleGross force-pushed the deaglegross-csharp-unions-schema-support branch from 95c3f7b to 230ed5d Compare August 5, 2026 10:48
@DeagleGross
DeagleGross changed the base branch from master to dotnet-vnext August 5, 2026 10:48
@DeagleGross

Copy link
Copy Markdown
Contributor Author

rebased on dotnet-vnext!

@DeagleGross
DeagleGross marked this pull request as ready for review August 5, 2026 10:51
Parameterize the document-level union test over OpenApiSpecVersion (3.0 and
3.1) to verify the anyOf-of-$refs and single-representation dedup hold for
both spec versions. Addresses PR review feedback.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7722de8a-3ea5-40fd-999a-4f647ce8f4f7
@martincostello
martincostello merged commit 705284d into domaindrivendev:dotnet-vnext Aug 5, 2026
10 checks passed
@DeagleGross
DeagleGross deleted the deaglegross-csharp-unions-schema-support branch August 5, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants