Skip to content

A union that System.Text.Json cannot classify makes every Minimal API route return 500 (and fails startup with MVC) #69826

Open

Description

@robertodalmonte

Part of #64599. 11.0.100-rc.2.26504.105; the same behaviour on RC 1.

One Minimal API endpoint taking a union that the structural classifier refuses (JsonUnionTypeStructuralClassifier, #129805) makes endpoint building throw. The request then returns 500 for every route in the app, including routes that don't use the union:

public record NotRequired;
public record PendingApproval;
public record PartlyApproved(Guid Approver);
[JsonUnion(TypeClassifier = typeof(JsonUnionTypeStructuralClassifier))]
public union Approval(NotRequired, PendingApproval, PartlyApproved);

app.MapGet("/ok", () => "ok");
app.MapPost("/x", (Approval a) => a);
// GET /ok => 500 NotSupportedException: The JsonUnionTypeStructuralClassifier cannot classify
//   union type 'Approval' because the case type 'NotRequired' can never be selected uniquely...

The throw comes from RouteEndpointDataSource.CreateRouteEndpointBuilder / get_Endpoints().

Setup Result
Minimal API only startup succeeds; every route 500 on the first request
AddControllers() plus the bad Minimal API endpoint StartAsync throws NotSupportedException
AddAuthorization() startup throws
The bad union only on an MVC action only that action fails (500); /ok and other MVC routes return 200
A union ambiguous only at read time (no classifier) only that endpoint fails (400)

The structural classifier rejecting indistinguishable cases is by design (#129805). The question is the scope of the failure. Is it intended that one such endpoint takes the whole endpoint table down on Minimal APIs, or should the exception be scoped to that endpoint, as MVC already does?

Probes: QOa and Probe2 section G in https://github.com/robertodalmonte/csharp-unions-probed

Activity

  1. added
    area-minimal
    on Oct 14, 2026
  2. github-actions commented on Oct 14, 2026

    @github-actions
    Contributor

    Triage Summary

    Area: area-minimal (exception thrown from RouteEndpointDataSource endpoint building)
    Type: Bug (one endpoint's build failure affects all routes, unlike MVC)

    Potential Duplicates

    • None found

    Notes

    • Reported failure site: RouteEndpointDataSource.CreateRouteEndpointBuilder / Endpoints in src/Http/Routing/src/Builder/RouteEndpointDataSource.cs.

    Generated by Issue Triage Agent for dotnet/aspnetcore for #69826 · copilot · auto · 25.7 AIC · ⌖ 0.996 AIC · ⊞ 30.4K · ◷

  3. Youssef1313 commented on Oct 14, 2026

    @Youssef1313
    Member
  4. self-assigned this
    on Oct 14, 2026
  5. added theissue type on Oct 14, 2026
  6. halter73 commented on Oct 15, 2026

    @halter73
    Member

    This isn't specific to unions. For example, an unregistered service parameter on a MapGet can be inferred as a body parameter and fail endpoint construction. That can prevent unrelated routes from working too. We deliberately reject inferred GET bodies (#35405); explicit [FromBody] is supported.

    I don't think we should change the product behavior this late in .NET 11. Isolating these failures would need a broader look at endpoint-construction errors, not just a special case for unions. Unconditionally evaluating endpoints at startup would also change when user-defined code runs, and it would not cover endpoints added after startup.

    I'd be open to revisiting this after .NET 11. Better diagnostics and opt-in early validation seem like good starting points. Per-endpoint isolation is worth considering for failures that are genuinely local to an endpoint, but not every routing error can be isolated that way.

  7. robertodalmonte commented on Oct 16, 2026

    @robertodalmonte
    Author

    Thanks. Agreed on both points: it isn't union-specific (an ordinary GET where a record parameter is inferred as a body exhibits the same behavior in the probes), and it makes sense to avoid a change this late in the .NET 11 cycle.

    One data point for the post-.NET 11 discussion: endpoints are already built at startup in most apps, albeit incidentally. AddAuthorization() (which AddControllers() registers) makes the authorization policy cache enumerate the endpoints inside StartAsync.

    Because of this, the same bad endpoint today either fails at startup or turns every request into a 500. Which one happens depends entirely on whether an unrelated service is registered (as seen in rows 2 and 3 of the table in the issue description). An opt-in early validation would make this incidental behavior deliberate and available without authorization.

    Could this get a milestone (Backlog or .NET 12) so it gets picked up in the next cycle?

  8. added this to the .NET 12 Planning milestone on Oct 16, 2026
  9. added
    bug
    on Oct 23, 2026
  10. robertodalmonte commented on Nov 11, 2026

    @robertodalmonte
    Author

    Re-ran on GA (SDK 11.0.100, ASP.NET Core 11.0.0). Both failures reproduce unchanged:

    • Without authorization: one Minimal API endpoint taking the unclassifiable union (TwoEmpty) makes GET /ok return 500 with the classifier's NotSupportedException, even as the first request the host sees, inside a MapGroup, and for MVC routes (GET /mvc/ok) beside it.
    • With AddAuthorization() or AddControllers(): startup throws from AuthorizationPolicyCache..ctor. AddOpenApi() alone starts, then every request is 500.
    • The same union on an MVC action still fails only that action.

    Captures: QOa reflection, QOa RDG, Probe2 section G (RDG).

    One detail on the "not union-specific" point. The non-union control (a GET whose record parameter is inferred as a body) behaves like the union only on the reflection path:

    reflection RDG
    non-union inferred GET body, GET /ok 500 200
    unclassifiable union endpoint, GET /ok 500 500

    So with the Request Delegate Generator the non-union endpoint failure is contained to its own endpoint, and the union one still takes the route table down. That may matter when the diagnostics and per-endpoint isolation work is scoped for .NET 12.

  11. DeagleGross commented on Nov 11, 2026

    @DeagleGross
    Member

    Thanks for providing more info; we put the item in .NET 12 Planning - hopefully we will work on this an include in the next .NET release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-minimalbug

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions