diff --git a/package.json b/package.json index a7e858dfc0..df2f6ba9a5 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ ] }, "dependencies": { - "@unleash/express-openapi": "^0.2.2", + "@unleash/express-openapi": "^0.3.0", "ajv": "^8.11.0", "ajv-formats": "^2.1.1", "async": "^3.2.4", diff --git a/src/lib/openapi/index.test.ts b/src/lib/openapi/index.test.ts index 447b6b09a8..8350c52bb5 100644 --- a/src/lib/openapi/index.test.ts +++ b/src/lib/openapi/index.test.ts @@ -27,25 +27,25 @@ test('removeJsonSchemaProps', () => { }); describe('createOpenApiSchema', () => { - test('createOpenApiSchema url', () => { + test('if no baseurl do not return servers', () => { expect( createOpenApiSchema({ unleashUrl: 'https://example.com', baseUriPath: '', - }).servers![0].url, - ).toEqual('https://example.com'); + }).servers, + ).toEqual([]); }); - test('if baseurl is set strips from serverUrl', () => { + test('if baseurl is set leave it in serverUrl', () => { expect( createOpenApiSchema({ unleashUrl: 'https://example.com/demo2', baseUriPath: '/demo2', }).servers![0].url, - ).toEqual('https://example.com'); + ).toEqual('https://example.com/demo2'); }); - test('if baseurl does not end with suffix, cowardly refuses to strip', () => { + test('if baseurl does not start with /, cowardly refuses to strip', () => { expect( createOpenApiSchema({ unleashUrl: 'https://example.com/demo2', @@ -66,6 +66,6 @@ describe('createOpenApiSchema', () => { unleashUrl: 'https://example.com/example/', baseUriPath: '/example', }).servers![0].url, - ).toEqual('https://example.com'); + ).toEqual('https://example.com/example'); }); }); diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index afa58a7e94..dca15410a2 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -340,10 +340,14 @@ const findRootUrl: (unleashUrl: string, baseUriPath: string) => string = ( return unleashUrl; } const baseUrl = new URL(unleashUrl); - if (baseUrl.pathname.indexOf(baseUriPath) >= 0) { - return `${baseUrl.protocol}//${baseUrl.host}`; - } - return baseUrl.toString(); + const url = + baseUrl.pathname.indexOf(baseUriPath) >= 0 + ? `${baseUrl.protocol}//${baseUrl.host}` + : baseUrl.toString(); + + return baseUriPath.startsWith('/') + ? new URL(baseUriPath, url).toString() + : url; }; export const createOpenApiSchema = ({ @@ -354,9 +358,10 @@ export const createOpenApiSchema = ({ 'paths' > => { const url = findRootUrl(unleashUrl, baseUriPath); + return { openapi: '3.0.3', - servers: url ? [{ url }] : [], + servers: baseUriPath ? [{ url }] : [], info: { title: 'Unleash API', version: apiVersion, diff --git a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap index 4dbce61513..a53b239203 100644 --- a/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap +++ b/src/test/e2e/api/openapi/__snapshots__/openapi.e2e.test.ts.snap @@ -12935,11 +12935,7 @@ If the provided project does not exist, the list of events will be empty.", "apiKey": [], }, ], - "servers": [ - { - "url": "http://localhost:4242", - }, - ], + "servers": [], "tags": [ { "description": "Create, update, and delete [Unleash addons](https://docs.getunleash.io/addons).", diff --git a/yarn.lock b/yarn.lock index b64a62c7c4..6eb2723fd4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1528,10 +1528,10 @@ "@typescript-eslint/types" "5.59.2" eslint-visitor-keys "^3.3.0" -"@unleash/express-openapi@^0.2.2": - version "0.2.2" - resolved "https://registry.yarnpkg.com/@unleash/express-openapi/-/express-openapi-0.2.2.tgz#b6e6a3d0fb93f8f52fc7d8f375e9bb52a3d3a46e" - integrity sha512-Evn1gVB5v7QMAs/mGjTc3NihX9wZnMdyBPvpd/JqMI8NDH9z/q46cYnh2t7bFPQj7FBghWwZlcJNm7PU0bxe7A== +"@unleash/express-openapi@^0.3.0": + version "0.3.0" + resolved "https://registry.yarnpkg.com/@unleash/express-openapi/-/express-openapi-0.3.0.tgz#3d65aeafc265732cf83c57b1d18452b5d0904856" + integrity sha512-rcbRNoL689knbemaTbL17FOb94hagr5T/vUUUZ1Fb107SGlBbOoQErf0tHlWpeCs6ffkM6uxo5BUnOAaf6CYDA== dependencies: ajv "^6.10.2" http-errors "^1.7.3"