Skip to content

Commit a46c4fa

Browse files
authored
Merge pull request #1166 from shockey/bug/baseurl-relative-fallback
Generate relative base url if we can't make an absolute one
2 parents 004c72a + f3e4ce2 commit a46c4fa

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/execute/index.js

Lines changed: 15 additions & 8 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) {
@@ -317,13 +320,17 @@ function swagger2BaseUrl({spec, scheme, contextUrl = ''}) {
317320
const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha(parsedContextUrl.protocol) || 'http'
318321
const computedHost = spec.host || parsedContextUrl.host || ''
319322
const computedPath = spec.basePath || ''
323+
let res
320324

321325
if (computedScheme && computedHost) {
322-
const res = `${computedScheme}://${computedHost + computedPath}`
323-
324-
// If last character is '/', trim it off
325-
return res[res.length - 1] === '/' ? res.slice(0, -1) : res
326+
// we have what we need for an absolute URL
327+
res = `${computedScheme}://${computedHost + computedPath}`
328+
}
329+
else {
330+
// if not, a relative URL will have to do
331+
res = computedPath
326332
}
327333

328-
return ''
334+
// If last character is '/', trim it off
335+
return res[res.length - 1] === '/' ? res.slice(0, -1) : res
329336
}

test/execute/baseurl.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,15 @@ describe('baseUrl', () => {
129129

130130
expect(res).toEqual('https://example.com:9090')
131131
})
132+
133+
it('should include a basePath when no contextUrl is available', () => {
134+
const res = baseUrl({
135+
spec: {
136+
title: 'a spec',
137+
basePath: '/mybase'
138+
}
139+
})
140+
141+
expect(res).toEqual('/mybase')
142+
})
132143
})

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)