Skip to content

Commit f7ea162

Browse files
authored
Fix absolute-path $ref resolution for schemas without a top-level $id (#1229)
* fix absolute-path $ref resolution for schemas without a top-level $id Signed-off-by: Morgan Chang <shin19991207@gmail.com> * implement automated test Signed-off-by: Morgan Chang <shin19991207@gmail.com> --------- Signed-off-by: Morgan Chang <shin19991207@gmail.com>
1 parent 0ae5603 commit f7ea162

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/languageservice/services/yamlSchemaService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ export class YAMLSchemaService extends JSONSchemaService {
505505
if (!refUri.startsWith('/')) return resolvedAgainstParent;
506506
const parentResource = resourceIndexByUri.get(parentSchemaURL)?.root;
507507
const parentResourceId = parentResource?.$id || parentResource?.id;
508+
if (!parentResourceId) return resolvedAgainstParent;
508509
const resolvedParentId = _resolveAgainstBase(parentSchemaURL, parentResourceId);
509510
if (!resolvedParentId.startsWith('http://') && !resolvedParentId.startsWith('https://')) return resolvedAgainstParent;
510511

test/schema.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,50 @@ describe('JSON Schema', () => {
219219
);
220220
});
221221

222+
it('Resolving absolute-path $refs without top-level id', function (testDone) {
223+
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
224+
service.setSchemaContributions({
225+
schemas: {
226+
'file:///main/schema.json': {
227+
type: 'object',
228+
properties: {
229+
name: {
230+
$ref: '/main/schema2.json',
231+
},
232+
},
233+
required: ['name'],
234+
},
235+
'file:///main/schema2.json': {
236+
type: 'string',
237+
enum: ['alice', 'bob'],
238+
description: 'Allowed names.',
239+
},
240+
},
241+
});
242+
243+
service
244+
.getResolvedSchema('file:///main/schema.json')
245+
.then((fs) => {
246+
assert.deepEqual(fs.errors, []);
247+
assert.deepEqual(fs.schema.properties['name'], {
248+
type: 'string',
249+
enum: ['alice', 'bob'],
250+
description: 'Allowed names.',
251+
_$ref: '/main/schema2.json',
252+
_baseUrl: 'file:///main/schema2.json',
253+
url: 'file:///main/schema2.json',
254+
});
255+
})
256+
.then(
257+
() => {
258+
return testDone();
259+
},
260+
(error) => {
261+
testDone(error);
262+
}
263+
);
264+
});
265+
222266
it('Preserves markdownDescription on $ref siblings', function (testDone) {
223267
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
224268
service.setSchemaContributions({

0 commit comments

Comments
 (0)