Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,12 @@ export function baseUrl(obj) {
return specIsOAS3 ? oas3BaseUrl(obj) : swagger2BaseUrl(obj)
}

function oas3BaseUrl({spec, server, serverVariables = {}}) {
function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) {
const servers = spec.servers

let selectedServerUrl = ''
let selectedServerObj = null

if (!servers || !Array.isArray(servers)) {
return ''
}

if (server) {
const serverUrls = servers.map(srv => srv.url)

Expand All @@ -232,7 +228,7 @@ function oas3BaseUrl({spec, server, serverVariables = {}}) {
}
}

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

return selectedServerUrl
return buildOas3UrlWithContext(selectedServerUrl, contextUrl)
}

function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
const parsedUrl = url.parse(ourUrl)
const parsedContextUrl = url.parse(contextUrl)

const computedScheme = stripNonAlpha(parsedUrl.protocol) || stripNonAlpha(parsedContextUrl.protocol) || ''
const computedHost = parsedUrl.host || parsedContextUrl.host
const computedPath = parsedUrl.pathname || ''

if (computedScheme && computedHost) {
const res = `${computedScheme}://${computedHost + computedPath}`

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

return ''
}

function getVariableTemplateNames(str) {
Expand Down
7 changes: 4 additions & 3 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,16 +368,17 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
})
})
describe('baseUrl', function () {
it.skip('should return / if no servers are specified', function () {
it('should consider contextUrls correctly with relative server paths', function () {
const spec = {
openapi: '3.0.0'
}

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

expect(res).toEqual('/')
expect(res).toEqual('https://gist.githubusercontent.com')
})
it('should default to using the first server if none is explicitly chosen', function () {
const spec = {
Expand Down