Skip to content

Commit

Permalink
Fix relative file paths with fragments (#603)
Browse files Browse the repository at this point in the history
* Fix file paths with fragments

* Support Windows path in test
  • Loading branch information
codeclown committed Dec 6, 2021
1 parent cb5b7e9 commit df7931c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,24 @@ export class YAMLSchemaService extends JSONSchemaService {
let schemaFromModeline = getSchemaFromModeline(doc);
if (schemaFromModeline !== undefined) {
if (!schemaFromModeline.startsWith('file:') && !schemaFromModeline.startsWith('http')) {
// If path contains a fragment and it is left intact, "#" will be
// considered part of the filename and converted to "%23" by
// path.resolve() -> take it out and add back after path.resolve
let appendix = '';
if (schemaFromModeline.indexOf('#') > 0) {
const segments = schemaFromModeline.split('#', 2);
schemaFromModeline = segments[0];
appendix = segments[1];
}
if (!path.isAbsolute(schemaFromModeline)) {
const resUri = URI.parse(resource);
schemaFromModeline = URI.file(path.resolve(path.parse(resUri.fsPath).dir, schemaFromModeline)).toString();
} else {
schemaFromModeline = URI.file(schemaFromModeline).toString();
}
if (appendix.length > 0) {
schemaFromModeline += '#' + appendix;
}
}
this.addSchemaPriority(schemaFromModeline, SchemaPriority.Modeline);
schemas.push(schemaFromModeline);
Expand Down
25 changes: 25 additions & 0 deletions test/yamlSchemaService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ describe('YAML Schema Service', () => {
expect(schema.schema.definitions.bar.type).eqls('string');
});

it('should handle file path with fragments', async () => {
const content = `# yaml-language-server: $schema=schema.json#/definitions/schemaArray\nfoo: bar`;
const yamlDock = parse(content);

requestServiceMock = sandbox.fake.resolves(`{"definitions": {"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
}}, "properties": {}}`);

const service = new SchemaService.YAMLSchemaService(requestServiceMock);
const schema = await service.getSchemaForResource('', yamlDock.documents[0]);

expect(requestServiceMock).calledTwice;
if (process.platform === 'win32') {
expect(requestServiceMock).calledWithExactly('file:///d%3A/schema.json');
expect(requestServiceMock).calledWithExactly('file:///d%3A/schema.json#/definitions/schemaArray');
} else {
expect(requestServiceMock).calledWithExactly('file:///schema.json');
expect(requestServiceMock).calledWithExactly('file:///schema.json#/definitions/schemaArray');
}

expect(schema.schema.type).eqls('array');
});

it('should handle modeline schema comment in the middle of file', () => {
const documentContent = `foo:\n bar\n# yaml-language-server: $schema=https://json-schema.org/draft-07/schema#\naa:bbb\n`;
const content = `${documentContent}`;
Expand Down

0 comments on commit df7931c

Please sign in to comment.