Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Jacobsen 608 posts 2406 karma points
    Nov 15, 2024 @ 14:58
    Bo Jacobsen
    0

    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?

  • Afreed 54 posts 261 karma points
    Nov 21, 2024 @ 06:55
    Afreed
    0

    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

    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>();
    

    Make sure you are registering after the pipeline

    Hope this helps!

  • Bo Jacobsen 608 posts 2406 karma points
    23 days ago
    Bo Jacobsen
    0

    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().

    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.

Please Sign in or register to post replies

Write your reply to:

Draft