Reputation: 179
is there any way that some services can be hidden from swagger UI when going into production but when run on localhost be displayed?
EX:
/// <summary>
/// GET: .../api/SomeController/{id: int}
/// </summary>
/// <param name="id">int</param>
/// <returns></returns>
public IHttpActionResult SomeService(int id) { return Ok();}
When i'm running on my environment and go to swagger UI localhost:12345/swagger/ui/index i will see that service documentation etc, but when i'm on http://someDomain/swagger/ui/index i will not see it.
I've been looking in the documentation but didn't find anything related to this.
Thank you.
Upvotes: 0
Views: 848
Reputation: 5719
You can create your own attribute and use it to exclude that method or whole controller from swagger:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public class HideInDocsAttribute:Attribute
{
}
And then:
public class HideInDocsFilter:IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer, IHostingEnvironment env)
{
if(env.IsEnvironment("Production")) {
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);
}
}
}
}
Upvotes: 3