Skip to content

Commit 8580bc0

Browse files
committed
Fix OAS3 context url inference
1 parent a864beb commit 8580bc0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/execute/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,13 @@ 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 = {}}) {
217+
console.log('hello')
217218
const servers = spec.servers
218219

219220
let selectedServerUrl = ''
220221
let selectedServerObj = null
221222

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

@@ -232,7 +229,7 @@ function oas3BaseUrl({spec, server, serverVariables = {}}) {
232229
}
233230
}
234231

235-
if (!selectedServerUrl) {
232+
if (!selectedServerUrl && servers) {
236233
// default to the first server if we don't have one by now
237234
selectedServerUrl = servers[0].url
238235
selectedServerObj = servers[0]
@@ -253,7 +250,25 @@ function oas3BaseUrl({spec, server, serverVariables = {}}) {
253250
})
254251
}
255252

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

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