Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger-ui-dist does not export SwaggerUIBundle #9163

Closed
KostyaTretyak opened this issue Aug 23, 2023 · 4 comments
Closed

swagger-ui-dist does not export SwaggerUIBundle #9163

KostyaTretyak opened this issue Aug 23, 2023 · 4 comments

Comments

@KostyaTretyak
Copy link
Contributor

KostyaTretyak commented Aug 23, 2023

Q&A (please complete the following information)

  • OS: Ubuntu 22.04
  • Method of installation: npm
  • Swagger-UI version: swagger-ui-dist v5.4.2

Describe the bug you're encountering

The documentation says I can use SwaggerUIBundle, which is not actually exported from swagger-ui-dist v5.4.2.

To reproduce...

console.log(require('swagger-ui-dist'));

Outputs:

{
  absolutePath: [Function: getAbsoluteFSPath],
  getAbsoluteFSPath: [Function: getAbsoluteFSPath]
}

Expected behavior

Either the documentation needs to be updated or SwaggerUIBundle needs to be exported.

@oxypomme
Copy link

By checking the file (swagger-ui-dist-package/index.js), we can find :

try {
  module.exports.SwaggerUIBundle = require("./swagger-ui-bundle.js")
  module.exports.SwaggerUIStandalonePreset = require("./swagger-ui-standalone-preset.js")
} catch(e) {
  // swallow the error if there's a problem loading the assets.
  // allows this module to support providing the assets for browserish contexts,
  // without exploding in a Node context.
  //
  // see https:/swagger-api/swagger-ui/issues/3291#issuecomment-311195388
  // for more information.
}

So SwaggerUIBundle can be exported, but it depends if an error is throwed or not

@KostyaTretyak
Copy link
Contributor Author

So, as described in the comments of the "catch" block, this feature is intended for the browser and is apparently not available for Node.js. Therefore, it is better to write about it in the documentation, so that the developers do not think that it is a bug.

@KostyaTretyak
Copy link
Contributor Author

I'm not sure what I need exactly is SwaggerUIBundle. I want to change the default URL that swagger-ui-dist gives when loading (https://petstore.swagger.io/v2/swagger.json). Maybe you can tell me how to do it?

@char0n
Copy link
Member

char0n commented Aug 31, 2023

@KostyaTretyak,

You need to use Node.js >= 20.0.0 for successfully importing SwaggerUI with Node.js and have all exports available. As support for Node.js has been introduced in v5.4.2, this is the current limitation. If you need earlier versions of Node.js, you can polyfill the global File class. More info in here: char0n/swagger-ui-nextjs#1.

I'm not sure what I need exactly is SwaggerUIBundle. I want to change the default URL that swagger-ui-dist

It was possible before by appending ?url=<URL> to the URL where SwaggerUI runs. But it's no longer possible due to GHSA-qrmm-w75w-3wpx. In order to get around it, you need to use your own initializer:

<!-- HTML for dev server -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Swagger UI</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <style>

  </style>
</head>

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

  <script>
    window.onload = () => {
      const ui = SwaggerUIBundle({
        url: "https://petstore.swagger.io/v2/swagger.json",
        dom_id: "#swagger-ui",
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        plugins: [
          SwaggerUIBundle.plugins.DownloadUrl
        ],
        // requestSnippetsEnabled: true,
        layout: "StandaloneLayout",
        queryConfigEnabled: true, // enables the reading of URL params
      });
    };
  </script>

</body>

</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants