Ocelot: Hide FileConfiguration and OutputCache controller from Swagger

Created on 14 Aug 2019  路  3Comments  路  Source: ThreeMammals/Ocelot

Expected Behavior / New Feature

I do not need to display FileConfigurationController and OutputCacheController in my swagger of API Geteway.

Actual Behavior / Motivation for New Feature

Screenshot_1

Steps to Reproduce the Problem

  1. Install-Package Ocelot -Version 13.5.2

How to hide/remove them?

Most helpful comment

I think this shouldn't be closed. Both solutions above work but are not ideal. In essence, unless necessary I think Ocelot should hide these controllers from being "discovered" by things like Swashbuckle.

I didn't have time to look into the code, but just adding [ApiExplorerSettings(IgnoreApi=true)] should do the job.

All 3 comments

Implement custom API Explorer like so:

public class FilteredApiExplorer : IApiExplorer
    {
        public Collection<ApiDescription> ApiDescriptions { get; }

        public FilteredApiExplorer(IApiExplorer explorer)
        {
            ApiDescriptions = new Collection<ApiDescription>(explorer.ApiDescriptions.Where(d => !d.ActionDescriptor.ControllerDescriptor.ControllerType.FullName.Contains("FileConfigurationController")).ToList());
        }
    }

Then replace the instance of IApiExplorer class in DI container with the custom one:

Services.Replace(typeof(IApiExplorer), new FilteredApiExplorer(apiExplorer));

Thanks @darcon77,
I know how to do it in swagger, but I hope that there are some setting in Ocelot for this.

For now I've solved it in this way

internal class HideOcelotControllersFilter : IDocumentFilter
    {
        private static readonly string[] _ignoredPaths = {
            "/configuration",
            "/outputcache/{region}"
        };

        public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
        {
            foreach(var ignorePath in _ignoredPaths)
            {
                swaggerDoc.Paths.Remove(ignorePath);
            }
        }
    }

then

services.AddSwaggerGen(cfg =>
   {
          cfg.DocumentFilter<HideOcelotControllersFilter>();
   });

it's tricky, I'm looking for another way

I think this shouldn't be closed. Both solutions above work but are not ideal. In essence, unless necessary I think Ocelot should hide these controllers from being "discovered" by things like Swashbuckle.

I didn't have time to look into the code, but just adding [ApiExplorerSettings(IgnoreApi=true)] should do the job.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FerAguilarR93 picture FerAguilarR93  路  3Comments

piyey picture piyey  路  6Comments

TomPallister picture TomPallister  路  4Comments

mogliang picture mogliang  路  3Comments

mkanakis picture mkanakis  路  3Comments