79604070

Date: 2025-05-02 20:27:21
Score: 0.5
Natty:
Report link

I am writing the answer to reply to @ThomasLedan.

As mentioned I had to override the index.html:

// 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);
            }
        }
    };
};

...
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");
});

SwaggerUI-filters-demo

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ThomasLedan
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: PhoenixAshes