Skip to content

Commit

Permalink
Merge pull request #336 from chgeo/master
Browse files Browse the repository at this point in the history
Fix `serveFiles` to work w/ dynamic swaggerDoc
  • Loading branch information
scottie1984 committed Feb 27, 2023
2 parents 98f8eb3 + 462cd4f commit 78c4180
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ var options = {}
app.use('/api-docs-one', swaggerUi.serveFiles(swaggerDocumentOne, options), swaggerUi.setup(swaggerDocumentOne));

app.use('/api-docs-two', swaggerUi.serveFiles(swaggerDocumentTwo, options), swaggerUi.setup(swaggerDocumentTwo));

app.use('/api-docs-dynamic', function(req, res, next){
req.swaggerDoc = swaggerDocument;
next();
}, swaggerUi.serveFiles(), swaggerUi.setup());
```

### Link to Swagger document
Expand Down
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"LICENSE"
],
"scripts": {
"test": "./node_modules/.bin/mocha test/*",
"test": "./node_modules/.bin/mocha test/*.spec.js",
"coverage:report": "nyc ./node_modules/.bin/mocha test/* || exit 0",
"coverage:badge": "npm run coverage:report && istanbul-badges-readme --coverageDir='./coverage'",
"test-app": "node ./test/testapp/run.js "
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 78c4180

Please sign in to comment.