diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index f02c29d1..39dbac91 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -103,7 +103,7 @@ export class YAMLHover { }; const removePipe = (value: string): string => { - return value.replace(/\|\|\s*$/, ''); + return value.replace(/\s\|\|\s*$/, ''); }; return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => { @@ -141,7 +141,7 @@ export class YAMLHover { if (s.schema.anyOf && isAllSchemasMatched(node, matchingSchemas, s.schema)) { //if append title and description of all matched schemas on hover title = ''; - markdownDescription = ''; + markdownDescription = s.schema.description ? s.schema.description + '\n' : ''; s.schema.anyOf.forEach((childSchema: JSONSchema, index: number) => { title += childSchema.title || s.schema.closestTitle || ''; markdownDescription += childSchema.markdownDescription || this.toMarkdown(childSchema.description) || ''; diff --git a/test/hover.test.ts b/test/hover.test.ts index 404ed676..9fd6041e 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -764,6 +764,102 @@ Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})` ); expect(telemetry.messages).to.be.empty; }); + it('should show the parent description in anyOf (no child descriptions)', async () => { + schemaProvider.addSchema(SCHEMA_ID, { + title: 'The Root', + description: 'Root Object', + type: 'object', + properties: { + optionalZipFile: { + title: 'ZIP file', + anyOf: [{ type: 'string', pattern: '\\.zip$' }, { type: 'null' }], + default: null, + description: 'Optional ZIP file path.', + }, + }, + required: ['optionalZipFile'], + additionalProperties: false, + }); + const content = 'optionalZipF|i|le:'; + const result = await parseSetup(content); + + assert.strictEqual(MarkupContent.is(result.contents), true); + assert.strictEqual( + (result.contents as MarkupContent).value, + `#### ZIP file || ZIP file\n\nOptional ZIP file path.\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})` + ); + expect(telemetry.messages).to.be.empty; + }); + it('should concat parent and child descriptions in anyOf', async () => { + schemaProvider.addSchema(SCHEMA_ID, { + title: 'The Root', + description: 'Root Object', + type: 'object', + properties: { + child: { + title: 'Child', + anyOf: [ + { + $ref: '#/definitions/FirstChoice', + }, + { + $ref: '#/definitions/SecondChoice', + }, + ], + description: 'The parent description.', + }, + }, + required: ['child'], + additionalProperties: false, + definitions: { + FirstChoice: { + title: 'FirstChoice', + description: 'The first choice', + type: 'object', + properties: { + choice: { + title: 'Choice', + default: 'first', + enum: ['first'], + type: 'string', + }, + property_a: { + title: 'Property A', + type: 'string', + }, + }, + required: ['property_a'], + }, + SecondChoice: { + title: 'SecondChoice', + description: 'The second choice', + type: 'object', + properties: { + choice: { + title: 'Choice', + default: 'second', + enum: ['second'], + type: 'string', + }, + property_b: { + title: 'Property B', + type: 'string', + }, + }, + required: ['property_b'], + }, + }, + }); + + const content = 'ch|i|ld:'; + const result = await parseSetup(content); + assert.strictEqual(MarkupContent.is(result.contents), true); + assert.strictEqual( + (result.contents as MarkupContent).value, + `#### FirstChoice || SecondChoice\n\nThe parent description.\nThe first choice || The second choice\n\nSource: [${SCHEMA_ID}](file:///${SCHEMA_ID})` + ); + expect(telemetry.messages).to.be.empty; + }); }); describe('Bug fixes', () => {