Skip to content

Allow injecting additional SwaggerUIBundle config (e.g. requestInterceptor) into the generated index.html #497

@123yoshidandy

Description

@123yoshidandy

Summary

It would be very helpful if this action exposed a way to inject additional
SwaggerUIBundle({...}) configuration into the generated index.html.

Today, the template at resources/index.html hard-codes the bundle call to:

const ui = SwaggerUIBundle({
  configUrl: "<swaggerConfig>",
  dom_id: '#swagger-ui'
})

Users can already influence parts of the runtime config via swagger-config.json,
but that file is JSON and therefore cannot carry function-valued options such as
requestInterceptor, responseInterceptor, onComplete, plugins, etc. These
options can only live inside the inline SwaggerUIBundle({...}) call site, which
this action does not currently expose.

Motivating use case

In our team, we use Try It Out → Execute primarily to obtain the rendered
curl command for the request, since the spec's server URL is behind a VPN /
IP allow-list and the real call is made from a different environment. We do
not need to wait for a response.

However, Execute has no built-in request timeout, so the UI hangs until the
browser/network eventually aborts the request, which in our environment is on
the order of tens of seconds, even though we already have the information we
wanted.

A requestInterceptor that attaches an AbortSignal.timeout(...) fixes this:

requestInterceptor: function (req) {
  req.signal = AbortSignal.timeout(3000);
  return req;
}

Because this is a function, it cannot be expressed via swagger-config.json and
must be injected into the inline SwaggerUIBundle({...}) call.

Proposed solution

Add a new optional input — e.g. extra-config — whose value is a JS fragment
appended inside the SwaggerUIBundle({...}) object literal in the generated
index.html. Implementation-wise this can be a new <extraConfig> placeholder
in resources/index.html, populated by the existing sed-based templating in
createIndexHtml.

Template:

const ui = SwaggerUIBundle({
  configUrl: "<swaggerConfig>",
  dom_id: '#swagger-ui',
  <extraConfig>
})

Example usage:

- uses: Legion2/swagger-ui-action@v1
  with:
    output: swagger-ui
    spec-file: openapi.yaml
    extra-config: |
      requestInterceptor: function (req) {
        req.signal = AbortSignal.timeout(3000);
        return req;
      }

Benefits:

  • Solves the timeout use case above.
  • Generalizes to other long-standing needs that require function values
    (responseInterceptor, custom plugins, onComplete, etc.) without
    adding a new input per option.
  • Minimal change: one new optional input, one placeholder in the template,
    no behavior change when the input is not set.

When the input is empty, the trailing comma in the template can be handled
either by emitting it conditionally from the action code or by relying on
JS's tolerance for trailing commas in object literals.

Alternative considered

A narrower input such as request-timeout: 3000 that only handles this one
case. It is simpler, but every future function-valued config option would
require yet another input, so the generalized extra-config approach feels
strictly better.

Current workaround

For now, we post-process the generated file in our workflow with sed:

- name: Add 3s timeout to Swagger UI Try It Out requests
  run: |
    sed -i "s/dom_id: '#swagger-ui'/dom_id: '#swagger-ui', requestInterceptor: function(req) { req.signal = AbortSignal.timeout(3000); return req; }/" docs/swagger-ui/index.html

This works but is fragile against future template changes in this action and
is not something we'd recommend other users copy.

Willingness to contribute

Happy to send a PR if the maintainers agree with the extra-config direction.
Please let me know which input name / shape you'd prefer before I start.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions