Custom swagger-ui filter plugin (opsFilter) in a .NET Core app

276 Views Asked by At

I'm trying to provide my custom logic for the opsFilter.

My app is a Microsoft.NET.Sdk.Web targeting net7.0.

app.UseSwaggerUI(o =>
  {
      o.SwaggerEndpoint("v1/swagger.json", "MyApplication");
      o.EnableTryItOutByDefault();
      o.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None);
      o.EnableDeepLinking();
      o.EnableFilter();

      // custom filter as the default from EnableFilter() only filters on tags and is case sensitive
      o.InjectJavascript("/js/custom-swagger-ui-filter.js");
  });

I am starting with a CaseInsensitive filter for my custom js (from this issue)

const CaseInsensitiveFilterPlugin = function (system) {
    return {
        fn: {
            opsFilter: (taggedOps, phrase) => {
                console.log("hello " + phrase)
                return taggedOps.filter((tagObj, tag) => tag.toLowerCase().indexOf(phrase.toLowerCase()) !== -1);
            }
        }
    }
};

document.addEventListener("load", function () {
    console.log("loading custom filter!")
    window.SwaggerUIBundle({
        plugins: [
            CaseInsensitiveFilterPlugin
        ],
    });
});

yields this html (truncated to focus on the issue)

</head>
...
<script src='/js/custom-swagger-ui-filter.js' type='text/javascript'></script>
...
</head>

<body>
    <div id="swagger-ui"></div>

    ...

    <script src="./swagger-ui-bundle.js"></script>
    <script src="./swagger-ui-standalone-preset.js"></script>
    ...
</body>

The plugin function does not get invoked so the default filter is still applied when I search. I don't see my plugin when I inspect the window.SwaggerUiBundle.plugins object.

Apart from attempting to override the whole index.html to ensure my script is loaded after swagger-ui-bundle.js, do you have any idea how to achieve this ?

Thanks a bunch!

  • Tried manually setting the filter in the console of the browser after all the index page was loaded, just for a proof of concept, but it still was not overridden
0

There are 0 best solutions below