Skip to content

Commit

Permalink
Feature: provide openapi download link (#144)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Christian Georgi <[email protected]>
  • Loading branch information
gregorwolf and chgeo authored Sep 20, 2024
1 parent f70d2dc commit 9b57d68
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.10.0 - tbd

### Added

- The OpenAPI spec is no available for download on `.../openapi.json` and linked in the UI.

## Version 0.9.0 - 2024-07-15

### Changed
Expand Down
12 changes: 8 additions & 4 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ 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)
router.use(mount, (req, _, next) => {
req.swaggerDoc = toOpenApiDoc(service, options)
const uiOptions = Object.assign({
customSiteTitle: `${service.name} - Swagger UI`,
swaggerUrl: openapiPath
}, swaggerUiOptions)
router.get(mount + '/openapi.json', (_, res) => res.json(toOpenApiDoc(service, options)))
router.use(mount, (_, _1, next) => {
next()
}, swaggerUi.serveFiles(), swaggerUi.setup(null, uiOptions))
}, swaggerUi.serveFiles(null, uiOptions), swaggerUi.setup(null, uiOptions))

addLinkToIndexHtml(service, apiPath)
})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cds-swagger-ui-express",
"version": "0.9.0",
"version": "0.10.0",
"description": "Swagger UI for CAP Services",
"main": "index.js",
"repository": "github:chgeo/cds-swagger-ui-express",
Expand Down
4 changes: 2 additions & 2 deletions tests/server-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('with options', ()=>{
})

test('Diagram', async()=>{
const { data } = await GET `/test-base/browse/swagger-ui-init.js`
expect (data) .not.to.contain('yuml')
const { data } = await GET `/test-base/browse/openapi.json`
expect (JSON.stringify(data)) .not.to.contain('yuml')
})

test('Swagger UI options', async()=>{
Expand Down
16 changes: 10 additions & 6 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,15 @@ 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')

data = (await GET `/$api-docs/admin/swagger-ui-init.js`).data
expect (data ) .to.be.a('string').that.contains('AdminService')
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')
expect (JSON.stringify(data)) .to.contain('yuml')

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')
expect (JSON.stringify(data)) .to.contain('yuml')
})

})
Expand Down

0 comments on commit 9b57d68

Please sign in to comment.