Skip to content

Commit 6286055

Browse files
committed
Extensions: jsonValidation json schema urls should support self-registered schemes. Fixes microsoft#67189
1 parent 85618de commit 6286055

2 files changed

Lines changed: 40 additions & 48 deletions

File tree

extensions/json-language-features/server/src/jsonServerMain.ts

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { xhr, XHRResponse, configure as configureHttpRequests, getErrorStatusDes
1313
import * as fs from 'fs';
1414
import URI from 'vscode-uri';
1515
import * as URL from 'url';
16-
import { startsWith } from './utils/strings';
1716
import { formatError, runSafe, runSafeAsync } from './utils/runner';
1817
import { JSONDocument, JSONSchema, getLanguageService, DocumentLanguageSettings, SchemaConfiguration } from 'vscode-json-languageservice';
1918
import { getLanguageModelCache } from './languageModelCache';
@@ -57,46 +56,51 @@ const workspaceContext = {
5756
return URL.resolve(resource, relativePath);
5857
}
5958
};
60-
61-
const schemaRequestService = (uri: string): Thenable<string> => {
62-
if (startsWith(uri, 'file://')) {
63-
const fsPath = URI.parse(uri).fsPath;
64-
return new Promise<string>((c, e) => {
65-
fs.readFile(fsPath, 'UTF-8', (err, result) => {
66-
err ? e(err.message || err.toString()) : c(result.toString());
67-
});
68-
});
69-
} else if (startsWith(uri, 'vscode://')) {
59+
function getSchemaRequestService(handledSchemas: { [schema: string]: boolean }) {
60+
61+
return (uri: string): Thenable<string> => {
62+
const protocol = uri.substr(0, uri.indexOf(':'));
63+
64+
if (!handledSchemas || handledSchemas[protocol]) {
65+
if (protocol === 'file') {
66+
const fsPath = URI.parse(uri).fsPath;
67+
return new Promise<string>((c, e) => {
68+
fs.readFile(fsPath, 'UTF-8', (err, result) => {
69+
err ? e(err.message || err.toString()) : c(result.toString());
70+
});
71+
});
72+
} else if (protocol === 'http' || protocol === 'https') {
73+
if (uri.indexOf('//schema.management.azure.com/') !== -1) {
74+
/* __GDPR__
75+
"json.schema" : {
76+
"schemaURL" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
77+
}
78+
*/
79+
connection.telemetry.logEvent({
80+
key: 'json.schema',
81+
value: {
82+
schemaURL: uri
83+
}
84+
});
85+
}
86+
const headers = { 'Accept-Encoding': 'gzip, deflate' };
87+
return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
88+
return response.responseText;
89+
}, (error: XHRResponse) => {
90+
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
91+
});
92+
}
93+
}
7094
return connection.sendRequest(VSCodeContentRequest.type, uri).then(responseText => {
7195
return responseText;
7296
}, error => {
7397
return Promise.reject(error.message);
7498
});
75-
}
76-
if (uri.indexOf('//schema.management.azure.com/') !== -1) {
77-
/* __GDPR__
78-
"json.schema" : {
79-
"schemaURL" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
80-
}
81-
*/
82-
connection.telemetry.logEvent({
83-
key: 'json.schema',
84-
value: {
85-
schemaURL: uri
86-
}
87-
});
88-
}
89-
const headers = { 'Accept-Encoding': 'gzip, deflate' };
90-
return xhr({ url: uri, followRedirects: 5, headers }).then(response => {
91-
return response.responseText;
92-
}, (error: XHRResponse) => {
93-
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
94-
});
95-
};
99+
};
100+
}
96101

97102
// create the JSON language service
98103
let languageService = getLanguageService({
99-
schemaRequestService,
100104
workspaceContext,
101105
contributions: [],
102106
});
@@ -117,8 +121,10 @@ let hierarchicalDocumentSymbolSupport = false;
117121
// in the passed params the rootPath of the workspace plus the client capabilities.
118122
connection.onInitialize((params: InitializeParams): InitializeResult => {
119123

124+
const handledProtocols = params.initializationOptions && params.initializationOptions['handledSchemaProtocols'];
125+
120126
languageService = getLanguageService({
121-
schemaRequestService,
127+
schemaRequestService: getSchemaRequestService(handledProtocols),
122128
workspaceContext,
123129
contributions: [],
124130
clientCapabilities: params.capabilities

extensions/json-language-features/server/src/utils/strings.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
export function startsWith(haystack: string, needle: string): boolean {
7-
if (haystack.length < needle.length) {
8-
return false;
9-
}
10-
11-
for (let i = 0; i < needle.length; i++) {
12-
if (haystack[i] !== needle[i]) {
13-
return false;
14-
}
15-
}
16-
17-
return true;
18-
}
19-
206
/**
217
* Determines if haystack ends with needle.
228
*/

0 commit comments

Comments
 (0)