If you are not using the Delivery API, it is best to remove it entirely from the pipeline by excluding it during the AddDeliveryApi() configuration. However, if you are using the Delivery API but want to exclude it from Swagger UI, you can use a custom IConfigureOptions
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Umbraco.Cms.Api.Common.OpenApi;
using Umbraco.Cms.Api.Delivery.Configuration;
public class ConfigureCustomUmbracoDeliveryApiSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
{
public void Configure(SwaggerGenOptions swaggerGenOptions)
{
if (swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey("delivery"))
{
swaggerGenOptions.SwaggerGeneratorOptions.SwaggerDocs.Remove("delivery");
}
// Optionally, clear other filters if needed (specific to Delivery API)
swaggerGenOptions.DocumentFilterDescriptors.RemoveAll(filter =>
filter.Type.Name.Contains("MimeTypeDocumentFilter", StringComparison.OrdinalIgnoreCase));
swaggerGenOptions.OperationFilterDescriptors.RemoveAll(filter =>
filter.Type.Name.Contains("SwaggerContentDocumentationFilter", StringComparison.OrdinalIgnoreCase) ||
filter.Type.Name.Contains("SwaggerMediaDocumentationFilter", StringComparison.OrdinalIgnoreCase));
swaggerGenOptions.ParameterFilterDescriptors.RemoveAll(filter =>
filter.Type.Name.Contains("SwaggerContentDocumentationFilter", StringComparison.OrdinalIgnoreCase) ||
filter.Type.Name.Contains("SwaggerMediaDocumentationFilter", StringComparison.OrdinalIgnoreCase));
}
}
// Register the custom configuration to replace the default behavior.
builder.Services.AddSingleton<IConfigureOptions<SwaggerGenOptions>, ConfigureCustomUmbracoDeliveryApiSwaggerGenOptions>();
Thanks for taking your time to reply to my question.
We do not use the Delivery API, so the best solution would be to remove it as you say. But enabling the Delivery API just had all the common logic for swagger out of the box.
So we ended up making our own .AddCustomSwagger() we switched out with the .AddDeliveryApi().
public static IUmbracoBuilder AddCustomSwagger(this IUmbracoBuilder builder)
{
builder.Services.ConfigureOptions<CustomConfigureSwaggerGenOptions>();
builder.Services.ConfigureOptions<CustomApiVersioningConfiguration>();
builder.Services.AddSwaggerGen(setupAction =>
{
setupAction.EnableAnnotations();
setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
In = ParameterLocation.Header
});
setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
builder.Services.Configure<UmbracoPipelineOptions>(options =>
{
// Remove default Swagger route setup (if needed)
options.PipelineFilters.RemoveAll(filter => filter is SwaggerRouteTemplatePipelineFilter);
// Setup your own Swagger routes with custom filter
options.AddFilter(new CustomSwaggerRouteTemplatePipelineFilter("custom-api-v1"));
});
builder.Services.AddAuthentication();
return builder;
}
The CustomConfigureSwaggerGenOptions contains DocumentFilter, DocInclusionPredicate, CustomOperationIds and CustomSchemaIds.
The CustomSwaggerRouteTemplatePipelineFilter use a PostPipelineAction to add UseSwagger, UseSwaggerUI and SwaggerEndpoint.
How to remove umbraco delivery api from swagger ui
Hi all.
How would I be able to remove umbraco delivery api from swagger ui and only use my custom one in Umbraco 13?
Hi Bo,
If you are not using the Delivery API, it is best to remove it entirely from the pipeline by excluding it during the AddDeliveryApi() configuration. However, if you are using the Delivery API but want to exclude it from Swagger UI, you can use a custom IConfigureOptions
Make sure you are registering after the pipeline
Hope this helps!
Hi Afreed.
Thanks for taking your time to reply to my question.
We do not use the Delivery API, so the best solution would be to remove it as you say. But enabling the Delivery API just had all the common logic for swagger out of the box.
So we ended up making our own .AddCustomSwagger() we switched out with the .AddDeliveryApi().
The CustomConfigureSwaggerGenOptions contains DocumentFilter, DocInclusionPredicate, CustomOperationIds and CustomSchemaIds.
The CustomSwaggerRouteTemplatePipelineFilter use a PostPipelineAction to add UseSwagger, UseSwaggerUI and SwaggerEndpoint.
is working on a reply...