Skip to content

Commit f3e4ce2

Browse files
committed
Generate fallback relative base urls for OAS3
1 parent 1741a99 commit f3e4ce2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/execute/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,18 @@ function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
286286
const computedScheme = stripNonAlpha(parsedUrl.protocol) || stripNonAlpha(parsedContextUrl.protocol) || ''
287287
const computedHost = parsedUrl.host || parsedContextUrl.host
288288
const computedPath = parsedUrl.pathname || ''
289+
let res
289290

290291
if (computedScheme && computedHost) {
291-
const res = `${computedScheme}://${computedHost + computedPath}`
292+
res = `${computedScheme}://${computedHost + computedPath}`
292293

293294
// If last character is '/', trim it off
294-
return res[res.length - 1] === '/' ? res.slice(0, -1) : res
295+
}
296+
else {
297+
res = computedPath
295298
}
296299

297-
return ''
300+
return res[res.length - 1] === '/' ? res.slice(0, -1) : res
298301
}
299302

300303
function getVariableTemplateNames(str) {

test/oas3/execute/main.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,23 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
497497

498498
expect(res).toEqual('http://google.com')
499499
})
500+
it('should create a relative url based on a relative server if no contextUrl is available', function () {
501+
const spec = {
502+
openapi: '3.0.0',
503+
servers: [
504+
{
505+
url: '/mypath'
506+
}
507+
]
508+
}
509+
510+
const res = baseUrl({
511+
spec,
512+
server: '/mypath'
513+
})
514+
515+
expect(res).toEqual('/mypath')
516+
})
500517
it('should return an empty string if no servers or contextUrl are provided', function () {
501518
const spec = {
502519
openapi: '3.0.0'

0 commit comments

Comments
 (0)