Hide Endpoints And Schemas from Swagger / OpenAPI

Swagger

I have a requirement to hide a few of the API’s controller endpoints from Swagger along with their respective schemas that aren’t yet ready for public consumption. I still want them to be available for Postman/Newman tests as part of the CI/CD. This can be achieved using the following Swashbuckle filter and MVC convention to successfully hide Swagger endpoints :-

MVC Convention to hide Controllers from Swagger

There’s more Controllers to hide than to show so it makes sense to explicitly specify the Controller names to generate documentation for. This could be improved by using Feature Toggles. My idea at the moment is that the visible controllers will get switched on as they pass testing and are ready for public consumption. Eventually this class will disappear.

Controller visibility

Its then just a case of adding the ActionModelConvention into the startup.cs……

Swagger document configuration

Swashbuckle Filter to hide Schemas

Similar to the Controllers above there are more schemas to hide than to show so I explicitly call out the ones to include in the generated Swagger/OpenAPI doc via a Swashbuckle SchemaFilter. Thanks to the discussion on GitHub….

Remove schemas from Swagger

Its then just a case of adding the filter into the Startup class….

There is the more fine grained approach of decorating each action to exclude with the following attribute…

[ApiExplorerSettings(IgnoreApi = true)]

Due to the amount of operations to exclude it made much more sense to go with omitting the actions by convention.

Leave a Reply