fastify / fastify-swagger-ui

Serve Swagger-UI for Fastify
MIT License
125 stars 38 forks source link

"Unable to render this definition" on refreshing the page #158

Closed blue86321 closed 2 weeks ago

blue86321 commented 2 weeks ago

Prerequisites

Fastify version

4.26.1

Plugin version

4.0.0

Node.js version

20.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.5

Description

In swagger docs, when unfolding the api difinitions, browser location (url) will change.

At this point, if we refresh the page, we will get error Unable to render this definition

It is because we find the difinition by using the current url and attach ./json, e.g. http://127.0.0.1:3000/api/docs#/auth/post_v1_auth_login/./json

image

To resolve this bug, the most straightforward way is to modify the swagger-initialzer.js (but this solution doesn't handle the corner case where the user's url contains #)

function resolveUrl(url) {
      var currentHref = window.location.href;
>     currentHref = currentHref.split('#')[0];
      currentHref = currentHref.endsWith('/') ? currentHref : currentHref + '/';
      var anchor = document.createElement('a');
      anchor.href = currentHref + url;
      return anchor.href
    }

A better way is to make it unchanged even we refresh the page, not sure if it is a big change or not. What do you think?

Link to code that reproduces the bug

No response

Expected Behavior

unfolding the API difinition, then refresh the browser, the docs stay the same instead of showing Unable to render this definition

mcollina commented 2 weeks ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

blue86321 commented 2 weeks ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Done. I use the initial suggestion to solve it. Seems like a hash # won't show up in a normal domain / url since it represents an anchor.

The PR is attached #160. Let me know if there is anything I can do.