@@ -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
259273function getVariableTemplateNames ( str ) {
0 commit comments