Skip to content

Commit 80fe209

Browse files
authored
Merge pull request #1141 from shockey/bug/ui-3681-oas3-relative-urls
Fix OAS3 context url inference
2 parents a864beb + 8bce4e0 commit 80fe209

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/execute/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,12 @@ export function baseUrl(obj) {
213213
return specIsOAS3 ? oas3BaseUrl(obj) : swagger2BaseUrl(obj)
214214
}
215215

216-
function oas3BaseUrl({spec, server, serverVariables = {}}) {
216+
function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) {
217217
const servers = spec.servers
218218

219219
let selectedServerUrl = ''
220220
let selectedServerObj = null
221221

222-
if (!servers || !Array.isArray(servers)) {
223-
return ''
224-
}
225-
226222
if (server) {
227223
const serverUrls = servers.map(srv => srv.url)
228224

@@ -232,7 +228,7 @@ function oas3BaseUrl({spec, server, serverVariables = {}}) {
232228
}
233229
}
234230

235-
if (!selectedServerUrl) {
231+
if (!selectedServerUrl && servers) {
236232
// default to the first server if we don't have one by now
237233
selectedServerUrl = servers[0].url
238234
selectedServerObj = servers[0]
@@ -253,7 +249,25 @@ function oas3BaseUrl({spec, server, serverVariables = {}}) {
253249
})
254250
}
255251

256-
return selectedServerUrl
252+
return buildOas3UrlWithContext(selectedServerUrl, contextUrl)
253+
}
254+
255+
function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
256+
const parsedUrl = url.parse(ourUrl)
257+
const parsedContextUrl = url.parse(contextUrl)
258+
259+
const computedScheme = stripNonAlpha(parsedUrl.protocol) || stripNonAlpha(parsedContextUrl.protocol) || ''
260+
const computedHost = parsedUrl.host || parsedContextUrl.host
261+
const computedPath = parsedUrl.pathname || ''
262+
263+
if (computedScheme && computedHost) {
264+
const res = `${computedScheme}://${computedHost + computedPath}`
265+
266+
// If last character is '/', trim it off
267+
return res[res.length - 1] === '/' ? res.slice(0, -1) : res
268+
}
269+
270+
return ''
257271
}
258272

259273
function getVariableTemplateNames(str) {

test/oas3/execute/main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,17 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
368368
})
369369
})
370370
describe('baseUrl', function () {
371-
it.skip('should return / if no servers are specified', function () {
371+
it('should consider contextUrls correctly with relative server paths', function () {
372372
const spec = {
373373
openapi: '3.0.0'
374374
}
375375

376376
const res = baseUrl({
377-
spec
377+
spec,
378+
contextUrl: 'https://gist.githubusercontent.com/hkosova/d223eb45c5198db09d08f2603cc0e10a/raw/ae22e290b4f21e19bbfc02b97498289792579fec/relative-server.yaml'
378379
})
379380

380-
expect(res).toEqual('/')
381+
expect(res).toEqual('https://gist.githubusercontent.com')
381382
})
382383
it('should default to using the first server if none is explicitly chosen', function () {
383384
const spec = {

0 commit comments

Comments
 (0)