Skip to content

Commit d1cb877

Browse files
committed
use https locations for all ols schema drafts
1 parent 628c87f commit d1cb877

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

src/parser/jsonParser.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { JSONSchema, JSONSchemaRef } from '../jsonSchema';
88
import { isNumber, equals, isBoolean, isString, isDefined, isObject } from '../utils/objects';
99
import { extendedRegExp, stringLength } from '../utils/strings';
1010
import { TextDocument, ASTNode, ObjectASTNode, ArrayASTNode, BooleanASTNode, NumberASTNode, StringASTNode, NullASTNode, PropertyASTNode, JSONPath, ErrorCode, Diagnostic, DiagnosticSeverity, Range, SchemaDraft } from '../jsonLanguageTypes';
11+
import { URI } from 'vscode-uri';
1112

1213
import * as l10n from '@vscode/l10n';
1314

@@ -171,11 +172,33 @@ export enum EnumMatch {
171172
Key, Enum
172173
}
173174

175+
const httpPrefix = `http://json-schema.org/`;
176+
const httpsPrefix = `https://json-schema.org/`;
177+
178+
export function normalizeId(id: string): string {
179+
// use the https prefix for the old json-schema.org meta schemas
180+
// See https://github.com/microsoft/vscode/issues/195189
181+
if (id.startsWith(httpPrefix)) {
182+
id = httpsPrefix + id.substring(httpPrefix.length);
183+
}
184+
// remove trailing '#', normalize drive capitalization
185+
try {
186+
return URI.parse(id).toString(true);
187+
} catch (e) {
188+
return id;
189+
}
190+
191+
}
192+
193+
export function getSchemaDraftFromId(schemaId: string): SchemaDraft | undefined {
194+
return schemaDraftFromId[normalizeId(schemaId)] ?? undefined;
195+
}
196+
174197
const schemaDraftFromId: { [id: string]: SchemaDraft } = {
175-
'http://json-schema.org/draft-03/schema#': SchemaDraft.v3,
176-
'http://json-schema.org/draft-04/schema#': SchemaDraft.v4,
177-
'http://json-schema.org/draft-06/schema#': SchemaDraft.v6,
178-
'http://json-schema.org/draft-07/schema#': SchemaDraft.v7,
198+
'https://json-schema.org/draft-03/schema': SchemaDraft.v3,
199+
'https://json-schema.org/draft-04/schema': SchemaDraft.v4,
200+
'https://json-schema.org/draft-06/schema': SchemaDraft.v6,
201+
'https://json-schema.org/draft-07/schema': SchemaDraft.v7,
179202
'https://json-schema.org/draft/2019-09/schema': SchemaDraft.v2019_09,
180203
'https://json-schema.org/draft/2020-12/schema': SchemaDraft.v2020_12
181204
};
@@ -378,7 +401,7 @@ export class JSONDocument {
378401
function getSchemaDraft(schema: JSONSchema, fallBack = SchemaDraft.v2020_12) {
379402
let schemaId = schema.$schema;
380403
if (schemaId) {
381-
return schemaDraftFromId[schemaId] ?? fallBack;
404+
return getSchemaDraftFromId(schemaId) ?? fallBack;
382405
}
383406
return fallBack;
384407
}

src/services/configuration.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const schemaContributions: ISchemaContributions = {
1111
schemaAssociations: [],
1212
schemas: {
1313
// bundle the schema-schema to include (localized) descriptions
14-
'http://json-schema.org/draft-04/schema#': {
15-
'$schema': 'http://json-schema.org/draft-04/schema#',
14+
'https://json-schema.org/draft-04/schema': {
1615
'definitions': {
1716
'schemaArray': {
1817
'type': 'array',
@@ -294,7 +293,7 @@ export const schemaContributions: ISchemaContributions = {
294293
},
295294
'default': {}
296295
},
297-
'http://json-schema.org/draft-07/schema#': {
296+
'https://json-schema.org/draft-07/schema': {
298297
'definitions': {
299298
'schemaArray': {
300299
'type': 'array',

src/services/jsonCompletion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ export class JSONCompletion {
740740
private addDollarSchemaCompletions(separatorAfter: string, collector: CompletionsCollector): void {
741741
const schemaIds = this.schemaService.getRegisteredSchemaIds(schema => schema === 'http' || schema === 'https');
742742
schemaIds.forEach(schemaId => {
743-
if (schemaId.startsWith('http://json-schema.org/draft-')) {
743+
if (schemaId.startsWith('https://json-schema.org/draft-')) {
744744
schemaId = schemaId + '#';
745745
}
746746
collector.add({

src/services/jsonSchemaService.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import * as Json from 'jsonc-parser';
77
import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
88
import { URI } from 'vscode-uri';
99
import * as Strings from '../utils/strings';
10-
import * as Parser from '../parser/jsonParser';
11-
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration } from '../jsonLanguageTypes';
10+
import { asSchema, getSchemaDraftFromId, JSONDocument, normalizeId } from '../parser/jsonParser';
11+
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration, SchemaDraft } from '../jsonLanguageTypes';
1212

1313
import * as l10n from '@vscode/l10n';
1414
import { createRegex } from '../utils/glob';
@@ -34,7 +34,7 @@ export interface IJSONSchemaService {
3434
/**
3535
* Looks up the appropriate schema for the given URI
3636
*/
37-
getSchemaForResource(resource: string, document?: Parser.JSONDocument): PromiseLike<ResolvedSchema | undefined>;
37+
getSchemaForResource(resource: string, document?: JSONDocument): PromiseLike<ResolvedSchema | undefined>;
3838

3939
/**
4040
* Returns all registered schema ids
@@ -193,9 +193,9 @@ export class ResolvedSchema {
193193
public readonly schema: JSONSchema;
194194
public readonly errors: string[];
195195
public readonly warnings: string[];
196-
public readonly schemaDraft: string | undefined;
196+
public readonly schemaDraft: SchemaDraft | undefined;
197197

198-
constructor(schema: JSONSchema, errors: string[] = [], warnings: string[] = [], schemaDraft: string | undefined) {
198+
constructor(schema: JSONSchema, errors: string[] = [], warnings: string[] = [], schemaDraft: SchemaDraft | undefined) {
199199
this.schema = schema;
200200
this.errors = errors;
201201
this.warnings = warnings;
@@ -205,7 +205,7 @@ export class ResolvedSchema {
205205
public getSection(path: string[]): JSONSchema | undefined {
206206
const schemaRef = this.getSectionRecursive(path, this.schema);
207207
if (schemaRef) {
208-
return Parser.asSchema(schemaRef);
208+
return asSchema(schemaRef);
209209
}
210210
return undefined;
211211
}
@@ -390,9 +390,6 @@ export class JSONSchemaService implements IJSONSchemaService {
390390
const errorMessage = l10n.t('Unable to load schema from \'{0}\'. No schema request service available', toDisplayString(url));
391391
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [errorMessage]));
392392
}
393-
if (url.startsWith('http://json-schema.org/')) {
394-
url = 'https' + url.substring(4); // always access json-schema.org with https. See https://github.com/microsoft/vscode/issues/195189
395-
}
396393
return this.requestService(url).then(
397394
content => {
398395
if (!content) {
@@ -433,8 +430,8 @@ export class JSONSchemaService implements IJSONSchemaService {
433430
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
434431
const schema = schemaToResolve.schema;
435432

436-
let schemaDraft = schema.$schema ? normalizeId(schema.$schema) : undefined;
437-
if (schemaDraft === 'http://json-schema.org/draft-03/schema') {
433+
const schemaDraft = schema.$schema ? getSchemaDraftFromId(schema.$schema) : undefined;
434+
if (schemaDraft === SchemaDraft.v3) {
438435
return this.promise.resolve(new ResolvedSchema({}, [l10n.t("Draft-03 schemas are not supported.")], [], schemaDraft));
439436
}
440437

@@ -634,7 +631,7 @@ export class JSONSchemaService implements IJSONSchemaService {
634631
}
635632
};
636633

637-
private getSchemaFromProperty(resource: string, document: Parser.JSONDocument): string | undefined {
634+
private getSchemaFromProperty(resource: string, document: JSONDocument): string | undefined {
638635
if (document.root?.type === 'object') {
639636
for (const p of document.root.properties) {
640637
if (p.keyNode.value === '$schema' && p.valueNode?.type === 'string') {
@@ -666,15 +663,15 @@ export class JSONSchemaService implements IJSONSchemaService {
666663
return schemas;
667664
}
668665

669-
public getSchemaURIsForResource(resource: string, document?: Parser.JSONDocument): string[] {
666+
public getSchemaURIsForResource(resource: string, document?: JSONDocument): string[] {
670667
let schemeId = document && this.getSchemaFromProperty(resource, document);
671668
if (schemeId) {
672669
return [schemeId];
673670
}
674671
return this.getAssociatedSchemas(resource);
675672
}
676673

677-
public getSchemaForResource(resource: string, document?: Parser.JSONDocument): PromiseLike<ResolvedSchema | undefined> {
674+
public getSchemaForResource(resource: string, document?: JSONDocument): PromiseLike<ResolvedSchema | undefined> {
678675
if (document) {
679676
// first use $schema if present
680677
let schemeId = this.getSchemaFromProperty(resource, document);
@@ -704,7 +701,7 @@ export class JSONSchemaService implements IJSONSchemaService {
704701
}
705702
}
706703

707-
public getMatchingSchemas(document: TextDocument, jsonDocument: Parser.JSONDocument, schema?: JSONSchema): PromiseLike<MatchingSchema[]> {
704+
public getMatchingSchemas(document: TextDocument, jsonDocument: JSONDocument, schema?: JSONSchema): PromiseLike<MatchingSchema[]> {
708705
if (schema) {
709706
const id = schema.id || ('schemaservice://untitled/matchingSchemas/' + idCounter++);
710707
const handle = this.addSchemaHandle(id, schema);
@@ -724,16 +721,6 @@ export class JSONSchemaService implements IJSONSchemaService {
724721

725722
let idCounter = 0;
726723

727-
function normalizeId(id: string): string {
728-
// remove trailing '#', normalize drive capitalization
729-
try {
730-
return URI.parse(id).toString(true);
731-
} catch (e) {
732-
return id;
733-
}
734-
735-
}
736-
737724
function normalizeResourceForMatching(resource: string): string {
738725
// remove queries and fragments, normalize drive capitalization
739726
try {

src/test/completion.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,8 @@ suite('JSON Completion', () => {
10791079
await testCompletionsFor('{ "$schema": | }', schema, {
10801080
items: [
10811081
{ label: '"http://myschemastore/test1"', resultText: '{ "$schema": "http://myschemastore/test1" }' },
1082-
{ label: '"http://json-schema.org/draft-04/schema#"', resultText: '{ "$schema": "http://json-schema.org/draft-04/schema#" }' },
1083-
{ label: '"http://json-schema.org/draft-07/schema#"', resultText: '{ "$schema": "http://json-schema.org/draft-07/schema#" }' }
1082+
{ label: '"https://json-schema.org/draft-04/schema#"', resultText: '{ "$schema": "https://json-schema.org/draft-04/schema#" }' },
1083+
{ label: '"https://json-schema.org/draft-07/schema#"', resultText: '{ "$schema": "https://json-schema.org/draft-07/schema#" }' }
10841084
]
10851085
});
10861086
await testCompletionsFor('{ "$schema": "|', schema, {

src/test/schema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ suite('JSON Schema', () => {
18311831

18321832
test('schema resolving severity', async function () {
18331833
const schema: JSONSchema = {
1834-
$schema: 'http://json-schema.org/draft-03/schema',
1834+
$schema: 'http://json-schema.org/draft-03/schema#',
18351835
type: 'string'
18361836
};
18371837

0 commit comments

Comments
 (0)