Skip to content

Commit 6176e1c

Browse files
authored
Fix inline schema with url handling (#505)
* Fix inline schema with url handling Signed-off-by: Yevhen Vydolob <[email protected]> * Fix test Signed-off-by: Yevhen Vydolob <[email protected]>
1 parent 4d3d997 commit 6176e1c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/languageservice/services/yamlSchemaService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class YAMLSchemaService extends JSONSchemaService {
281281

282282
let schemaFromModeline = this.getSchemaFromModeline(doc);
283283
if (schemaFromModeline !== undefined) {
284-
if (!schemaFromModeline.startsWith('file:')) {
284+
if (!schemaFromModeline.startsWith('file:') && !schemaFromModeline.startsWith('http')) {
285285
if (!path.isAbsolute(schemaFromModeline)) {
286286
const resUri = URI.parse(resource);
287287
schemaFromModeline = URI.file(path.resolve(path.parse(resUri.fsPath).dir, schemaFromModeline)).toString();

test/yamlSchemaService.test.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
import * as sinon from 'sinon';
6+
import * as chai from 'chai';
7+
import * as sinonChai from 'sinon-chai';
8+
import * as SchemaService from '../src/languageservice/services/yamlSchemaService';
9+
import { parse } from '../src/languageservice/parser/yamlParser07';
10+
11+
const expect = chai.expect;
12+
chai.use(sinonChai);
13+
14+
describe('YAML Schema Service', () => {
15+
const sandbox = sinon.createSandbox();
16+
afterEach(() => {
17+
sandbox.restore();
18+
});
19+
20+
describe('Schema for resource', () => {
21+
let requestServiceMock: sinon.SinonSpy;
22+
23+
beforeEach(() => {
24+
requestServiceMock = sandbox.fake.resolves(undefined);
25+
});
26+
27+
it('should handle inline schema http url', () => {
28+
const documentContent = `# yaml-language-server: $schema=http://json-schema.org/draft-07/schema# anothermodeline=value\n`;
29+
const content = `${documentContent}\n---\n- `;
30+
const yamlDock = parse(content);
31+
32+
const service = new SchemaService.YAMLSchemaService(requestServiceMock);
33+
service.getSchemaForResource('', yamlDock.documents[0]);
34+
35+
expect(requestServiceMock).calledOnceWith('http://json-schema.org/draft-07/schema#');
36+
});
37+
38+
it('should handle inline schema https url', () => {
39+
const documentContent = `# yaml-language-server: $schema=https://json-schema.org/draft-07/schema# anothermodeline=value\n`;
40+
const content = `${documentContent}\n---\n- `;
41+
const yamlDock = parse(content);
42+
43+
const service = new SchemaService.YAMLSchemaService(requestServiceMock);
44+
service.getSchemaForResource('', yamlDock.documents[0]);
45+
46+
expect(requestServiceMock).calledOnceWith('https://json-schema.org/draft-07/schema#');
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)