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

Configure swagger-ui-dist URL server side #5710

Open
2 tasks
gbertoncelli opened this issue Nov 12, 2019 · 6 comments
Open
2 tasks

Configure swagger-ui-dist URL server side #5710

gbertoncelli opened this issue Nov 12, 2019 · 6 comments

Comments

@gbertoncelli
Copy link

gbertoncelli commented Nov 12, 2019

Is your feature request related to a problem?

This issue is related to the #4624 and the swagger-ui-dist library. There is no way to change the url on the server side without modifying the node_module folders or without having an endpoint that accept requests for the index.html and that serves the modified index. Both this solution are not acceptable, in fact for the first one it's clearly a bad practice to change the node_modules folder content on runtime and for the second I cannot replace an example URL that I have no certainty it will be constant in each update.

Describe the solution you'd like

I do not know a clear solution for this problem. Maybe the only way is indeed to modify the content of the index.html but at least I was expecting that there will be a function to do that inside the library. Maybe this is the simplest solution.

Describe alternatives you've considered

  • Save index.html content and serve it to the index.html endpoint (not so usable without Express or similar)
  • On application startup replace https://petstore.swagger.io/v2/swagger.json with my JSON swagger configuration endpoint.
@vsuharnikov
Copy link

Another alternative is to store the configuration in a separate file, e.g. swagger-conf.json and load it through a script with defer attribute. We still have an example file, but it is easy to provide own swagger-conf.json on nginx/express/etc

@amadeus-torwell
Copy link

amadeus-torwell commented Apr 6, 2020

You could also redirect to the ?url= query paramenter:

import swaggerUI from 'swagger-ui-dist'
app.get('/docs', (req, res, next) => {
  if (req.url === '/docs/') {
    return res.redirect('/docs/?url=/api/swagger')
  }
  next();
});
app.use('/docs', express.static(swaggerUI.getAbsoluteFSPath()));

@gbertoncelli
Copy link
Author

Yes but they are all workarounds...

@gonenduk
Copy link
Contributor

@deusama
That was my workaround but it stopped working with version 4.1.3.
Reading the url as a query parameter is blocked now.

@painpita
Copy link

Here is my solution for anyone wondering :

Create your own swagger-initializer.js file with the correct API descriptor and replace the script in the swagger index.html file :

const fs = require("fs")

const indexContent = fs.readFileSync(`${pathToSwaggerUi}/index.html`)
  .toString()
  .replace("./swagger-initializer.js", "http://localhost/swagger-initializer.js")


app.get("/", (req, res) => res.send(indexContent))
app.get("/index.html", (req, res) => res.send(indexContent)) // you need to do this since the line below serves `index.html` at both routes

app.use(express.static(pathToSwaggerUi))

@WeetA34
Copy link

WeetA34 commented Aug 4, 2022

or

const express = require('express')
const fs = require('fs')
const path = require('path')
const pathToSwaggerUi = require('swagger-ui-dist').getAbsoluteFSPath()

const indexContent = fs.readFileSync(path.join(pathToSwaggerUi, 'swagger-initializer.js'))
    .toString()
    .replace('https://petstore.swagger.io/v2/swagger.json', '/path/to/swagger.json')

const app = express()
app.get('/swagger-initializer.js', (req, res) => res.send(indexContent))
app.use('/path/to/swagger.json', express.static(path.join(__dirname__, 'swagger.json')))
app.use(express.static(pathToSwaggerUi))

app.listen(8080)

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

No branches or pull requests

6 participants