Skip to content

Commit

Permalink
fix(swagger): Fallback to old swagger.json endpoint (#364)
Browse files Browse the repository at this point in the history
Previously, only a 404 on /openapi/v2 would cause a fallback
to the /swagger.json endpoint. Now, ALL error codes from /openapi/v2
cause a fallback to swagger.json
  • Loading branch information
ajpauwels authored and silasbw committed Nov 27, 2018
1 parent d17bb81 commit 42f3e5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ class Root extends Component {
return new Promise((resolve, reject) => {
return this.http.request('GET', { path: '/openapi/v2' }, (modernGETErr, modernGETRes) => {
if (modernGETErr) return reject(modernGETErr)
if (modernGETRes.statusCode === 404) {
if (modernGETRes.statusCode !== 200) {
return this.http.request('GET', { path: '/swagger.json' }, (legacyGETErr, legacyGETRes) => {
if (legacyGETErr) return reject(legacyGETErr)
if (legacyGETRes.statusCode !== 200) {
return reject(new Error(`Failed to get /swagger.json: ${legacyGETRes.statusCode}`))
if (modernGETRes.statusCode !== 404) {
return reject(new Error(`Failed to get /openapi/v2: ${modernGETRes.statusCode}`))
} else {
return reject(new Error(`Failed to get /swagger.json: ${legacyGETRes.statusCode}`))
}
}
this._addSpec(legacyGETRes.body)
return resolve(this)
})
} else if (modernGETRes.statusCode !== 200) {
return reject(new Error(`Failed to get /openapi/v2: ${modernGETRes.statusCode}`))
}
this._addSpec(modernGETRes.body)
return resolve(this)
Expand Down
13 changes: 12 additions & 1 deletion test/swagger-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ describe('lib.swagger-client', () => {
nock(common.api.url)
.get('/openapi/v2')
.reply(500, 'Internal Error')

nock(common.api.url)
.get('/swagger.json')
.reply(500, {
paths: {
'/api/': {
get: {
operationId: 'getCoreAPIVersions'
}
}
}
})
})

it('returns an error message with the status code', (done) => {
Expand All @@ -123,7 +135,6 @@ describe('lib.swagger-client', () => {
})
.catch(err => {
expect(err.message).to.equal('Failed to get /openapi/v2: 500')

done()
})
})
Expand Down

0 comments on commit 42f3e5d

Please sign in to comment.