Base solution for your next web application
Open Closed

Swagger UI hiding endpoints #11617


User avatar
0
conorcorr created

We are using ASPNet Zero (Angular + .Net Core) v11.1

We have tried to hide endpoints from Swagger by using [ApiExplorerSettings(IgnoreApi = true)], [RemoteService(false)] attributes. They do remove the endpoints from swagger but the enpoints themselves no longer exist while calling via the appservices or methods from typscript. We also tried creating a custom attribute [ShowSwagger] but ended up with the same problem.

Another possible solution:

We would like the swagger/v1/swagger.json endpoint to behave differently depending on whether we are in development or production. When in development, we want all endpoints to be visible so that NSwag can build the service proxies correctly. When in production, we only want the customer-facing endpoints visible. It's possible that this can be achieved with a custom DocumentFilter?

Thank you.


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @conorcorr

    Yes, you can create a custom DocumentFilter and achieve this. A sample document filter would be something like this;

    public class SwaggerDocumentFilter : IDocumentFilter
    {
    	public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
    	{
    		swaggerDoc.Paths
    			.Where(x =>
    				x.Key.Contains("/part-of-endpoint") ||
    			)
    			.ToList()
    			.ForEach(x => swaggerDoc.Paths.Remove(x.Key));
    	}
    }