Skip to content

Minimal APIs (RDF): Support union types as endpoint parameters and return types #66542

@DeagleGross

Description

@DeagleGross

Support C# union types in the reflection-based RequestDelegateFactory path for Minimal APIs — both as input (request body) parameters and as return types.

Scenarios

Union as request body parameter:

union Command(CreateOrder, UpdateOrder);

app.MapPost("/orders", (Command command) =>
{
    return Results.Ok();
});

Union as return type:

union Order(CompletedOrder, InProgressOrder);

app.MapGet("/orders/{id}", (int id) =>
{
    if (id <= 0) return new InProgressOrder { Id = id, State = "..." };
    return new CompletedOrder { Id = id, TotalPrice = 123 };
});

Areas to investigate

  1. Return type handling (AddResponseWritingToMethodCall in RequestDelegateFactory.cs):

    • When return type is a union, RDF should route to the JSON serialization path
    • The HasKnownPolymorphism() check may need updating — unions are not polymorphic in the [JsonPolymorphic] sense, but they do need STJ to handle serialization. Verify that the standard JSON path works correctly without the "fast path" flag, or add a parallel check for unions
  2. Request body deserialization (HandleRequestBodyAndCompileRequestDelegateForJson):

    • This path calls options.GetReadOnlyTypeInfo(bodyType) and passes the JsonTypeInfo to ReadFromJsonAsync
    • For unions, bodyType will be the union type itself. STJ's union converter handles deserialization — likely no RDF changes needed here, but verify with tests
  3. Parameter inference (CreateArgument):

    • Union types that aren't simple (no TryParse, not a service, not from route/query/header) should be inferred as body parameters
    • Verify this works correctly — union types should fall through to body binding

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions