diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts index b6534452..af9f7b2f 100644 --- a/src/languageservice/services/yamlSchemaService.ts +++ b/src/languageservice/services/yamlSchemaService.ts @@ -505,6 +505,7 @@ export class YAMLSchemaService extends JSONSchemaService { if (!refUri.startsWith('/')) return resolvedAgainstParent; const parentResource = resourceIndexByUri.get(parentSchemaURL)?.root; const parentResourceId = parentResource?.$id || parentResource?.id; + if (!parentResourceId) return resolvedAgainstParent; const resolvedParentId = _resolveAgainstBase(parentSchemaURL, parentResourceId); if (!resolvedParentId.startsWith('http://') && !resolvedParentId.startsWith('https://')) return resolvedAgainstParent; diff --git a/test/schema.test.ts b/test/schema.test.ts index cc6e9b67..8c7d0761 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -219,6 +219,50 @@ describe('JSON Schema', () => { ); }); + it('Resolving absolute-path $refs without top-level id', function (testDone) { + const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext); + service.setSchemaContributions({ + schemas: { + 'file:///main/schema.json': { + type: 'object', + properties: { + name: { + $ref: '/main/schema2.json', + }, + }, + required: ['name'], + }, + 'file:///main/schema2.json': { + type: 'string', + enum: ['alice', 'bob'], + description: 'Allowed names.', + }, + }, + }); + + service + .getResolvedSchema('file:///main/schema.json') + .then((fs) => { + assert.deepEqual(fs.errors, []); + assert.deepEqual(fs.schema.properties['name'], { + type: 'string', + enum: ['alice', 'bob'], + description: 'Allowed names.', + _$ref: '/main/schema2.json', + _baseUrl: 'file:///main/schema2.json', + url: 'file:///main/schema2.json', + }); + }) + .then( + () => { + return testDone(); + }, + (error) => { + testDone(error); + } + ); + }); + it('Preserves markdownDescription on $ref siblings', function (testDone) { const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext); service.setSchemaContributions({