Repository navigation
Replies: 2 comments
|
I think there are two separate concerns here: controller discovery and API documentation. A ControllerFeatureProvider controls which controllers MVC discovers, but if your OpenAPI generation is using a separate source of endpoint metadata or a custom discovery mechanism, you may need to filter the API descriptions as well. One approach is to use an For example: public class HideInternalControllersConvention
: IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
if (controller.ControllerName.StartsWith("Internal"))
{
controller.ApiExplorer.IsVisible = false;
foreach (var action in controller.Actions)
{
action.ApiExplorer.IsVisible = false;
}
}
}
}
}Register it through MVC options: builder.Services.AddControllers(options =>
{
options.Conventions.Add(
new HideInternalControllersConvention());
});This keeps the filtering logic outside the controllers themselves. One thing worth checking: which OpenAPI generator are you using ( Also, hiding an endpoint from ApiExplorer only affects documentation; it does not replace the actual endpoint exclusion you already implemented. |
|
"That makes a lot of sense. Since controller discovery and API documentation are two separate concerns, it's clear why a ControllerFeatureProvider on its own doesn't automatically stop those controllers from showing up in the OpenAPI docs, meaning a separate filter for the API descriptions is needed." |
Uh oh!
There was an error while loading. Please reload this page.
I'm using a custom ControllerFeatureProvider to exclude certain controllers from my API (and I'd like to go further and be able to exclude certain endpoints from a controller but not worked out a way to do that yet!). This works and the excluded controllers are not reachable. However I've noticed that the OpenApi docs that are generated still include the excluded controllers. Surely the OpenApi stuff should respect the ControllerFeatureProvider? If not, for whatever reason, is there a way to exclude controllers/endpoints from the OpenApi docs without using attributes/touching the actual controller code?
All reactions