From f1d09a26256fcb69aad40dd4581114959188a66a Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 14 Jul 2021 10:24:37 +0300 Subject: [PATCH] Fix bad schema handle Signed-off-by: Yevhen Vydolob --- .../services/yamlCompletion.ts | 2 +- src/languageservice/services/yamlHover.ts | 2 +- .../services/yamlSchemaService.ts | 2 +- test/autoCompletion.test.ts | 20 +++++++++++++++++-- test/hover.test.ts | 20 +++++++++++++++++-- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index f3e6a129..e36584a8 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -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; diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index e8d885c8..e609caa1 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -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; diff --git a/src/languageservice/services/yamlSchemaService.ts b/src/languageservice/services/yamlSchemaService.ts index bacd5364..a9df8644 100644 --- a/src/languageservice/services/yamlSchemaService.ts +++ b/src/languageservice/services/yamlSchemaService.ts @@ -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; } diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index 7d2c581e..bb5e464c 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -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'; @@ -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 ); @@ -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')); diff --git a/test/hover.test.ts b/test/hover.test.ts index ecb26055..3ca96a51 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -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; @@ -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 ); @@ -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; + }); }); });