Skip to content

[Bug]: AddServer only adds server to one API version #3306

@evochriso

Description

@evochriso

Describe the bug

I have 2 versions of my API so I'm using the following code to generate 2 swagger docs, one per version:

c.SwaggerDoc("v1", new OpenApiInfo
{
    Title = "API v1",
    Version = "v1",
    Description = "API v1"
});

c.SwaggerDoc("v2", new OpenApiInfo
{
    Title = "API v2",
    Version = "v2",
    Description = "API v2"
});

This works as expected and generates 2 different swagger docs, and Swagger UI correctly shows the different docs. When publishing the application, I need to add the server and authentication settings to the generated docs. I am using this code to add the server

c.AddServer(new OpenApiServer
{
    Url = server
});

and this code to add auth:

c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
    Type = SecuritySchemeType.OAuth2,
    In = ParameterLocation.Header,
    Flows = new OpenApiOAuthFlows
    {
        Implicit = new OpenApiOAuthFlow
        {
            AuthorizationUrl = Configuration.GetValue<Uri>(authUri),
            Scopes = new Dictionary<string, string> { { "user_impersonation", "Access API" } }
        }
    }
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{{
    new OpenApiSecurityScheme
    {
        Reference = new OpenApiReference
        {
            Type = ReferenceType.SecurityScheme,
            Id = "oauth2"
        },
        Scheme = "oauth2",
        Name = "oauth2",
        In = ParameterLocation.Header
    }, Array.Empty<string>()
}});

This produces the expected v1 doc with the server and auth scheme:

Image

However the v2 doc does not have the server or auth URL.

Image

Is this perhaps a bug, or is there additional config code I needed when generating multiple swagger docs for versioning?

Expected behavior

The current code should produce 2 docs with identical server and authentication info since the AddServer() function doesn't allow targeting a specific doc.

Swashbuckle.AspNetCore version

7.3.1

.NET Version

net8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp-wantedA change up for grabs for contributions from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions