Skip to content

MoondanceZ/Yarkool.SwaggerUI

Repository files navigation

Yarkool.SwaggerUI

image

//Add swagger
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "API V1", Version = "v1" });
    options.AddServer(new OpenApiServer()
    {
        Url = "",
        Description = "Description"
    });
    options.CustomOperationIds(apiDesc =>
    {
        var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
        return controllerAction!.ControllerName + "-" + controllerAction.ActionName;
    });

    foreach (var file in Directory.GetFiles(AppContext.BaseDirectory, "*.xml"))
    {
        options.IncludeXmlComments(file);
    }

    var securityScheme = new OpenApiSecurityScheme
    {
        Description = "Format: Bearer {access_token}",
        Name = "Authorization",
        In = ParameterLocation.Header,
        Scheme = "Bearer",
        Type = SecuritySchemeType.ApiKey
    };

    options.AddSecurityDefinition("Bearer", securityScheme);

    options.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Id = "Bearer", //The name of the previously defined security scheme.
                    Type = ReferenceType.SecurityScheme
                }
            },
            Array.Empty<string>()
        }
    });
});
app.UseYarkoolSwaggerUI(c =>
{
    c.SwaggerEndpoint("/v1/swagger.json", "V1 Docs");
});

app.UseRouting().UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

Releases

No releases published

Packages

No packages published

Languages