Skip to content

Commit

Permalink
Fix serveFiles to work w/ dynamic swaggerDoc
Browse files Browse the repository at this point in the history
`serveFiles` did not respect on-the-fly changes in a swagger doc,
but rendered the old content.

The fix now also enables this pattern, where `serveFiles` doesn't need
the swaggerDoc upfront (which is desired in dynamic cases):

```js
app.use('/api-docs-dynamic', function(req, res, next){
  swaggerDocument.info.description = `Hello ${count}!`;
  req.swaggerDoc = swaggerDocument;
  next();
}, swaggerUi.serveFiles(), swaggerUi.setup());
//                     ^^^
```
  • Loading branch information
chgeo committed Feb 23, 2023
1 parent 7d88de3 commit 0f37a59
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ window.onload = function() {
const authorized = ui.preauthorizeApiKey(key, value);
if(!!authorized) clearInterval(pid);
}, 500)
}
}
Expand Down Expand Up @@ -254,6 +254,7 @@ var swaggerInitFunction = function (swaggerDoc, opts) {
res.sendStatus(404)
} else if (trimQuery(req.url).endsWith('/swagger-ui-init.js')) {
if (req.swaggerDoc) {
opts.swaggerDoc = req.swaggerDoc
swaggerInitFile = jsTplString.toString().replace('<% swaggerOptions %>', stringify(opts))
}
res.set('Content-Type', 'application/javascript')
Expand Down
9 changes: 9 additions & 0 deletions test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,13 @@ describe('integration', function() {
assert(!classes.includes('unlocked'));
});

it('should have API Documentation hosted at /api-docs-dynamic', async function() {
const httpResponse = await sitepage.goto('http://localhost:3001/api-docs-dynamic/');
assert.ok(httpResponse.ok());
const html = await sitepage.evaluate(() => document.querySelector('.swagger-ui').innerHTML);
assert.ok(html);
console.log(html);
assert.match(html, /Hello [\d]+/);
});

});
16 changes: 8 additions & 8 deletions test/testapp/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ var swaggerHtml = swaggerUi.generateHTML(swaggerDocument, swaggerUiOpts)
app.use('/api-docs-html1', swaggerUi.serveFiles(swaggerDocument, swaggerUiOpts))
app.get('/api-docs-html1', (req, res) => { res.send(swaggerHtml) });

let count = 0
app.use('/api-docs-dynamic', function(req, res, next){
count = count + 1
swaggerDocument.info.description = `Hello ${count}!`;
req.swaggerDoc = swaggerDocument;
next();
}, swaggerUi.serveFiles(), swaggerUi.setup());

var swaggerUiOpts3 = {
explorer: false,
swaggerOptions: options,
Expand All @@ -113,14 +121,6 @@ var swaggerUiOpts3 = {
app.use('/api-docs-jsstr', swaggerUi.serve)
app.get('/api-docs-jsstr', swaggerUi.setup(null, swaggerUiOpts3));

// let count = 0
// app.use('/api-docs-dynamic', function(req, res, next){
// count = count + 1
// swaggerDocument.info.description = `Hello ${count}!`;
// req.swaggerDoc = swaggerDocument;
// next();
// }, swaggerUi.serveFiles(swaggerDocument, options), swaggerUi.setup());

var swaggerUiOpts4 = {
swaggerOptions: {
url: 'http://localhost:' + (app.get('port') || 3001) + '/swagger.json'
Expand Down

0 comments on commit 0f37a59

Please sign in to comment.