Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscode-json-languageservice",
"version": "5.7.0",
"version": "5.7.1",
"description": "Language service for JSON",
"main": "./lib/umd/jsonLanguageService.js",
"typings": "./lib/umd/jsonLanguageService",
Expand Down
30 changes: 18 additions & 12 deletions src/services/jsonSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Matc
import * as l10n from '@vscode/l10n';
import { createRegex } from '../utils/glob';
import { isObject, isString } from '../utils/objects';
import { DiagnosticRelatedInformation, Range } from 'vscode-languageserver-types';

export interface IJSONSchemaService {

Expand Down Expand Up @@ -180,19 +181,23 @@ class SchemaHandle implements ISchemaHandle {


export class UnresolvedSchema {
public schema: JSONSchema;
public errors: SchemaDiagnostic[];
public readonly schema: JSONSchema;
public readonly errors: SchemaDiagnostic[];

constructor(schema: JSONSchema, errors: SchemaDiagnostic[] = []) {
this.schema = schema;
this.errors = errors;
}
}

export type SchemaDiagnostic = { message: string; code: ErrorCode }
export type SchemaDiagnostic = { readonly message: string; readonly code: ErrorCode; relatedInformation?: DiagnosticRelatedInformation[] }

function toDiagnostic(message: string, code: ErrorCode): SchemaDiagnostic {
return { message, code };
function toDiagnostic(message: string, code: ErrorCode, relatedURL?: string): SchemaDiagnostic {
const relatedInformation: DiagnosticRelatedInformation[] | undefined = relatedURL ? [{
location: { uri: relatedURL, range: Range.create(0, 0, 0, 0) },
message
}] : undefined;
return { message, code, relatedInformation };
}

export class ResolvedSchema {
Expand Down Expand Up @@ -394,25 +399,25 @@ export class JSONSchemaService implements IJSONSchemaService {
public loadSchema(url: string): PromiseLike<UnresolvedSchema> {
if (!this.requestService) {
const errorMessage = l10n.t('Unable to load schema from \'{0}\'. No schema request service available', toDisplayString(url));
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError)]));
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError, url)]));
}
return this.requestService(url).then(
content => {
if (!content) {
const errorMessage = l10n.t('Unable to load schema from \'{0}\': No content.', toDisplayString(url));
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError)]);
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, ErrorCode.SchemaResolveError, url)]);
}
const errors = [];
if (content.charCodeAt(0) === 65279) {
errors.push(toDiagnostic(l10n.t('Problem reading content from \'{0}\': UTF-8 with BOM detected, only UTF 8 is allowed.', toDisplayString(url)), ErrorCode.SchemaResolveError));
errors.push(toDiagnostic(l10n.t('Problem reading content from \'{0}\': UTF-8 with BOM detected, only UTF 8 is allowed.', toDisplayString(url)), ErrorCode.SchemaResolveError, url));
content = content.trimStart();
}

let schemaContent: JSONSchema = {};
const jsonErrors: Json.ParseError[] = [];
schemaContent = Json.parse(content, jsonErrors);
if (jsonErrors.length) {
errors.push(toDiagnostic(l10n.t('Unable to parse content from \'{0}\': Parse error at offset {1}.', toDisplayString(url), jsonErrors[0].offset), ErrorCode.SchemaResolveError));
errors.push(toDiagnostic(l10n.t('Unable to parse content from \'{0}\': Parse error at offset {1}.', toDisplayString(url), jsonErrors[0].offset), ErrorCode.SchemaResolveError, url));
}
return new UnresolvedSchema(schemaContent, errors);
},
Expand All @@ -435,7 +440,7 @@ export class JSONSchemaService implements IJSONSchemaService {
errorCode += code;
}
const errorMessage = l10n.t('Unable to load schema from \'{0}\': {1}.', toDisplayString(url), message);
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, errorCode)]);
return new UnresolvedSchema(<JSONSchema>{}, [toDiagnostic(errorMessage, errorCode, url)]);
}
);
}
Expand Down Expand Up @@ -511,9 +516,10 @@ export class JSONSchemaService implements IJSONSchemaService {
return referencedHandle.getUnresolvedSchema().then(unresolvedSchema => {
parentHandle.dependencies.add(uri);
if (unresolvedSchema.errors.length) {
const error = unresolvedSchema.errors[0];
const loc = refSegment ? uri + '#' + refSegment : uri;
const errorMessage = l10n.t('Problems loading reference \'{0}\': {1}', loc, unresolvedSchema.errors[0].message);
resolveErrors.push(toDiagnostic(errorMessage, unresolvedSchema.errors[0].code));
const errorMessage = refSegment? l10n.t('Problems loading reference \'{0}\': {1}', refSegment, error.message) : error.message;
resolveErrors.push(toDiagnostic(errorMessage, error.code, uri));
}
mergeRef(node, unresolvedSchema.schema, referencedHandle, refSegment);
return resolveRefs(node, unresolvedSchema.schema, referencedHandle);
Expand Down
12 changes: 7 additions & 5 deletions src/services/jsonValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TextDocument, ErrorCode, PromiseConstructor, LanguageSettings, Document
import * as l10n from '@vscode/l10n';
import { JSONSchemaRef, JSONSchema } from '../jsonSchema';
import { isBoolean } from '../utils/objects';
import { DiagnosticRelatedInformation } from 'vscode-languageserver-types';

export class JSONValidation {

Expand Down Expand Up @@ -53,25 +54,26 @@ export class JSONValidation {
let schemaRequest = documentSettings?.schemaRequest ? toDiagnosticSeverity(documentSettings.schemaRequest) : DiagnosticSeverity.Warning;

if (schema) {
const addSchemaProblem = (errorMessage: string, errorCode: ErrorCode) => {
const addSchemaProblem = (errorMessage: string, errorCode: ErrorCode, relatedInformation?: DiagnosticRelatedInformation[]) => {
if (jsonDocument.root && schemaRequest) {
const astRoot = jsonDocument.root;
const property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
if (property && property.keyNode.value === '$schema') {
const node = property.valueNode || property;
const range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode, 'json', relatedInformation));
} else {
const range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode, 'json', relatedInformation));
}
}
};
if (schema.errors.length) {
addSchemaProblem(schema.errors[0].message, schema.errors[0].code);
const error = schema.errors[0];
addSchemaProblem(error.message, error.code, error.relatedInformation);
} else if (schemaValidation) {
for (const warning of schema.warnings) {
addSchemaProblem(warning.message, warning.code);
addSchemaProblem(warning.message, warning.code, warning.relatedInformation);
}
const semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation, documentSettings?.schemaDraft);
if (semanticErrors) {
Expand Down
10 changes: 8 additions & 2 deletions src/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { promises as fs } from 'fs';
import * as url from 'url';
import * as path from 'path';
import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema, LanguageService } from '../jsonLanguageService';
import { DiagnosticSeverity, ErrorCode, SchemaConfiguration } from '../jsonLanguageTypes';
import { DiagnosticSeverity, ErrorCode, Range, SchemaConfiguration } from '../jsonLanguageTypes';

function toDocument(text: string, config?: Parser.JSONDocumentConfig, uri = 'foo://bar/file.json'): { textDoc: TextDocument, jsonDoc: Parser.JSONDocument } {

Expand Down Expand Up @@ -1079,7 +1079,13 @@ suite('JSON Schema', () => {
service.clearExternalSchemas();

resolvedSchema = await service.getSchemaForResource('main.bar');
assert.deepStrictEqual(resolvedSchema?.errors, [{ message: "Problems loading reference 'http://myschemastore/myschemafoo': Unable to load schema from 'http://myschemastore/myschemafoo': Resource not found.", code: ErrorCode.SchemaResolveError }]);
const message = "Unable to load schema from 'http://myschemastore/myschemafoo': Resource not found.";
assert.deepStrictEqual(resolvedSchema?.errors, [
{
message: message,
code: ErrorCode.SchemaResolveError,
relatedInformation: [{ location: { uri: 'http://myschemastore/myschemafoo', range: Range.create(0, 0, 0, 0) }, message: message }]
}]);

service.clearExternalSchemas();
service.registerExternalSchema({ uri: id2, schema: schema2 });
Expand Down