New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exclude controllers methods from docs without using the Obsolete attribute #90 #153
Comments
Swashbuckle is built on top of WebApi's built-in metadata layer - ApiExplorer. If you decorate a controller or action with the following attribute:
then this will ultimately cause the entire controller or individual action to be omitted from the Swagger output |
I have a scenario where I have a BaseController which each API inherits. I want to hide this BaseController. The moment I put the below line all controllers are hidden from the documentation. How do we handle this? |
+1 for hiding just the BaseController. |
I've managed to do this by creating custom attribute: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class HideInDocsAttribute:Attribute
{
} and custom IDocumentFilter: public class HideInDocsFilter:IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
foreach (var apiDescription in apiExplorer.ApiDescriptions)
{
if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any() && !apiDescription.ActionDescriptor.GetCustomAttributes<HideInDocsAttribute>().Any()) continue;
var route = "/" + apiDescription.Route.RouteTemplate.TrimEnd('/');
swaggerDoc.paths.Remove(route);
}
}
} this way every method that has [HideInDocs] attribute will be excluded. EDIT: You can apply this attribute to Controller and have all it methods excluded. |
@Misiu 👍 |
Hello, This doesnt work for me. apiDescription.Route.RouteTemplate is always api/{controller}/{id} Therefore, it can not remove it. How do I fix it? I dont want to manually define a new routemap for each controller. Thank you. |
If it was possible to do the opposite from defaults - include only controllers that are decorated with specific attribute. When app has multiple controllers that are not needed in docs and only some that are needed. |
@SlyNet |
@SlyNet private class ApplyDocumentVendorExtensions : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
var paths = new Dictionary<string, PathItem>(swaggerDoc.paths);
swaggerDoc.paths.Clear();
foreach (var path in paths)
{
if (path.Key.Contains("foo"))
swaggerDoc.paths.Add(path);
}
}
} |
For someone that comes across this and is using ODataControllers and not API Controller, I had to update the assigning of route as below. This might also work for the problem @ahmettahasakar ran into.
|
I did something a little different for OData. The solution above was removing more than one method of the controller, often a GET and POST at the same time. I adjusted this a bit so it'll work on the methods it's attached to. If there is a more condensed version that someone can think of, that'll be great. Would update this more to check to see if all methods in the controller are being ignored, then remove the path.
|
Why isn't there just a simple attribute to SKIP a method? I feel like Swashbuckle is a lot harder to use than it needs to be; everything requires building your own document filter when the most common primitives could have been put in the box |
@Cloudmersive Don't forget Swashbuckle is OpenSource! |
Did you read the first response to this issue? What’s your problem with the ApiExplorerSettings attribute? |
Might have missed the point that it can be added to individual actions @domaindrivendev ? |
To hide a custom BaseController, you can make it abstract.
|
@rvdb2 abstract class doesn't hide the an custom controller. I'm trying in Azure Service Fabric ASP.NET Core Stateless service. |
@waqasdilawar I retried it using a new Web API project in VS2015, with 2 empty controllers: If I run the project in VS2015 (using IIS), CustomBaseController.Get is unaccessible and hidden in documentation, while the inherited CustomController.Get is accessible and visible. If I make CustomBaseController non-abstract, its Get method becomes accessible and visible. Hopefully this makes it reproducible for you. |
@Misiu 👍
otherwise RouteTemplate is always
|
Here is a sample ShowInSwagger implementation: using System.Web.Http;
To use just add the [ShowInSwagger] attribute to the controller class or method. |
Improvement to take verb (GET/POST/etc) into consideration: `public class HideInDocsFilter : IDocumentFilter
|
Use |
I had the same issue. Set your BaseController access modifier to "Protected" instead of "Public". This will hide the Controller showing up in Swagger. It worked for me. |
I had similar issue, where i want to show only 1 controller in documentation out of 80+ Controller with 400+ action methods. |
You can add the following attribute to Controllers and Actions to exclude them from the generated documentation: |
If i want to Individual Method in Controller instead of whole Controller Ignore what can I do?
The text was updated successfully, but these errors were encountered: