Skip to content

Commit

Permalink
fix(resolver): align resolver strategies behavior
Browse files Browse the repository at this point in the history
Refs #3728
  • Loading branch information
char0n committed Jan 3, 2025
1 parent 36b6537 commit e5b9f33
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
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 resole', 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

0 comments on commit e5b9f33

Please sign in to comment.