Skip to content

Minimal API unions support in RDF and RDG as endpoint parameters and return types#66951

Open
DeagleGross wants to merge 14 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/unions/minimalapi-inputoutput
Open

Minimal API unions support in RDF and RDG as endpoint parameters and return types#66951
DeagleGross wants to merge 14 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/unions/minimalapi-inputoutput

Conversation

@DeagleGross
Copy link
Copy Markdown
Member

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:

  1. [Runtime] JsonSerializer throws when serializing null value in a value-type nullable union case runtime#128688
  2. [Runtime] STJ: union vs. class asymmetry — missing JSON silently produces default(union) that crashes on serialize runtime#128834
  3. [Aspnetcore] RequestDelegateGenerator: [AsParameters] emits unqualified type names — CS0234 when user namespace collides #66950
  4. [Aspnetcore] Source generator silently drops [FromBody(EmptyBodyBehavior=Allow)] when two endpoints share a delegate signature #66912
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

Scenario Verified
Unambiguous primitive cases (bool/string, etc.) Round-trip
Ambiguous primitive cases without classifier 400 BadRequest
Ambiguous primitive cases with classifier Round-trip
Nullable union wrapper (Union?) Value + null
Union with nullable case Fails without classifier, passes with
Object-case union Fails without classifier, passes with
Object-case union, payload of derived type Resolves to nearest declared case
Nested union (union inside union) Requires outer classifier
Ambiguous numeric union Fails without classifier, passes with
Polymorphic case union ([JsonPolymorphic] cases) Binds active case
ConfigureHttpJsonOptions honored Yes
Explicit [FromBody] Behaves like implicit body
Empty body Status depends on nullability + policy
Non-JSON content type 415 UnsupportedMediaType
Union as property of wrapper DTO Deserializes correctly
Union as property with [JsonRequired] 400 on missing, 400 on null, 200 on present
[AsParameters] container with union body + route slot Union → body, sibling → route

Return types

Scenario Verified
Primitive cases Serialize as expected
Task<Union> / ValueTask<Union> Serialize active case
Union? return Value + null
Union with nullable case Each case handled
Object-case union Serializes active case
Nested union Serializes innermost case
Runtime-type dispatch Resolves to nearest declared case
Ambiguous numeric union Serializes by .NET runtime type
Ambiguous string union Serializes by .NET runtime type
Returned via Results<T1,T2> wrappers Serializes active case
Union as property of wrapper DTO Serializes active case
Union with polymorphic case type Emits discriminator
Custom JsonTypeClassifier Does not affect serialization
IAsyncEnumerable<Union> Streams active case per item
ConfigureHttpJsonOptions honored Yes

Closes #66542
Closes #66543

@DeagleGross DeagleGross self-assigned this Jun 1, 2026
Copilot AI review requested due to automatic review settings June 1, 2026 09:31
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Jun 1, 2026
Copy link
Copy Markdown
Contributor

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

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

@DeagleGross DeagleGross requested review from a team and wtgodbe as code owners June 1, 2026 16:47
@gfoidl gfoidl added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minimal APIs (RDG): Source-generated support for union types Minimal APIs (RDF): Support union types as endpoint parameters and return types

4 participants