Skip to content

Commit 3d6bbdb

Browse files
dtkavshockey
authored andcommitted
fix: handle empty servers list in oas3 spec when getting baseUrl (#1310)
1 parent 2e3518c commit 3d6bbdb

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/execute/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function oas3BaseUrl({spec, pathName, method, server, contextUrl, serverVariable
270270
let selectedServerUrl = ''
271271
let selectedServerObj = null
272272

273-
if (server && servers) {
273+
if (server && servers && servers.length) {
274274
const serverUrls = servers.map(srv => srv.url)
275275

276276
if (serverUrls.indexOf(server) > -1) {
@@ -279,7 +279,7 @@ function oas3BaseUrl({spec, pathName, method, server, contextUrl, serverVariable
279279
}
280280
}
281281

282-
if (!selectedServerUrl && servers) {
282+
if (!selectedServerUrl && servers && servers.length) {
283283
// default to the first server if we don't have one by now
284284
selectedServerUrl = servers[0].url
285285
selectedServerObj = servers[0]

test/oas3/execute/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,20 @@ describe('buildRequest - OpenAPI Specification 3.0', function () {
755755

756756
expect(res).toEqual('http://google.com')
757757
})
758+
it('should fall back to contextUrls if servers list is empty', function () {
759+
const spec = {
760+
openapi: '3.0.0',
761+
servers: []
762+
}
763+
764+
const res = baseUrl({
765+
spec,
766+
server: 'http://some-invalid-server.com/',
767+
contextUrl: 'http://google.com/'
768+
})
769+
770+
expect(res).toEqual('http://google.com')
771+
})
758772
it('should create a relative url based on a relative server if no contextUrl is available', function () {
759773
const spec = {
760774
openapi: '3.0.0',

0 commit comments

Comments
 (0)