I am writing the answer to reply to @ThomasLedan.
As mentioned I had to override the index.html
:
In a Swagger
folder in my project, I added index.html
as Embdedded Resource to my project, from Swashbuckle. I did this a while back and now they have split the index.html
to multiple files, so adjustments are needed to include them in your project, or use the version I am still using.
index.html
:
...
configObject.layout = "StandaloneLayout";
// My Custom Code
configObject.plugins = [
SwaggerUIBundle.plugins.DownloadUrl,
AdvancedFilterPlugin
];
// End My Custom Code
// Parse and add interceptor functions
var interceptors = JSON.parse('%(Interceptors)');
...
Added /js/custom-swagger-ui-filter.js
under wwwroot
in my porject
// Originally from https://github.com/swagger-api/swagger-ui/issues/3876#issuecomment-650697211, refactored slightly
const AdvancedFilterPlugin = function (system) {
return {
fn: {
opsFilter: function (taggedOps, phrase) {
phrase = phrase.toLowerCase();
var normalTaggedOps = JSON.parse(JSON.stringify(taggedOps));
for (const [tagObj, value] of Object.entries(normalTaggedOps)) {
const operations = value.operations;
let i = operations.length;
while (i--) {
const operation = operations[i].operation;
const parameters = (operation.parameters || []).map(param => JSON.stringify(param)).join('').toLowerCase();
const responses = (operation.responses || {}).toString().toLowerCase();
const requestBody = (operation.requestBody || {}).toString().toLowerCase();
if (
operations[i].path.toLowerCase().includes(phrase) ||
(operation.summary && operation.summary.toLowerCase().includes(phrase)) ||
(operation.description && operation.description.toLowerCase().includes(phrase)) ||
parameters.includes(phrase) ||
responses.includes(phrase) ||
requestBody.includes(phrase)
) {
// Do nothing
} else {
operations.splice(i, 1);
}
}
if (operations.length === 0) {
delete normalTaggedOps[tagObj];
} else {
normalTaggedOps[tagObj].operations = operations;
}
}
return system.Im.fromJS(normalTaggedOps);
}
}
};
};
Startup.cs
or Program.cs
:...
app.UseSwaggerUI(c =>
{
...
// custom filter as the default from EnableFilter() only filters on tags and is case sensitive
c.EnableFilter();
c.InjectJavascript("/js/custom-swagger-ui-filter.js");
var assembly = GetType().Assembly;
c.IndexStream = () => assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Swagger.index.html");
});