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

Commit

Permalink
fix: handle JSON pointer parsing errors - local refs (#147)
Browse files Browse the repository at this point in the history
* fix: handle JSON pointer parsing errors - local refs

* style: lint.fix
  • Loading branch information
P0lip committed Nov 25, 2019
1 parent 1b2785a commit c704289
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
34 changes: 33 additions & 1 deletion src/__tests__/resolver.spec.ts
Expand Up @@ -1425,7 +1425,7 @@ describe('resolver', () => {
expect(result.errors.length).toEqual(1);
});

test('should track syntactically invalid JSON pointers', async () => {
test('should track syntactically invalid JSON pointers used by remote refs', async () => {
const source = {
foo: 'bar',
inner: {
Expand Down Expand Up @@ -1457,6 +1457,38 @@ describe('resolver', () => {
]);
});

test('should track syntactically invalid JSON pointers used by local refs', async () => {
const source = {
foo: 'bar',
inner: {
$ref: '#.',
},
};

const resolver = new Resolver({
resolvers: {
file: {
async resolve() {
return {};
},
},
},
});

const result = await resolver.resolve(source);

expect(result.errors).toStrictEqual([
{
code: 'PARSE_POINTER',
message: "'#.' JSON pointer is invalid",
path: [],
uriStack: [],
pointerStack: [],
uri: expect.any(Object),
},
]);
});

test('should replace what it can even if some inner remote refs fail', async () => {
const source = {
inner: {
Expand Down
19 changes: 17 additions & 2 deletions src/crawler.ts
@@ -1,4 +1,5 @@
import { pointerToPath } from '@stoplight/json';
import { Optional } from '@stoplight/types';
import { DepGraph } from 'dependency-graph';
import { get } from 'lodash';

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

private _runner: Types.IResolveRunner;

constructor(runner: Types.IResolveRunner, jsonPointer?: string) {
constructor(runner: Types.IResolveRunner, jsonPointer: Optional<string>, private _resolved: Types.IResolveResult) {
this.jsonPointer = jsonPointer;
this._runner = runner;
}
Expand Down Expand Up @@ -95,7 +96,21 @@ export class ResolveCrawler implements Types.ICrawler {
if (Utils.uriIsJSONPointer(ref)) {
if (this._runner.dereferenceInline) {
const targetPointer = Utils.uriToJSONPointer(ref);
const targetPath = pointerToPath(targetPointer);
let targetPath;
try {
targetPath = pointerToPath(targetPointer);
} catch {
this._resolved.errors.push({
code: 'PARSE_POINTER',
message: `'${ref}' JSON pointer is invalid`,
uri: this._runner.baseUri,
uriStack: this._runner.uriStack,
pointerStack: [],
path: [],
});

return;
}

/**
* Protects against circular references back to something higher up in the tree
Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Expand Up @@ -151,7 +151,7 @@ export class ResolveRunner implements Types.IResolveRunner {
}

// create our crawler instance
const crawler = new ResolveCrawler(this, jsonPointer);
const crawler = new ResolveCrawler(this, jsonPointer, resolved);

// crawl to build up the uriResolvers and pointerGraph
crawler.computeGraph(resolved.result, targetPath, jsonPointer || '');
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Expand Up @@ -247,6 +247,7 @@ export interface IResolveRunner {
dereferenceRemote: boolean;
uriCache: ICache;
depth: number;
uriStack: string[];
baseUri: uri.URI;

graph: DepGraph<IGraphNodeData>;
Expand Down

0 comments on commit c704289

Please sign in to comment.