Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specify which API is documented by Swagger

I'm new to swagger and have it installed and running but it's picking up far more API files than desired. I have been hunting a way to specify which API is documented.

like image 589
user174634 Avatar asked Mar 03 '23 17:03

user174634


1 Answers

You can put an ApiExplorerSettings attribute on a controller to remove it from Swagger:

[ApiExplorerSettings(IgnoreApi = true)]
public class TestApiController : ControllerBase
{
}

If you want to apply this on a lot of controllers based on some logic, it can be done e.g. with an action model convention: https://github.com/juunas11/AspNetCoreHideRoutesFromSwagger/blob/983bad788755b4a81d2cce30f82bc28887b61924/HideRoutesFromSwagger/Controllers/SecondController.cs#L18-L28

public class ActionHidingConvention : IActionModelConvention
{
    public void Apply(ActionModel action)
    {
        // Replace with any logic you want
        if (action.Controller.ControllerName == "Second")
        {
            action.ApiExplorer.IsVisible = false;
        }
    }
}

The convention is added in ConfigureServices like:

services.AddControllers(o =>
{
    o.Conventions.Add(new ActionHidingConvention());
});
like image 151
juunas Avatar answered Mar 05 '23 15:03

juunas