Search code examples
c#asp.net-coreswaggeropenapiswagger-codegen

ASP.NET Core & Swagger : generate multiple outputs


I'm using ASP.NET Core with Swashbuckle.AspNetCore.SwaggerGen in version v6.1.5.

I want routes like [HttpGet] or [HttpGet("user")] in a public swagger.json for public endpoints and other routes that contain internal inside the route like this [HttpGet("internal/user/{userGuid}")] to be in an internal instance of swagger.json endpoints.

I configure Swagger in startup in ConfigureServices like this:

services.AddSwaggerGen(gen =>
{
    gen.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "Server API",
        Version = "1.0",
        Description = "This API features all public available endpoints showing different API features."
    });
    gen.SwaggerDoc("v1-internal", new OpenApiInfo
    {
        Title = "Viewer Server API (internal)",
        Version = "v1-internal",
        Description = "This API features all public available endpoints showing different API features."
    });
});

and in Configure

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Viewer Server API v1");
    c.SwaggerEndpoint("/swagger/v1-internal/swagger.json", "Viewer Server API v1 (internal)");
});

How can I get it to create a public and an internal swagger.json?


Solution

  • Filtering different version or in my case "visibility-layer" can be done like the follwing. Configure Swagger in startup in ConfigureServices like this:

    services.AddSwaggerGen(gen =>
    {
        gen.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "Server API",
            Version = "1.0",
            Description = "This API features all public available endpoints showing different API features."
        });
        gen.SwaggerDoc("v1-internal", new OpenApiInfo
        {
            Title = "Viewer Server API (internal)",
            Version = "v1-internal",
            Description = "This API features all public available endpoints showing different API features."
        });
    
        gen.DocInclusionPredicate((docName, apiDesc) =>
        {
            if (docName.Contains("internal"))
            {
                return apiDesc.RelativePath.Contains("internal/");
            }
            else
            {
                return !apiDesc.RelativePath.Contains("internal/");
            }
        });
    });
    

    and in Configure

    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Viewer Server API v1");
        c.SwaggerEndpoint("/swagger/v1-internal/swagger.json", "Viewer Server API v1 (internal)");
    });
    

    And now you can choose different configurations like in the image below: enter image description here