Skip to content

Commit

Permalink
serve openapi spec to allow download
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorwolf committed Sep 4, 2024
1 parent f368090 commit 5ca93dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 9 additions & 3 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ module.exports = (options={}, swaggerUiOptions={}) => {
cds.on('serving', service => {
const apiPath = options.basePath + service.path
const mount = apiPath.replace('$', '[\\$]')
const openapiPath = apiPath + '/openapi.json'
debug?.('serving Swagger UI for', { service: service.name, at: apiPath })

const uiOptions = Object.assign({ customSiteTitle: `${service.name} - Swagger UI` }, swaggerUiOptions)
const uiOptions = Object.assign({
customSiteTitle: `${service.name} - Swagger UI`,
swaggerUrl: openapiPath
}, swaggerUiOptions)
router.get(mount+'/openapi.json', function (req, res) {
res.json(toOpenApiDoc(service, options))
})
router.use(mount, (req, _, next) => {
req.swaggerDoc = toOpenApiDoc(service, options)
next()
}, swaggerUi.serveFiles(), swaggerUi.setup(null, uiOptions))
}, swaggerUi.serveFiles(null, uiOptions), swaggerUi.setup(null, uiOptions))

addLinkToIndexHtml(service, apiPath)
})
Expand Down
12 changes: 7 additions & 5 deletions tests/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe.only('Swagger UI', ()=>{
})
test('Diagram', async()=>{
const { data } = await GET `/$api-docs/browse/swagger-ui-init.js`
expect (data) .to.contain('yuml')
expect (data) .to.contain('swaggerUrl')
})
test('preview link in index.html', async()=>{
const { data } = await GET `/`
Expand All @@ -28,11 +28,13 @@ describe.only('Swagger UI', ()=>{
})

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')
let data = (await GET `/$api-docs/browse/openapi.json`).data
expect (data ) .to.be.a('object').to.have.property('openapi')
expect (data ) .to.be.a('object').to.have.property('x-sap-shortText', 'Service for namespace CatalogService')

data = (await GET `/$api-docs/admin/swagger-ui-init.js`).data
expect (data ) .to.be.a('string').that.contains('AdminService')
data = (await GET `/$api-docs/admin/openapi.json`).data
expect (data ) .to.be.a('object').to.have.property('openapi')
expect (data ) .to.be.a('object').to.have.property('x-sap-shortText', 'Service for namespace AdminService')
})

})
Expand Down

0 comments on commit 5ca93dc

Please sign in to comment.