From d727487fbf625f28563ef647ce87eea68bef1ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Fri, 3 Jan 2025 17:41:58 +0100 Subject: [PATCH] fix(resolver): align resolver strategies behavior on invalid path discriminator (#3760) Refs #3728 --- src/index.js | 1 + .../strategies/openapi-3-1-apidom/resolve.js | 2 +- test/index.js | 21 ++++++++++++++++ test/resolver/strategies/generic/index.js | 24 +++++++++++++++++++ .../strategies/openapi-3-1-apidom/index.js | 4 ++-- 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 9036c1401..0514c2be1 100644 --- a/src/index.js +++ b/src/index.js @@ -120,6 +120,7 @@ Swagger.prototype = { useCircularStructures: this.useCircularStructures, requestInterceptor: this.requestInterceptor || null, responseInterceptor: this.responseInterceptor || null, + pathDiscriminator: this.pathDiscriminator || [], skipNormalization: this.skipNormalization || false, ...options, }).then((obj) => { diff --git a/src/resolver/strategies/openapi-3-1-apidom/resolve.js b/src/resolver/strategies/openapi-3-1-apidom/resolve.js index 4dd77ff14..27b7c7c90 100644 --- a/src/resolver/strategies/openapi-3-1-apidom/resolve.js +++ b/src/resolver/strategies/openapi-3-1-apidom/resolve.js @@ -161,7 +161,7 @@ const resolveOpenAPI31Strategy = async (options) => { return { spec: toValue(normalized), errors }; } catch (error) { if (error instanceof InvalidJsonPointerError || error instanceof EvaluationJsonPointerError) { - return { spec: null, errors: [] }; + return { spec, errors: [] }; } throw error; } diff --git a/test/index.js b/test/index.js index 6c5cdeb9e..b3fa38bd9 100644 --- a/test/index.js +++ b/test/index.js @@ -60,6 +60,27 @@ describe('constructor', () => { }); }); + test('should accept pathDiscriminator', async () => { + const spec = { + one: { + $ref: '#/two', + }, + two: { + hi: 'hello', + }, + }; + const client = await SwaggerClient({ spec, pathDiscriminator: ['two'] }); + + expect(client.spec).toEqual({ + one: { + $ref: '#/two', + }, + two: { + hi: 'hello', + }, + }); + }); + test('should resolve a cyclic spec when baseDoc is specified', async () => { const spec = { paths: { diff --git a/test/resolver/strategies/generic/index.js b/test/resolver/strategies/generic/index.js index b7f0597b2..6ce0f1721 100644 --- a/test/resolver/strategies/generic/index.js +++ b/test/resolver/strategies/generic/index.js @@ -48,6 +48,30 @@ describe('resolver', () => { }); }); + test('should resolve nothing when invalid pathDiscriminator supplied', async () => { + const spec = { + openapi: '3.0.4', + components: { + schemas: { + one: { type: 'string' }, + two: { $ref: '#/components/schemas/one' }, + }, + }, + }; + const result = await Swagger.resolve({ spec, pathDiscriminator: ['info1'] }); + + expect(result.errors).toEqual([]); + expect(result.spec).toEqual({ + openapi: '3.0.4', + components: { + schemas: { + one: { type: 'string' }, + two: { $ref: '#/components/schemas/one' }, + }, + }, + }); + }); + test('should resolve OpenAPI 3.0.0 Description', async () => { const spec = { openapi: '3.0.0', diff --git a/test/resolver/strategies/openapi-3-1-apidom/index.js b/test/resolver/strategies/openapi-3-1-apidom/index.js index d79e5ccfb..8085546c7 100644 --- a/test/resolver/strategies/openapi-3-1-apidom/index.js +++ b/test/resolver/strategies/openapi-3-1-apidom/index.js @@ -205,14 +205,14 @@ describe('resolve', () => { }); describe('and pathDiscriminator compiles into invalid JSON Pointer', () => { - test('should return spec as null', async () => { + test('should not resolve', async () => { const spec = globalThis.loadJsonFile(path.join(fixturePath, 'petstore.json')); const resolvedSpec = await SwaggerClient.resolve({ spec, pathDiscriminator: ['path', 'to', 'nothing'], }); - expect(resolvedSpec).toEqual({ spec: null, errors: [] }); + expect(resolvedSpec).toEqual({ spec, errors: [] }); }); });