Skip to content

Commit 1741a99

Browse files
committed
Generate relative base url if we can't make an absolute one
1 parent 004c72a commit 1741a99

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/execute/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,17 @@ function swagger2BaseUrl({spec, scheme, contextUrl = ''}) {
317317
const computedScheme = scheme || firstSchemeInSpec || stripNonAlpha(parsedContextUrl.protocol) || 'http'
318318
const computedHost = spec.host || parsedContextUrl.host || ''
319319
const computedPath = spec.basePath || ''
320+
let res
320321

321322
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
323+
// we have what we need for an absolute URL
324+
res = `${computedScheme}://${computedHost + computedPath}`
325+
}
326+
else {
327+
// if not, a relative URL will have to do
328+
res = computedPath
326329
}
327330

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

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
})

0 commit comments

Comments
 (0)