Minimal API unions support in RDF and RDG as endpoint parameters and return types#66951
Open
DeagleGross wants to merge 14 commits into
Open
Minimal API unions support in RDF and RDG as endpoint parameters and return types#66951DeagleGross wants to merge 14 commits into
DeagleGross wants to merge 14 commits into
Conversation
…parameters are not bound
Contributor
There was a problem hiding this comment.
Pull request overview
Adds Minimal API coverage for C# union types (System.Text.Json union support) across both reflection-based (RDF) and source-generated (RDG) request delegate creation paths, focusing on JSON body binding and JSON responses. Also updates the minimal sample to demonstrate union usage with JsonUnion classifiers.
Changes:
- Add a shared set of union test types (primitive unions, ambiguous unions, nullable cases, object-case unions, nested unions, polymorphic-case unions, classifier-backed unions).
- Add RDG/RDF tests validating union request-body binding behaviors (success, ambiguity → 400, classifier resolution, empty-body behaviors, non-JSON content-type → 415, wrapper DTO scenarios,
[AsParameters]). - Add RDG/RDF tests validating union response serialization behaviors (sync/Task/ValueTask, nullable wrapper, nested unions, ambiguous unions, runtime-type dispatch, polymorphic case behavior,
IAsyncEnumerable<T>,ConfigureHttpJsonOptions). - Refactor existing JSON-body tests to use a new helper that consistently creates an empty JSON body request.
Show a summary per file
| File | Description |
|---|---|
| src/Http/samples/MinimalSample/Program.cs | Adds sample endpoints/types demonstrating unions in body binding and as return values, including classifier examples. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/SharedTypes.Unions.cs | Introduces shared union types and classifier factories used by generator tests. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.Unions.Responses.cs | Adds union-focused response serialization tests for RDG/RDF. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.Unions.Body.cs | Adds union-focused JSON body binding tests for RDG/RDF. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.JsonBodyOrService.cs | Uses the new empty-body helper for consistency. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.JsonBody.cs | Uses the new empty-body helper for consistency. |
| src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTestBase.cs | Adds helpers for empty JSON body requests and custom content types. |
Copilot's findings
- Files reviewed: 7/7 changed files
- Comments generated: 2
…hub.com/DeagleGross/aspnetcore into dmkorolev/unions/minimalapi-inputoutput
Youssef1313
reviewed
Jun 2, 2026
Youssef1313
reviewed
Jun 2, 2026
Youssef1313
reviewed
Jun 2, 2026
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.
Current PR validates aspnetcore correctly integrates with System.Text.Json to support unions. Note, that this PR covers unions only as body-parameters (input) and as return types (output). Unions support for using them in query/route/header (basically non-body) will be covered in #66648.
There are several bugs/innacuracies/behavioral questions I observed in both aspnetcore/runtime which are now being tracked:
Here is the list of scenarios I validated. Each row runs in both `CompileTimeCreationTests` (RDG / source-gen path) and `RuntimeCreationTests` (RDF / reflection path):
Request body binding
Union?)[JsonPolymorphic]cases)ConfigureHttpJsonOptionshonored[FromBody][JsonRequired][AsParameters]container with union body + route slotReturn types
Task<Union>/ValueTask<Union>Union?returnResults<T1,T2>wrappersJsonTypeClassifierIAsyncEnumerable<Union>ConfigureHttpJsonOptionshonoredCloses #66542
Closes #66543