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

Commit

Permalink
fix: handle direct circular file $refs (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Dec 13, 2022
1 parent 775dabf commit 9e227ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/__tests__/fixtures/schemas/circular-file-reference.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"anyOf": [
{
"$ref": "circular-file-reference.json"
},
{
"$ref": "circular-file-reference.json#/anyOf"
}
]
}
31 changes: 31 additions & 0 deletions src/__tests__/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,37 @@ describe('resolver', () => {
],
});
});

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

expect(resolved.errors).toEqual([]);
expect(resolved.result).toStrictEqual({
anyOf: [
{
anyOf: [
{
$ref: 'circular-file-reference.json',
},
{
$ref: 'circular-file-reference.json#/anyOf',
},
],
},
{
$ref: 'circular-file-reference.json#/anyOf',
},
],
});
});
});

describe('cache', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ export class ResolveRunner implements Types.IResolveRunner {
}
}

if (String(ref).length > 0 && this.isFile(this.baseUri) && this.isFile(ref) && this.baseUri.path() === ref.path()) {
ref = new ExtendedURI(`#${ref.fragment()}`);
}

if (this.transformRef) {
return this.transformRef(
{
Expand Down

0 comments on commit 9e227ca

Please sign in to comment.