scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.41k stars 225 forks source link

[DOCS] Incorrect example in docs for modify swagger file on the fly before load #318

Closed Avm07 closed 1 year ago

Avm07 commented 1 year ago

Incorrect example in readme for modifying swagger file on the fly before load:

const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

var options = {}

app.use('/api-docs', function(req, res, next){
    swaggerDocument.servers[0].url = req.get('host');
    req.swaggerDoc = swaggerDocument;
    next();
}, swaggerUi.serveFiles(swaggerDocument, options), swaggerUi.setup());

^^^ This does not work for me then I try to change

swaggerDocument.servers[0].url = req.get('host')

Correct example, which works:

const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

app.use('/api-docs', function(req, res, next){
    swaggerDocument.servers[0].url = req.get('host');
    req.swaggerDoc = swaggerDocument;
    next();
}, swaggerUi.serve, swaggerUi.setup());
scottie1984 commented 1 year ago

I haven't been able to replicate any issue with the docs. I don't see anything in particular as to why it might not have worked.

Avm07 commented 1 year ago

I haven't been able to replicate any issue with the docs. I don't see anything in particular as to why it might not have worked.

Try to see updates in the browser after the doc is loaded and updated on the fly. It will render the old version without updates, while logging will show that the document updated correctly.

chgeo commented 1 year ago

See https://github.com/scottie1984/swagger-ui-express/pull/336, which should fix this issue. It's not a documentation issue, though.