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
-
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
-
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
-
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
Support C# union types in the reflection-based
RequestDelegateFactorypath for Minimal APIs — both as input (request body) parameters and as return types.Scenarios
Union as request body parameter:
Union as return type:
Areas to investigate
Return type handling (
AddResponseWritingToMethodCallinRequestDelegateFactory.cs):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 unionsRequest body deserialization (
HandleRequestBodyAndCompileRequestDelegateForJson):options.GetReadOnlyTypeInfo(bodyType)and passes theJsonTypeInfotoReadFromJsonAsyncbodyTypewill be the union type itself. STJ's union converter handles deserialization — likely no RDF changes needed here, but verify with testsParameter inference (
CreateArgument):TryParse, not a service, not from route/query/header) should be inferred as body parameters