Skip to content

Commit

Permalink
Serve multiple services w/o race conditions (#49)
Browse files Browse the repository at this point in the history
Use swagger-ui-express's `serveFiles` instead of `serve` for this.
Otherwise requests for different services will yield the same
`swagger-ui-init.js` (with the wrong swagger spec).

Requires `swagger-ui-express` 4.6.2
  • Loading branch information
chgeo authored Feb 27, 2023
1 parent 54ca368 commit daa53d4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
- The default site title is now _\<Service Name\> - Swagger UI_ (instead of just _Swagger UI_)
- REST services are now available as well in the Swagger UI. Previously, only OData services were allowed. This was to avoid potential issues as the underlying OpenAPI transformation uses OData as intermediate protocol. So, as long as we don't see issues, allow REST services as well.

### Fixed

- Multiple services are now properly served, w/o the previous stale content of `swagger-ui-init.js`. Requires `swagger-ui-express` >= 4.6.2

## Version 0.5.0 - 2022-06-09

### Added
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = (options={}, swaggerUiOptions={}) => {
router.use(mount, (req, _, next) => {
req.swaggerDoc = toOpenApiDoc(service, options)
next()
}, swaggerUi.serve, swaggerUi.setup(null, uiOptions))
}, swaggerUi.serveFiles(), swaggerUi.setup(null, uiOptions))

addLinkToIndexHtml(service, apiPath)
})
return router
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"dependencies": {
"@sap/cds-dk": ">=4.9",
"swagger-ui-express": "^4"
"swagger-ui-express": "^4.6.2"
},
"peerDependencies": {
"@sap/cds": ">=5",
Expand Down
8 changes: 8 additions & 0 deletions tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ describe('Swagger UI', ()=>{
expect (data) .match (/Show in Swagger UI/i)
})

test('multiple services', async()=>{
let data = (await GET `/$api-docs/browse/swagger-ui-init.js`).data
expect (data ) .to.be.a('string').that.contains('CatalogService')

data = (await GET `/$api-docs/admin/swagger-ui-init.js`).data
expect (data ) .to.be.a('string').that.contains('AdminService')
})

})

0 comments on commit daa53d4

Please sign in to comment.