Skip to content

Commit

Permalink
Fix schema url fragments support
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob committed Aug 23, 2021
1 parent 4f03d4a commit 414d72c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,16 @@ export class YAMLSchemaService extends JSONSchemaService {
this.customSchemaProvider = customSchemaProvider;
}

public resolveSchemaContent(
async resolveSchemaContent(
schemaToResolve: UnresolvedSchema,
schemaURL: string,
dependencies: SchemaDependencies
): Promise<ResolvedSchema> {
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
const schema = schemaToResolve.schema;
let schema = schemaToResolve.schema;
const contextService = this.contextService;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const findSection = (schema: JSONSchema, path: string): any => {
const findSection = (schema: JSONSchema, path: string): JSONSchema => {
if (!path) {
return schema;
}
Expand Down Expand Up @@ -176,15 +175,15 @@ export class YAMLSchemaService extends JSONSchemaService {
});
};

const resolveRefs = (
const resolveRefs = async (
node: JSONSchema,
parentSchema: JSONSchema,
parentSchemaURL: string,
parentSchemaDependencies: SchemaDependencies
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> => {
if (!node || typeof node !== 'object') {
return Promise.resolve(null);
return null;
}

const toWalk: JSONSchema[] = [node];
Expand Down Expand Up @@ -260,7 +259,17 @@ export class YAMLSchemaService extends JSONSchemaService {
if (parentSchemaURL.indexOf('#') > 0) {
const segments = parentSchemaURL.split('#', 2);
if (segments[0].length > 0 && segments[1].length > 0) {
openPromises.push(resolveExternalLink(node, segments[0], segments[1], parentSchemaURL, parentSchemaDependencies));
const newSchema = {};
await resolveExternalLink(newSchema, segments[0], segments[1], parentSchemaURL, parentSchemaDependencies);
for (const key in schema) {
if (key === 'required') {
continue;
}
if (Object.prototype.hasOwnProperty.call(schema, key) && !Object.prototype.hasOwnProperty.call(newSchema, key)) {
newSchema[key] = schema[key];
}
}
schema = newSchema;
}
}

Expand All @@ -275,9 +284,8 @@ export class YAMLSchemaService extends JSONSchemaService {
return Promise.all(openPromises);
};

return resolveRefs(schema, schema, schemaURL, dependencies).then(() => {
return new ResolvedSchema(schema, resolveErrors);
});
await resolveRefs(schema, schema, schemaURL, dependencies);
return new ResolvedSchema(schema, resolveErrors);
}

public getSchemaForResource(resource: string, doc: JSONDocument): Promise<ResolvedSchema> {
Expand Down
26 changes: 26 additions & 0 deletions test/yamlSchemaService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,31 @@ describe('YAML Schema Service', () => {

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

it('should handle url with fragments when root object is schema', async () => {
const content = `# yaml-language-server: $schema=https://json-schema.org/draft-07/schema#/definitions/schemaArray`;
const yamlDock = parse(content);

requestServiceMock = sandbox.fake.resolves(`{"definitions": {"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"bar": {
"type": "string"
}
}, "properties": {"foo": {"type": "boolean"}}, "required": ["foo"]}`);

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

expect(requestServiceMock).calledTwice;
expect(requestServiceMock).calledWithExactly('https://json-schema.org/draft-07/schema');
expect(requestServiceMock).calledWithExactly('https://json-schema.org/draft-07/schema#/definitions/schemaArray');

expect(schema.schema.type).eqls('array');
expect(schema.schema.required).is.undefined;
expect(schema.schema.definitions.bar.type).eqls('string');
});
});
});

0 comments on commit 414d72c

Please # to comment.