Open API: Use Microsoft.AspNetCore.OpenApi for Open API document generation#21058
Conversation
…OpenApi` Also installed `Swashbuckle.AspNetCore.SwaggerUI` for now to use as UI only.
Still missing polymorphism settings for both APIs
…ts project to avoid models mode exception being logged in tests
…tom transformers for handling enums and polymorphism
…ration # Conflicts: # Directory.Packages.props
…ration # Conflicts: # Directory.Packages.props # src/Umbraco.Cms.Api.Common/Configuration/ConfigureUmbracoSwaggerGenOptions.cs # src/Umbraco.Cms.Api.Common/OpenApi/EnumSchemaFilter.cs # src/Umbraco.Cms.Api.Common/OpenApi/MimeTypeDocumentFilter.cs # src/Umbraco.Cms.Api.Common/OpenApi/RemoveSecuritySchemesDocumentFilter.cs # src/Umbraco.Cms.Api.Common/OpenApi/SwaggerRouteTemplatePipelineFilter.cs # src/Umbraco.Cms.Api.Delivery/Configuration/ConfigureUmbracoDeliveryApiSwaggerGenOptions.cs # src/Umbraco.Cms.Api.Delivery/Configuration/ConfigureUmbracoMemberAuthenticationDeliveryApiSwaggerGenOptions.cs # src/Umbraco.Cms.Api.Delivery/Filters/SwaggerContentDocumentationFilter.cs # src/Umbraco.Cms.Api.Delivery/Filters/SwaggerDocumentationFilterBase.cs # src/Umbraco.Cms.Api.Delivery/Filters/SwaggerMediaDocumentationFilter.cs # src/Umbraco.Cms.Api.Management/Configuration/ConfigureUmbracoManagementApiSwaggerGenOptions.cs # src/Umbraco.Cms.Api.Management/OpenApi.json # src/Umbraco.Cms.Api.Management/OpenApi/BackOfficeSecurityRequirementsOperationFilterBase.cs # src/Umbraco.Cms.Api.Management/OpenApi/NotificationHeaderFilter.cs # src/Umbraco.Cms.Api.Management/OpenApi/ReponseHeaderOperationFilter.cs # src/Umbraco.Cms.Api.Management/OpenApi/RequireNonNullablePropertiesSchemaFilter.cs # src/Umbraco.Web.UI.Client/src/packages/core/backend-api/sdk.gen.ts # src/Umbraco.Web.UI.Client/src/packages/core/backend-api/types.gen.ts # templates/UmbracoExtension/Composers/UmbracoExtensionApiComposer.cs
|
@AndyButland I've made some changes since your last review and also created an issue in the ASP.NET Core repository, dotnet/aspnetcore#66340, so that we can eventually remove our workaround in https://github.com/umbraco/Umbraco-CMS/pull/21058/changes#diff-7416dbdc100a3dae0edff11951c554ab2954e4853fb404b86458c693ec395380. |
…irectory.Packages.props and removed unnecessary Swashbuckle reference.
…HeaderTransformer.
…void potential KeyNotFoundException.
AndyButland
left a comment
There was a problem hiding this comment.
Looks great @lauraneto, and in testing all seems to be working as expected. I've regenerated the typed client, and verified everything builds, the backoffice works as expected and the Swagger UI displays with the expected endpoint and model information.
The last few things I've found I think are non-controversial so I've committed them to save another round - but please have a look and if you have any concerns we can amend. Otherwise good to merge in I would say, please go ahead when you are happy to.
…StaticAssets.csproj.
…oft-open-api-document-generation # Conflicts: # src/Umbraco.Cms.Api.Management/OpenApi.json # src/Umbraco.Web.UI.Client/src/packages/core/backend-api/types.gen.ts
#22670) * Update Umbraco extension template for OpenAPI route changes Following the migration from Swashbuckle to Microsoft.AspNetCore.OpenApi in #21058, the extension template still pointed at the old Swagger URL pattern and used outdated terminology in code comments. - generate-client npm script now points at /umbraco/openapi/{name}.json instead of /umbraco/swagger/{name}/swagger.json - generate-openapi.js renames swaggerUrl to openApiUrl and updates the example URL in the missing-argument error message - UmbracoExtensionApiComposer.cs comments updated from "Swagger" to "OpenAPI" * Scope custom OpenAPI document to extension's own endpoints Without an explicit ShouldInclude predicate, Microsoft.AspNetCore.OpenApi only includes endpoints whose ApiExplorer GroupName equals the document name. The template's controller declared a different group name, so the custom document was created but stayed empty (paths: []), which in turn made npm run generate-client produce an empty TypeScript SDK. Filter by the [MapToApi] attribute already present on the extension's controller base, mirroring the pattern used by the Management and Delivery API options. * Add Microsoft.AspNetCore.OpenApi reference to Central package management The PerProject mode of the umbraco-extension template took a direct dependency on Microsoft.AspNetCore.OpenApi (with a long comment explaining why) but the Central mode did not, so default Central scaffolds failed to build with the source-generator interceptors error. Mirror the dependency in the Central csproj block and Directory.Packages.props.
Summary
Migrates from
Swashbuckle.AspNetCoreto Microsoft's nativeMicrosoft.AspNetCore.OpenApipackage for OpenAPI document generation.Biggest changes
Microsoft.AspNetCore.OpenApiandSwashbuckle.AspNetCore.SwaggerUIand uninstalledSwashbuckle.AspNetCore- keeping the swagger UI for now.swagger->openapiandfilter->transformer, including document and UI routes.ConfigureUmbracoOpenApiOptionsBaseclass from which our APIs (Default, Management and Delivery) extend from, which contains shared configuration/transformers.IOperationIdHandlerISchemaIdHandlerRemoveSecuritySchemesDocumentFilterISubTypesSelectorandEnumSchemaFilterhave been removed as they are no longer needed due to the correct Json Serializer options being usedReplaceOpenApiSchemaServiceto replace the registered service, for each API, with one that uses their corresponding settings.SwaggerRouteTemplatePipelineFilterand introducedUmbracoOpenApiOptionsoptions, making it easier to configureWhat's New
UmbracoOpenApiOptions- Configure OpenAPI document routes and UIExcludeFromDefaultOpenApiDocumentAttribute- Exclude endpoints from the default OpenAPI documentUmbracoSchemaIdGenerator- Static class for generating schema IDs (replaces handler interfaces)UmbracoOperationIdTransformer- Public transformer for customizing operation IDsAddBackofficeSecurityRequirements()- Extension method onOpenApiOptionsto easily secure custom Management APIsAddDeliveryApiOpenApiMemberAuthentication()- Extension method onIServiceCollectionto enable member authentication in the Delivery API OpenAPI documentAddOpenApiDocumentToUi()- Extension method to add OpenAPI documents to the UI dropdown, respecting configured route templatesOnPreMapEndpointsandOnPostMapEndpointsmethods onIUmbracoPipelineFilterBreaking Changes
Type Changes
ISubTypesHandler,ISubTypesSelector,SubTypesHandler,SubTypesSelectorJsonSerializerOptionsISchemaIdHandler,ISchemaIdSelector,SchemaIdHandler,SchemaIdSelectorUmbracoSchemaIdGeneratorclassIOperationIdHandler,IOperationIdSelector,OperationIdHandler,OperationIdSelectorIDocumentInclusionSelector,DocumentInclusionSelectorConfigureUmbracoSwaggerGenOptionsConfigureUmbracoOpenApiOptionsBaseEnumSchemaFilterMimeTypeDocumentFilterMimeTypesTransformerRemoveSecuritySchemesDocumentFilterBackOfficeSecurityRequirementsOperationFilterBaseAddBackofficeSecurityRequirements()extension methodRequireNonNullablePropertiesSchemaFilterRequireNonNullablePropertiesSchemaTransformerSwaggerRouteTemplatePipelineFilterUmbracoOpenApiOptionsfor route/availability configurationConfigureUmbracoMemberAuthenticationDeliveryApiSwaggerGenOptionsAddDeliveryApiOpenApiMemberAuthentication()extension methodConfigureUmbracoDeliveryApiSwaggerGenOptionsConfigureUmbracoDeliveryApiOpenApiOptionsConfigureUmbracoManagementApiSwaggerGenOptionsConfigureUmbracoManagementApiOpenApiOptionsSeveral internal types were also removed/replaced (e.g.,
NotificationHeaderFilter→NotificationHeaderTransformer).URL Changes
/umbraco/swagger/{documentName}/swagger.json/umbraco/openapi/{documentName}.json/umbraco/swagger//umbraco/openapi/The new paths can be customized via
UmbracoOpenApiOptions.RouteTemplateandUmbracoOpenApiOptions.UiRoutePrefix.Note: The OAuth client ID for the OpenAPI UI remains
umbraco-swaggerfor backwards compatibility with existing database registrations inumbracoOpenIddictApplications.OpenAPI Document Changes
These changes apply to both Management API and Delivery API.
Structural Changes
"nullable": true"type": ["null", "string"](OpenAPI 3.1 style)$refPolymorphism Changes
propertyName: "$type"Delivery API Specific
securitySchemeswithsecurityarrayApiContentResponseModelIApiContentResponseModel(matches declared interface)uniqueItems: trueHashSet<Guid>paramsKnown Limitations
Polymorphic Type Schema Naming
Microsoft.AspNetCore.OpenApi prefixes derived type schema names with the base type name for polymorphic types.
Example:
DocumentPropertyValuePermissionPresentationModelIPermissionPresentationModelDocumentPropertyValuePermissionPresentationModelThis is a known limitation (dotnet/aspnetcore#58332). The discriminator values remain unchanged, so JSON serialization is unaffected. Client SDK type names will differ.
Test Plan
Documentation
umbraco/UmbracoDocs#8021