From 13a57a9210e6b7ca9834fa15c94c095806452701 Mon Sep 17 00:00:00 2001 From: James McGonegal Date: Thu, 1 Aug 2024 09:13:45 -0400 Subject: [PATCH] fix(fromOpenApi): support subdirectories in the server URL (#59) --- src/open-api/from-open-api.ts | 2 +- test/oas/oas-servers.test.ts | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/open-api/from-open-api.ts b/src/open-api/from-open-api.ts index d54e506..0bf5733 100644 --- a/src/open-api/from-open-api.ts +++ b/src/open-api/from-open-api.ts @@ -53,7 +53,7 @@ export async function fromOpenApi( for (const baseUrl of serverUrls) { const path = normalizeSwaggerUrl(url) const requestUrl = isAbsoluteUrl(baseUrl) - ? new URL(path, baseUrl).href + ? new URL(`${baseUrl}${path}`).href : joinPaths(path, baseUrl) if ( diff --git a/test/oas/oas-servers.test.ts b/test/oas/oas-servers.test.ts index 5651d9b..ce9f5b8 100644 --- a/test/oas/oas-servers.test.ts +++ b/test/oas/oas-servers.test.ts @@ -78,6 +78,43 @@ it('supports relative server url', async () => { ]) }) +it('supports server url with subdirectory', async () => { + const handlers = await fromOpenApi( + createOpenApiSpec({ + servers: [{ url: 'https://example.com/subdir' }], + paths: { + '/numbers': { + get: { + responses: { + 200: { + content: { + 'application/json': { + example: [1, 2, 3], + }, + }, + }, + }, + }, + }, + }, + }), + ) + expect(await inspectHandlers(handlers)).toEqual([ + { + handler: { + method: 'GET', + path: 'https://example.com/subdir/numbers', + }, + response: { + status: 200, + statusText: 'OK', + headers: expect.arrayContaining([['content-type', 'application/json']]), + body: JSON.stringify([1, 2, 3]), + }, + }, + ]) +}) + it('supports multiple server urls', async () => { const handlers = await fromOpenApi( createOpenApiSpec({