Skip to content
This repository was archived by the owner on Jul 13, 2023. It is now read-only.

Commit 86e67b2

Browse files
authored
fix: handle JSON pointer parsing errors (#140)
1 parent 1c6e074 commit 86e67b2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/__tests__/resolver.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,38 @@ describe('resolver', () => {
13601360
expect(result.errors.length).toEqual(1);
13611361
});
13621362

1363+
test('should track syntactically invalid JSON pointers', async () => {
1364+
const source = {
1365+
foo: 'bar',
1366+
inner: {
1367+
$ref: 'https://foo.com/oas#invalid',
1368+
},
1369+
};
1370+
1371+
const resolver = new Resolver({
1372+
resolvers: {
1373+
https: {
1374+
async resolve() {
1375+
return {};
1376+
},
1377+
},
1378+
},
1379+
});
1380+
1381+
const result = await resolver.resolve(source);
1382+
1383+
expect(result.errors).toStrictEqual([
1384+
{
1385+
code: 'PARSE_POINTER',
1386+
message: "'#invalid' JSON pointer is invalid",
1387+
path: [],
1388+
uriStack: [],
1389+
pointerStack: [],
1390+
uri: expect.any(Object),
1391+
},
1392+
]);
1393+
});
1394+
13631395
test('should replace what it can even if some inner remote refs fail', async () => {
13641396
const source = {
13651397
inner: {

src/runner.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,21 @@ export class ResolveRunner implements Types.IResolveRunner {
119119
let targetPath: any;
120120
const jsonPointer = opts && opts.jsonPointer && opts.jsonPointer.trim();
121121
if (jsonPointer && jsonPointer !== '#' && jsonPointer !== '#/') {
122-
targetPath = pointerToPath(jsonPointer);
122+
try {
123+
targetPath = pointerToPath(jsonPointer);
124+
} catch {
125+
resolved.errors.push({
126+
code: 'PARSE_POINTER',
127+
message: `'${jsonPointer}' JSON pointer is invalid`,
128+
uri: this.baseUri,
129+
uriStack: this.uriStack,
130+
pointerStack: [],
131+
path: [],
132+
});
133+
134+
return resolved;
135+
}
136+
123137
resolved.result = get(resolved.result, targetPath);
124138
}
125139

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ export type ResolverErrorCode =
184184
| 'RESOLVE_URI'
185185
| 'PARSE_URI'
186186
| 'RESOLVE_POINTER'
187+
| 'PARSE_POINTER'
187188
| 'TRANSFORM_DEREFERENCED';
189+
188190
export interface IResolveError {
189191
code: ResolverErrorCode;
190192
message: string;

0 commit comments

Comments
 (0)