Skip to content

Commit bc4f37e

Browse files
committed
fix: undefined string is returned as base path if server object is incorrect (has no url property)
1 parent 901425f commit bc4f37e

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ export const extractOperationBasePath = (servers?: OpenAPIV3.ServerObject[]): st
324324
try {
325325
const [firstServer] = servers
326326
let serverUrl = firstServer.url
327+
if(!serverUrl) {
328+
return ''
329+
}
330+
327331
const { variables = {} } = firstServer
328332

329333
for (const param of Object.keys(variables)) {

test/utils.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,12 @@ describe('Unit test for extractOperationBasePath', () => {
5050
expect(extractOperationBasePath([{ url: '/v1/' }])).toEqual('/v1')
5151
expect(extractOperationBasePath([{ url: '/' }])).toEqual('')
5252
})
53+
54+
// todo: should really handle this case in api-unifier, it returns incorrect object in this case,
55+
// since url is required for server object
56+
test('Should handle Servers with empty url correctly', () => {
57+
// @ts-expect-error - Testing edge case with missing url property
58+
expect(extractOperationBasePath([{ }])).toEqual('')
59+
})
5360
})
5461

0 commit comments

Comments
 (0)