Open Closed

Abp Swagger #5913


User avatar
0
dipak.z created
  • ABP Framework version: v5.2.1
  • UI Type: Angular / MVC / Blazor WASM / Blazor Server
  • Database System: EF Core (PostgreSQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

I want two swagger UI in one swagger there is default apis which are in default in abp.io project like identity server/client,languages,users,roles etc and in other swagger i want only my created app services apis how can i achieve this?


7 Answer(s)
  • User Avatar
    0
    Anjali_Musmade created
    Support Team Support Team Member

    Hi

    please check this guide to generate two endpoints in a swagger https://github.com/domaindrivendev/Swashbuckle.AspNetCore#generate-multiple-swagger-documents

    you can then add [ApiExplorerSettings(GroupName = "v2")] in each controller of your api.

    let me know if you face any issue

  • User Avatar
    0
    dipak.z created

    i tried this but Apis of identity server also shows in swagger i dont want that apis in v2 swagger

    [ApiExplorerSettings(GroupName = "v2")] public class SampleAPIAppService : ImageProcessingAPIAppService { [HttpGet] [ApiExplorerSettings(GroupName = "v2")] public int GetVersion() { return 1; } }

    in Web Module of project

    private void ConfigureSwaggerServices(IServiceCollection services) { services.AddAbpSwaggerGen( options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "ImageProcessingAPI API", Version = "v1" }); options.SwaggerDoc("v2", new OpenApiInfo { Title = "ImageProcessingAPI API V2", Version = "v2" }); options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); } ); }

    app.UseAbpSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "ImageProcessingAPI API"); options.SwaggerEndpoint("/swagger/v2/swagger.json", "ImageProcessingAPI API V2"); });

  • User Avatar
    0
    jfistelmann created

    You can conditionally hide endpoints using the DocInclusionPredicate. At the moment, it always returns true in the code you shared.

    Here's some inspiration:

    private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
        {
            context.Services.AddAbpSwaggerGenWithOAuth(
                configuration["AuthServer:Authority"],
                new Dictionary<string, string>
                {
                    {"Something", "Something API"}
                },
                options =>
                {
                    options.SwaggerDoc("v1", new OpenApiInfo { Title = "Something API - V1", Version = "v1" });
                    options.DocInclusionPredicate((docName, description) =>
                    {
                        return description.ActionDescriptor.IsControllerAction() && docName switch
                        {
                            "v1" => description.GroupName == null || description.GroupName == "v1",
                            "v2" => description.GroupName == null || description.GroupName == "v2",
                            _ => false,
                        };
    
                        //if (!description.TryGetMethodInfo(out MethodInfo methodInfo))
                        //    return false;
    
                        //var versions = methodInfo.DeclaringType
                        //    .GetCustomAttributes(true)
                        //    .OfType<ApiVersionAttribute>()
                        //    .SelectMany(attr => attr.Versions);
                        //return true;
                        //return versions.Any(v => $"v{v}" == docName);
                    });
                    options.IgnoreObsoleteActions();
                    options.IgnoreObsoleteProperties();
                    options.CustomSchemaIds(SwaggerCustomizations.GenerateSchemaId);
                    options.CustomOperationIds(SwaggerCustomizations.GenerateOperationId);
                });
        }
    
  • User Avatar
    0
    dipak.z created

    Thank you this is working, but i want two swagger ui in one there is my apis with JWT auth and in 2nd swagger there is default apis of abp, identity server etc there is no token required. for my apis default route /swagger for abp / identity server /admin/swagger

    private void ConfigureSwaggerServices(IServiceCollection services) { services.AddAbpSwaggerGen( options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "ImageProcessingAPI API", Version = "v1" }); options.SwaggerDoc("v2", new OpenApiInfo { Title = "ImageProcessingAPI API V2",Description="v2" , Version = "v2" }); options.DocInclusionPredicate((docName, description) => { return description.ActionDescriptor.IsControllerAction() && docName switch { "v2" => description.GroupName == null || description.GroupName == "v1", "v1" => description.GroupName == null || description.GroupName == "v2", _ => description.GroupName == null || description.GroupName == "v2", }; }); options.IgnoreObsoleteActions(); options.IgnoreObsoleteProperties(); options.CustomSchemaIds(type => type.FullName); } ); }

  • User Avatar
    0
    jfistelmann created

    You may refer to this:

    https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3354#issuecomment-393793398

    or this:

    https://github.com/domaindrivendev/Swashbuckle.AspNetCore#generate-multiple-swagger-documents

    you basically add some extra source to your OnApplicationInitialization with something like:

    app.UseSwaggerUI(options =>
    	{
    		options.SwaggerEndpoint("/swagger/v1/swagger.json", "AbpProjectName API V1");
    		options.SwaggerEndpoint("/swagger/v2/swagger.json", "AbpProjectName API V2");
    	}
    
  • User Avatar
    0
    dipak.z created

    this add versions in dropdown of swagger ui i want two swagger ui on diff routes like

    /admin/swagger /public/swagger

    like that

  • User Avatar
    0
    jfistelmann created

    this add versions in dropdown of swagger ui i want two swagger ui on diff routes like

    /admin/swagger /public/swagger

    like that

    can you try this? https://stackoverflow.com/a/77216365

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09