How to apply the Umbraco schema and operation IDs for custom Management APIs
All core Management APIs have a custom scheme for their generated OpenAPI schema and operation IDs.
This scheme is strictly opt-in to avoid affecting custom APIs by default. In this article, we'll see how to opt-in to the scheme.
If you are happy with your APIs' default schema and operation IDs, nothing is likely gained by using the Umbraco ones.
Schema IDs
Schema IDs are handled by ISchemaIdHandler implementations. To opt-in to the Umbraco schema IDs, we base our implementation on the core handler:
SampleSchemaIdHandler.cs
usingUmbraco.Cms.Api.Common.OpenApi;namespaceUmbracoDocs.Samples;// this schema ID handler extends the Umbraco schema IDs// to all types in the UmbracoDocs.Samples namespacepublicclassSampleSchemaIdHandler:SchemaIdHandler{publicoverrideboolCanHandle(Type type)=>type.Namespace=="UmbracoDocs.Samples";}
Then, we implement a composer to register the new schema ID handler:
Operation IDs follow the same pattern as schema IDs. The only difference is that the IOperationIdHandler operates at the API level, not at the type level.
Again, to opt-in to the Umbraco operation IDs, we base our implementation on the core handler:
SampleOperationIdHandler.cs
usingAsp.Versioning;usingMicrosoft.AspNetCore.Mvc.ApiExplorer;usingMicrosoft.AspNetCore.Mvc.Controllers;usingMicrosoft.Extensions.Options;usingUmbraco.Cms.Api.Common.OpenApi;namespaceUmbracoDocs.Samples;// this operation ID handler extends the Umbraco operation IDs// to all API controllers in the UmbracoDocs.Samples namespacepublicclassSampleOperationIdHandler:OperationIdHandler{publicSampleOperationIdHandler(IOptions<ApiVersioningOptions> apiVersioningOptions): base(apiVersioningOptions) { } protected override bool CanHandle(ApiDescription apiDescription, ControllerActionDescriptor controllerActionDescriptor)
=> controllerActionDescriptor.ControllerTypeInfo.Namespace == "UmbracoDocs.Samples";}
Then, we implement a composer to register the new operation ID handler: