From 062ed122c9283f7337397b69045fc26ac40aff9f Mon Sep 17 00:00:00 2001 From: Yannik Tausch Date: Tue, 7 Jan 2025 15:55:46 +0100 Subject: [PATCH] display parent description for anyOf --- src/languageservice/services/yamlHover.ts | 4 +- test/hover.test.ts | 72 ++++++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index f02c29d1..f30eab3a 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 5dbc6d55..e7017d57 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -774,7 +774,7 @@ Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})` title: 'ZIP file', anyOf: [{ type: "string", pattern: "\\.zip$" }, { type: "null" }], default: null, - description: "Optional ZIP file path." + description: "Optional ZIP file path.", }, }, required: ['optionalZipFile'], @@ -790,6 +790,76 @@ Source: [${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'], + }, + }, + }); + + let content = 'ch|i|ld:'; + let 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', () => {