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

fix: resolve root inline JSON pointers #146

Merged
merged 1 commit into from
Nov 23, 2019
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
14 changes: 14 additions & 0 deletions src/__tests__/fixtures/schemas/circular-root-reference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"": {
"type": "string"
},
"anyOf": [
{
"$ref": "#"
},
{
"$ref": "#/"
}
]
}
39 changes: 39 additions & 0 deletions src/__tests__/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,45 @@ describe('resolver', () => {
],
});
});

test('should handle circular local root reference', async () => {
const resolver = new Resolver({
resolvers: {
file: new FileReader(),
},
});
const docUri = join(__dirname, './fixtures/schemas/circular-root-reference.json');
const resolved = await resolver.resolve(JSON.parse(fs.readFileSync(docUri, 'utf8')), {
baseUri: docUri,
});

expect(resolved.errors).toEqual([]);
expect(resolved.result).toStrictEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
'': {
type: 'string',
},
anyOf: [
{
$schema: 'http://json-schema.org/draft-07/schema#',
'': {
type: 'string',
},
anyOf: [
{
$ref: '#',
},
{
$ref: '#/',
},
],
},
{
type: 'string',
},
],
});
});
});

describe('cache', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ResolveCrawler implements Types.ICrawler {
* Protects against circular references back to something higher up in the tree
* Will stop #/definitions/columns/rows -> #/definitions/columns
*/
let referencesParent = true;
let referencesParent = targetPath.length > 0;
for (const i in targetPath) {
if (parentPath[i] !== targetPath[i]) {
referencesParent = false;
Expand Down
6 changes: 3 additions & 3 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class ResolveRunner implements Types.IResolveRunner {
if (!dependants.length) continue;

const pointerPath = pointerToPath(pointer);
const val = get(draft, pointerPath);
const val = pointerPath.length === 0 ? original(draft) : get(draft, pointerPath);
for (const dependant of dependants) {
// check to prevent circular references in the resulting JS object
// this implementation is MUCH more performant than decycling the final object to remove circulars
Expand Down Expand Up @@ -323,7 +323,7 @@ export class ResolveRunner implements Types.IResolveRunner {
let ref = new URI(refStr);

// Does ref only have a fragment
if (ref.toString().charAt(0) !== '#') {
if (refStr[0] !== '#') {
const isFile = this.isFile(ref);

// if we're working with a file, resolve any path diferences and make sure the scheme is set
Expand Down Expand Up @@ -426,7 +426,7 @@ export class ResolveRunner implements Types.IResolveRunner {
const { val, ref, resolvingPointer, parentPointer, pointerStack } = opts;

// slice to make a fresh copy since we mutate in crawler for performance
const parentPath = (opts.parentPath || []).slice();
const parentPath = opts.parentPath ? opts.parentPath.slice() : [];

const uriCacheKey = this.computeUriCacheKey(ref);
const lookupResult: Types.IUriResult = {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export const addToJSONPointer = (pointer: string, part: string): string => {

/** @hidden */
export const uriToJSONPointer = (uri: uri.URI): string => {
return uri && uri.fragment() ? `#${uri.fragment()}` : '';
return uri.fragment() ? `#${uri.fragment()}` : '#';
};

/** @hidden */
export const uriIsJSONPointer = (ref: uri.URI): boolean => {
return ref.toString().slice(0, 2) === '#/';
return ref.path() === '';
};