From df2bd7fcef06fb3e50b0d5b6fb7b2de613eb0065 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 29 Dec 2021 16:18:18 +0100 Subject: [PATCH] Fix resolving refs in external refs. Fixes #121 --- src/services/jsonSchemaService.ts | 2 +- src/test/schema.test.ts | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/services/jsonSchemaService.ts b/src/services/jsonSchemaService.ts index 272b77f..0279a1a 100644 --- a/src/services/jsonSchemaService.ts +++ b/src/services/jsonSchemaService.ts @@ -580,7 +580,7 @@ export class JSONSchemaService implements IJSONSchemaService { if (!seenRefs.has(ref)) { const id = segments[1]; if (id !== undefined && isSubSchemaRef(id)) { // A $ref to a sub-schema with an $id (i.e #hello) - tryMergeSubSchema(next, id, handle); + tryMergeSubSchema(next, id, parentHandle); } else { // A $ref to a JSON Pointer (i.e #/definitions/foo) mergeByJsonPointer(next, parentSchema, parentHandle.uri, id); // can set next.$ref again, use seenRefs to avoid circle } diff --git a/src/test/schema.test.ts b/src/test/schema.test.ts index b1aadf0..538aca3 100644 --- a/src/test/schema.test.ts +++ b/src/test/schema.test.ts @@ -450,6 +450,43 @@ suite('JSON Schema', () => { }); }); + test('Resolving external $ref to ref', async function () { + const service = new SchemaService.JSONSchemaService(newMockRequestService(), workspaceContext); + service.setSchemaContributions({ + schemas: { + "https://myschemastore/main/schema1.json": { + id: 'https://myschemastore/main/schema1.json', + type: 'object', + properties: { + p1: { + '$ref': 'https://myschemastore/main/schema2.json#red' + } + } + }, + "https://myschemastore/main/schema2.json": { + id: 'https://myschemastore/main/schema2.json', + definitions: { + "_red": { + $id: '#red', + $ref: '#yellow' + }, + "_yellow": { + $id: '#yellow', + type: 'number', + const: 5 + } + } + } + } + }); + + const resolvedSchema = await service.getResolvedSchema('https://myschemastore/main/schema1.json'); + assert.deepStrictEqual(resolvedSchema?.schema.properties?.p1, { + type: 'number', + const: 5 + }); + }); + test('Resolving external $ref recursive', async function () { const service = new SchemaService.JSONSchemaService(newMockRequestService(), workspaceContext); service.setSchemaContributions({