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

Commit c704289

Browse files
authored
fix: handle JSON pointer parsing errors - local refs (#147)
* fix: handle JSON pointer parsing errors - local refs * style: lint.fix
1 parent 1b2785a commit c704289

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/__tests__/resolver.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ describe('resolver', () => {
14251425
expect(result.errors.length).toEqual(1);
14261426
});
14271427

1428-
test('should track syntactically invalid JSON pointers', async () => {
1428+
test('should track syntactically invalid JSON pointers used by remote refs', async () => {
14291429
const source = {
14301430
foo: 'bar',
14311431
inner: {
@@ -1457,6 +1457,38 @@ describe('resolver', () => {
14571457
]);
14581458
});
14591459

1460+
test('should track syntactically invalid JSON pointers used by local refs', async () => {
1461+
const source = {
1462+
foo: 'bar',
1463+
inner: {
1464+
$ref: '#.',
1465+
},
1466+
};
1467+
1468+
const resolver = new Resolver({
1469+
resolvers: {
1470+
file: {
1471+
async resolve() {
1472+
return {};
1473+
},
1474+
},
1475+
},
1476+
});
1477+
1478+
const result = await resolver.resolve(source);
1479+
1480+
expect(result.errors).toStrictEqual([
1481+
{
1482+
code: 'PARSE_POINTER',
1483+
message: "'#.' JSON pointer is invalid",
1484+
path: [],
1485+
uriStack: [],
1486+
pointerStack: [],
1487+
uri: expect.any(Object),
1488+
},
1489+
]);
1490+
});
1491+
14601492
test('should replace what it can even if some inner remote refs fail', async () => {
14611493
const source = {
14621494
inner: {

src/crawler.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pointerToPath } from '@stoplight/json';
2+
import { Optional } from '@stoplight/types';
23
import { DepGraph } from 'dependency-graph';
34
import { get } from 'lodash';
45

@@ -20,7 +21,7 @@ export class ResolveCrawler implements Types.ICrawler {
2021

2122
private _runner: Types.IResolveRunner;
2223

23-
constructor(runner: Types.IResolveRunner, jsonPointer?: string) {
24+
constructor(runner: Types.IResolveRunner, jsonPointer: Optional<string>, private _resolved: Types.IResolveResult) {
2425
this.jsonPointer = jsonPointer;
2526
this._runner = runner;
2627
}
@@ -95,7 +96,21 @@ export class ResolveCrawler implements Types.ICrawler {
9596
if (Utils.uriIsJSONPointer(ref)) {
9697
if (this._runner.dereferenceInline) {
9798
const targetPointer = Utils.uriToJSONPointer(ref);
98-
const targetPath = pointerToPath(targetPointer);
99+
let targetPath;
100+
try {
101+
targetPath = pointerToPath(targetPointer);
102+
} catch {
103+
this._resolved.errors.push({
104+
code: 'PARSE_POINTER',
105+
message: `'${ref}' JSON pointer is invalid`,
106+
uri: this._runner.baseUri,
107+
uriStack: this._runner.uriStack,
108+
pointerStack: [],
109+
path: [],
110+
});
111+
112+
return;
113+
}
99114

100115
/**
101116
* Protects against circular references back to something higher up in the tree

src/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class ResolveRunner implements Types.IResolveRunner {
151151
}
152152

153153
// create our crawler instance
154-
const crawler = new ResolveCrawler(this, jsonPointer);
154+
const crawler = new ResolveCrawler(this, jsonPointer, resolved);
155155

156156
// crawl to build up the uriResolvers and pointerGraph
157157
crawler.computeGraph(resolved.result, targetPath, jsonPointer || '');

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export interface IResolveRunner {
247247
dereferenceRemote: boolean;
248248
uriCache: ICache;
249249
depth: number;
250+
uriStack: string[];
250251
baseUri: uri.URI;
251252

252253
graph: DepGraph<IGraphNodeData>;

0 commit comments

Comments
 (0)