Skip to content

Commit 588b2da

Browse files
fix enum values to be unique (#1028)
* fix enum values to be unique fix enum values to be unique * when several schemas get combined (e.g., `oneOf`, `anyOf`) the available values for an `enum` could get merged * this could lead to enum values present several time * this PR only adds an enum values to the merged list, if its value is not present so far * corresponding unit test is present as well example: schema: ```json { "type": "object", "properties": { "animal": { "description": "should return this description", "anyOf": [ { "enum": [ "cat", "dog", "non" ], "enumDescriptions": [ "", "Canis familiaris" ] }, { "enum": [ "bird", "fish", "non" ], "enumDescriptions": [ "", "Special fish" ] } ] } } } ``` Without the fix the `available value` list is: `[ cat, dog, non, bird, fish, non ]` with `non` being present twice. With the fix the second `non` is removed, leaving only: `[ cat, dog, non, bird, fish ]` * Update yamlHover.ts --------- Co-authored-by: Muthurajan Sivasubramanian <[email protected]>
1 parent 8d95fb4 commit 588b2da

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/languageservice/services/yamlHover.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ export class YAMLHover {
131131
if (typeof enumValue !== 'string') {
132132
enumValue = JSON.stringify(enumValue);
133133
}
134-
markdownEnums.push({
135-
value: enumValue,
136-
description: markdownEnumDescriptions[idx],
137-
});
134+
//insert only if the value is not present yet (avoiding duplicates)
135+
if (!markdownEnums.some((me) => me.value === enumValue)) {
136+
markdownEnums.push({
137+
value: enumValue,
138+
description: markdownEnumDescriptions[idx],
139+
});
140+
}
138141
});
139142
}
140143
if (s.schema.anyOf && isAllSchemasMatched(node, matchingSchemas, s.schema)) {

test/hover.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,46 @@ Allowed Values:
583583
* \`dog\`: Canis familiaris
584584
* \`non\`
585585
586+
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
587+
);
588+
});
589+
590+
it('Hover displays unique enum values', async () => {
591+
schemaProvider.addSchema(SCHEMA_ID, {
592+
type: 'object',
593+
properties: {
594+
animal: {
595+
description: 'should return this description',
596+
anyOf: [
597+
{
598+
enum: ['cat', 'dog', 'non'],
599+
enumDescriptions: ['', 'Canis familiaris'],
600+
},
601+
{
602+
enum: ['bird', 'fish', 'non'], // the second "non" from this enum should be filtered out
603+
enumDescriptions: ['', 'Special fish'],
604+
},
605+
],
606+
},
607+
},
608+
});
609+
const content = 'animal:\n no|n|'; // len: 13, pos: 12
610+
const result = await parseSetup(content);
611+
612+
assert.strictEqual(MarkupContent.is(result.contents), true);
613+
assert.strictEqual((result.contents as MarkupContent).kind, 'markdown');
614+
assert.strictEqual(
615+
(result.contents as MarkupContent).value,
616+
`should return this description
617+
618+
Allowed Values:
619+
620+
* \`cat\`
621+
* \`dog\`: Canis familiaris
622+
* \`non\`
623+
* \`bird\`
624+
* \`fish\`: Special fish
625+
586626
Source: [${SCHEMA_ID}](file:///${SCHEMA_ID})`
587627
);
588628
});

0 commit comments

Comments
 (0)