Skip to content

Commit c94fa43

Browse files
dlechdatho7561
authored andcommitted
Fix YAML JSON Schema files starting with %YAML 1.x
Fix a bug where YAML files containing a JSON Schema that start with %YAML 1.x or a comment that contains a number would fail to to be parsed. This is caused by the fact that the JSON Schema parser would contain the number, e.g. 1.2 as the value of `unresolvedJsonSchema.schema` and so the test for `=== undefined` would fail. This would cause the error from the JSON parser to be returned instead of trying to parse the file as a YAML file. To work around this, also check if `unresolvedJsonSchema.schema` is a number. It if is, it clearly was not a JSON Schema and so we can safely try to parse the file as a YAML file. Fixes: #922 Signed-off-by: David Lechner <dlechner@baylibre.com>
1 parent 6424ff6 commit c94fa43

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/languageservice/services/yamlSchemaService.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,12 @@ export class YAMLSchemaService extends JSONSchemaService {
634634
const requestService = this.requestService;
635635
return super.loadSchema(schemaUri).then((unresolvedJsonSchema: UnresolvedSchema) => {
636636
// If json-language-server failed to parse the schema, attempt to parse it as YAML instead.
637-
if (unresolvedJsonSchema.errors && unresolvedJsonSchema.schema === undefined) {
637+
// If the YAML file starts with %YAML 1.x or contains a comment with a number the schema will
638+
// contain a number instead of being undefined, so we need to check for that too.
639+
if (
640+
unresolvedJsonSchema.errors &&
641+
(unresolvedJsonSchema.schema === undefined || typeof unresolvedJsonSchema.schema === 'number')
642+
) {
638643
return requestService(schemaUri).then(
639644
(content) => {
640645
if (!content) {

test/yamlSchema.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ describe('YAML Schema', () => {
2929
});
3030

3131
it('Loading yaml scheme', async () => {
32-
requestServiceStub.resolves(`
33-
properties:
34-
fooBar:
35-
items:
36-
type: string
37-
type: array
38-
type: object
39-
`);
32+
requestServiceStub.resolves(`%YAML 1.2
33+
---
34+
properties:
35+
fooBar:
36+
items:
37+
type: string
38+
type: array
39+
type: object
40+
`);
4041
const service = new SchemaService.YAMLSchemaService(requestServiceStub, workspaceContext);
4142
const result = await service.loadSchema('fooScheme.yaml');
4243
expect(requestServiceStub.calledOnceWith('fooScheme.yaml'));

0 commit comments

Comments
 (0)