Hiding Controller And Schemas from Swagger/OpenAPI documentation


Had a scenario where I need to hide some of the API’s controller endpoints from Swagger along with their respective schemas that aren’t yet ready for public consumption. I still wanted them to be available for Postman/Newman tests as part of the CI/CD. Manged to achieve this using the following Swashbuckle filter and MVC convention:-

MVC Convention to hide Controllers

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.

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

Swashbuckle Filter to hide Schemas

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

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

%d bloggers like this: