Skip to content

Commit

Permalink
Fix bad schema handle
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Jul 20, 2021
1 parent 9042999 commit 1c112bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class YAMLCompletion extends JSONCompletion {

currentDoc.currentDocIndex = currentDocIndex;
return this.schemaService.getSchemaForResource(document.uri, currentDoc).then((schema) => {
if (!schema) {
if (!schema || schema.errors.length) {
return Promise.resolve(result);
}
const newSchema = schema;
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class YAMLHover {
};

return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node) {
if (schema && node && !schema.errors.length) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);

let title: string | undefined = undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export class YAMLSchemaService extends JSONSchemaService {
const highestPrioSchemas = this.highestPrioritySchemas(schemas);
const schemaHandle = super.createCombinedSchema(resource, highestPrioSchemas);
return schemaHandle.getResolvedSchema().then((schema) => {
if (schema.schema) {
if (schema.schema && typeof schema.schema !== 'string') {
schema.schema.url = schemaHandle.url;
}

Expand Down
20 changes: 18 additions & 2 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import assert = require('assert');
import path = require('path');
import { createExpectedCompletion } from './utils/verifyError';
import { ServiceSetup } from './utils/serviceSetup';
import { CompletionList, InsertTextFormat, MarkupContent, MarkupKind } from 'vscode-languageserver';
import { CompletionList, InsertTextFormat, MarkupContent, MarkupKind, Position } from 'vscode-languageserver';
import { expect } from 'chai';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { LanguageService } from '../src';
Expand All @@ -22,7 +22,10 @@ describe('Auto Completion Tests', () => {
let yamlSettings: SettingsState;

before(() => {
languageSettingsSetup = new ServiceSetup().withCompletion();
languageSettingsSetup = new ServiceSetup().withCompletion().withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
});
const { languageService: langService, languageHandler: langHandler, yamlSettings: settings } = setupLanguageService(
languageSettingsSetup.languageSettings
);
Expand Down Expand Up @@ -2064,7 +2067,20 @@ describe('Auto Completion Tests', () => {
createExpectedCompletion('and', 'and', 2, 4, 2, 4, 12, InsertTextFormat.Snippet, { documentation: undefined })
);
});

it('completion should handle bad schema', async () => {
const doc = setupSchemaIDTextDocument('foo:\n bar', 'bad-schema.yaml');
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(doc);
const result = await languageHandler.completionHandler({
position: Position.create(0, 1),
textDocument: doc,
});

expect(result.items).to.be.empty;
});
});

describe('Array completion', () => {
it('Simple array object completion with "-" without any item', async () => {
const schema = require(path.join(__dirname, './fixtures/testArrayCompletionSchema.json'));
Expand Down
20 changes: 18 additions & 2 deletions test/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { ServiceSetup } from './utils/serviceSetup';
import { SCHEMA_ID, setupLanguageService, setupSchemaIDTextDocument } from './utils/testHelper';
import { LanguageService } from '../src';
import * as assert from 'assert';
import { Hover, MarkupContent } from 'vscode-languageserver';
import { Hover, MarkupContent, Position } from 'vscode-languageserver';
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { expect } from 'chai';

describe('Hover Tests', () => {
let languageSettingsSetup: ServiceSetup;
Expand All @@ -17,7 +18,10 @@ describe('Hover Tests', () => {
let yamlSettings: SettingsState;

before(() => {
languageSettingsSetup = new ServiceSetup().withHover();
languageSettingsSetup = new ServiceSetup().withHover().withSchemaFileMatch({
uri: 'http://google.com',
fileMatch: ['bad-schema.yaml'],
});
const { languageService: langService, languageHandler: langHandler, yamlSettings: settings } = setupLanguageService(
languageSettingsSetup.languageSettings
);
Expand Down Expand Up @@ -331,5 +335,17 @@ describe('Hover Tests', () => {
`should return this description\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
);
});

it('should work with bad schema', async () => {
const doc = setupSchemaIDTextDocument('foo:\n bar', 'bad-schema.yaml');
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(doc);
const result = await languageHandler.hoverHandler({
position: Position.create(0, 1),
textDocument: doc,
});

expect(result).to.be.null;
});
});
});

0 comments on commit 1c112bf

Please sign in to comment.