ApiExplorer and OpenAPI union support#67001
Open
DeagleGross wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
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
anyOfemission for union types (including nested/contained unions and multi-Producesscenarios). - 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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