Skip to content

ApiExplorer and OpenAPI union support#67001

Open
DeagleGross wants to merge 3 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/unions/mvcexplorer+openapi
Open

ApiExplorer and OpenAPI union support#67001
DeagleGross wants to merge 3 commits into
dotnet:mainfrom
DeagleGross:dmkorolev/unions/mvcexplorer+openapi

Conversation

@DeagleGross
Copy link
Copy Markdown
Member

Thanks to #65650 merged before, union support it trivial now. This PR validates and extends test coverage for C# union types through MVC ApiExplorer and OpenAPI.

API Explorer is the core component, which produces metadata from code definition of minimal api or controllers, which is consumed by many libraries like OpenAPI (aspnetcore's), Swashbuckle, NSwag and etc. Since after #65650 multiple types may be produced by API Explorer per same statusCode and contentType, I was wondering if unions should not be a separate type, but its case types should expand in API Explorer. However, I found that Swashbuckle and NSwag (and other external consumers) of API Explorer do not support multiple response types properly as we do now, so it will break them. I have started the conversation in the issue - please comment on that if you see value in implementing this differently: #66544 (comment)

Closes #66544

@DeagleGross DeagleGross self-assigned this Jun 3, 2026
Copilot AI review requested due to automatic review settings June 3, 2026 12:01
@DeagleGross DeagleGross requested review from a team and halter73 as code owners June 3, 2026 12:01
@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jun 3, 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

This PR expands ASP.NET Core’s union-type coverage across MVC ApiExplorer and the built-in OpenAPI document/schema generation by adding targeted unit tests plus end-to-end integration snapshots via the OpenAPI sample app.

Changes:

  • Added ApiExplorer tests validating union response-type inference and coexistence/merge semantics when multiple types share the same status code.
  • Added OpenAPI schema/document tests validating anyOf emission for union types (including nested/contained unions and multi-Produces scenarios).
  • Extended the OpenAPI sample + integration snapshots to include a new “unions” document and representative union endpoints for regression coverage.
Show a summary per file
File Description
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Shared/SharedTypes.Unions.cs Adds shared union-shaped types (primitive/object cases + container) used by OpenAPI tests and sample.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.UnionSchemas.cs Adds schema-service tests verifying unions produce component $ref + anyOf (response + request body + typed results).
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiDocumentService/OpenApiDocumentServiceTests.UnionResponses.cs Adds document-service tests verifying multi-response-type merging into outer anyOf without flattening union internals.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApiDocumentLocalizationTests.VerifyOpenApiDocumentIsInvariant.verified.txt Updates invariant integration snapshot to include union endpoints and schemas.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_2/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=unions.verified.txt Adds OpenAPI 3.2 snapshot for the new “unions” document.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_2/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt Updates controller snapshot to include union-related controller routes/schemas.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_1/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=unions.verified.txt Adds OpenAPI 3.1 snapshot for the new “unions” document.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_1/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt Updates controller snapshot to include union-related controller routes/schemas.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=unions.verified.txt Adds OpenAPI 3.0 snapshot for the new “unions” document.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/snapshots/OpenApi3_0/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=controllers.verified.txt Updates controller snapshot to include union-related controller routes/schemas.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Integration/OpenApiDocumentIntegrationTests.cs Registers the new “unions” document for integration testing across spec versions.
src/OpenApi/sample/Sample.csproj Compiles the new union shared-types file into the OpenAPI sample app for snapshot generation.
src/OpenApi/sample/Program.cs Registers and maps the new unions OpenAPI document + endpoints in the sample app.
src/OpenApi/sample/Endpoints/MapUnionsEndpoints.cs Adds minimal API endpoints that exercise union responses/requests and multi-Produces combinations.
src/OpenApi/sample/Controllers/TestController.cs Adds controller endpoints returning unions and multiple ProducesResponseType entries for union scenarios.
src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.Unions.cs Adds ApiExplorer unit tests validating union response-type behavior in endpoint metadata processing.
src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs Makes the test class partial to split union coverage into a dedicated file.

Copilot's findings

  • Files reviewed: 17/17 changed files
  • Comments generated: 0

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

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI: Generate anyOf schemas for C# union types

2 participants