Skip to content

Commit 8b240d2

Browse files
committed
fix absolute-path $ref resolution for schemas without a top-level $id
Signed-off-by: Morgan Chang <shin19991207@gmail.com>
1 parent 0ae5603 commit 8b240d2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,47 @@ 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.yaml': {
227+
type: 'object',
228+
properties: {
229+
name: {
230+
$ref: '/main/schema2.yaml',
231+
},
232+
},
233+
},
234+
'file:///main/schema2.yaml': {
235+
type: 'string',
236+
description: 'Something.',
237+
},
238+
},
239+
});
240+
241+
service
242+
.getResolvedSchema('file:///main/schema.yaml')
243+
.then((fs) => {
244+
assert.deepEqual(fs.errors, []);
245+
assert.deepEqual(fs.schema.properties['name'], {
246+
type: 'string',
247+
description: 'Something.',
248+
_$ref: '/main/schema2.yaml',
249+
_baseUrl: 'file:///main/schema2.yaml',
250+
url: 'file:///main/schema2.yaml',
251+
});
252+
})
253+
.then(
254+
() => {
255+
return testDone();
256+
},
257+
(error) => {
258+
testDone(error);
259+
}
260+
);
261+
});
262+
222263
it('Preserves markdownDescription on $ref siblings', function (testDone) {
223264
const service = new SchemaService.YAMLSchemaService(requestServiceMock, workspaceContext);
224265
service.setSchemaContributions({

0 commit comments

Comments
 (0)