From 7d88de3bbd9604fb4e794bdb97bc70400a7f0431 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Thu, 23 Feb 2023 15:01:10 +0100 Subject: [PATCH 1/3] Do not execute `run.js` in tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 98f31e7..f29fbac 100644 --- a/package.json +++ b/package.json @@ -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 " From 0f37a59b917fc91da5ffd8866501d75831fd2aa8 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Thu, 23 Feb 2023 15:15:44 +0100 Subject: [PATCH 2/3] Fix `serveFiles` to work w/ dynamic swaggerDoc `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()); // ^^^ ``` --- index.js | 3 ++- test/test.spec.js | 9 +++++++++ test/testapp/app.js | 16 ++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index f4998f1..d05479a 100644 --- a/index.js +++ b/index.js @@ -139,7 +139,7 @@ window.onload = function() { const authorized = ui.preauthorizeApiKey(key, value); if(!!authorized) clearInterval(pid); }, 500) - + } } @@ -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') diff --git a/test/test.spec.js b/test/test.spec.js index fbecefd..b35fb9e 100644 --- a/test/test.spec.js +++ b/test/test.spec.js @@ -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]+/); + }); + }); \ No newline at end of file diff --git a/test/testapp/app.js b/test/testapp/app.js index 33d9e38..0907cba 100644 --- a/test/testapp/app.js +++ b/test/testapp/app.js @@ -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, @@ -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' From 462cd4f7e7f4da885d31f8e5774b34bad0edfd32 Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Thu, 23 Feb 2023 15:29:15 +0100 Subject: [PATCH 3/3] Enhance docs --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ee12244..2362ad4 100644 --- a/README.md +++ b/README.md @@ -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