From 1b453f9ab5e3f6bba7c5f16b9821e871b7bc8298 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Thu, 31 Oct 2024 12:19:35 +0100 Subject: [PATCH] Schema Does Not Load With URI myproto:///schema.json (#248) --- src/services/jsonSchemaService.ts | 2 +- src/test/schema.test.ts | 55 ++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/services/jsonSchemaService.ts b/src/services/jsonSchemaService.ts index 7b84ee3..dfd2a25 100644 --- a/src/services/jsonSchemaService.ts +++ b/src/services/jsonSchemaService.ts @@ -490,7 +490,7 @@ export class JSONSchemaService implements IJSONSchemaService { }; const resolveExternalLink = (node: JSONSchema, uri: string, refSegment: string | undefined, parentHandle: SchemaHandle): PromiseLike => { - if (contextService && !/^[A-Za-z][A-Za-z0-9+\-.+]*:\/\/.*/.test(uri)) { + if (contextService && !/^[A-Za-z][A-Za-z0-9+\-.+]*:\/.*/.test(uri)) { uri = contextService.resolveRelativePath(uri, parentHandle.uri); } uri = normalizeId(uri); diff --git a/src/test/schema.test.ts b/src/test/schema.test.ts index f59316a..ba986db 100644 --- a/src/test/schema.test.ts +++ b/src/test/schema.test.ts @@ -1998,7 +1998,60 @@ suite('JSON Schema', () => { const testDoc = toDocument(JSON.stringify({ $schema: httpUrl, bar: 2 })); let validation = await ls.doValidation(testDoc.textDoc, testDoc.jsonDoc); assert.deepStrictEqual(validation.map(v => v.message), ['Value must be 3.']); - assert.deepStrictEqual([httpsUrl], accesses); + assert.deepStrictEqual([httpsUrl], accesses); }); + test('combined schemas and URIs without host', async function () { + const schemas: SchemaConfiguration[] = [{ + uri: 'myproto:///schema.json', + fileMatch: ['foo.json'], + }, + { + uri: 'https://myschemastore/schema2.json', + fileMatch: ['foo.json'], + } + ]; + const schemaContents: { [uri: string]: JSONSchema } = { + ['myproto:/schema.json']: { + type: 'object', + properties: { + bar: { + type: 'string' + } + } + }, + ['https://myschemastore/schema2.json']: { + type: 'object', + properties: { + foo: { + type: 'string' + } + } + } + }; + + const accesses: string[] = []; + + + const schemaRequestService: SchemaRequestService = async (uri: string) => { + if (uri === `https://myschemastore/schema2.json` || uri === `myproto:/schema.json`) { + return '{}'; + } + throw new Error('Unknown schema ' + uri); + }; + + + const ls = getLanguageService({ workspaceContext, schemaRequestService }); + ls.configure({ schemas }); + + { + const { textDoc, jsonDoc } = toDocument('{ }', undefined, 'foo://bar/folder/foo.json'); + const res = await ls.doValidation(textDoc, jsonDoc); + console.log(res); + } + + }); + + + });