Skip to content

Fix schema url fragments support #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});