From 8580bc0a6d99753de8d4eeba863e0764b309b6a5 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Thu, 28 Sep 2017 15:20:25 -0700 Subject: [PATCH 1/2] Fix OAS3 context url inference --- src/execute/index.js | 29 ++++++++++++++++++++++------- test/oas3/execute/main.js | 7 ++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/execute/index.js b/src/execute/index.js index 1fb893d18..dc4c801b6 100755 --- a/src/execute/index.js +++ b/src/execute/index.js @@ -213,16 +213,13 @@ export function baseUrl(obj) { return specIsOAS3 ? oas3BaseUrl(obj) : swagger2BaseUrl(obj) } -function oas3BaseUrl({spec, server, serverVariables = {}}) { +function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) { + console.log('hello') const servers = spec.servers let selectedServerUrl = '' let selectedServerObj = null - if (!servers || !Array.isArray(servers)) { - return '' - } - if (server) { const serverUrls = servers.map(srv => srv.url) @@ -232,7 +229,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] @@ -253,7 +250,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) { diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index c56f7526d..d98228012 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -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 = { From 8bce4e03320e02448bb6b41c4b0ec7fbf4543e54 Mon Sep 17 00:00:00 2001 From: kyle Date: Fri, 29 Sep 2017 17:32:50 -0700 Subject: [PATCH 2/2] Remove console.log --- src/execute/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/execute/index.js b/src/execute/index.js index dc4c801b6..8f62d8ed6 100755 --- a/src/execute/index.js +++ b/src/execute/index.js @@ -214,7 +214,6 @@ export function baseUrl(obj) { } function oas3BaseUrl({spec, server, contextUrl, serverVariables = {}}) { - console.log('hello') const servers = spec.servers let selectedServerUrl = ''