Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolver): align resolver strategies behavior on invalid path discriminator #3760

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/strategies/openapi-3-1-apidom/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
24 changes: 24 additions & 0 deletions test/resolver/strategies/generic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test/resolver/strategies/openapi-3-1-apidom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] });
});
});

Expand Down
Loading