Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ export class YamlCompletion {
case 'anyOf': {
let value = propertySchema.default !== undefined ? propertySchema.default : propertySchema.const;
if (value !== undefined) {
if (type === 'string') {
if (type === 'string' && typeof value === 'string') {
value = toYamlStringScalar(value);
}
insertText += `${indent}${key}: \${${insertIndex++}:${value}}\n`;
Expand Down Expand Up @@ -1227,9 +1227,12 @@ export class YamlCompletion {
key === 'null' ? this.getInsertTextForValue(key, '', 'string') : key
}: \${${insertIndex++}:${propertySchema.default}}\n`;
break;
case 'string':
insertText += `${indent}${key}: \${${insertIndex++}:${toYamlStringScalar(propertySchema.default)}}\n`;
case 'string': {
const value =
typeof propertySchema.default === 'string' ? toYamlStringScalar(propertySchema.default) : propertySchema.default;
insertText += `${indent}${key}: \${${insertIndex++}:${value}}\n`;
break;
}
case 'array':
case 'object':
// TODO: support default value for array object
Expand Down
30 changes: 30 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ describe('Auto Completion Fix Tests', () => {
languageService.configure(languageSettingsSetup.languageSettings);
});

describe('nullable string defaults in object snippets', () => {
for (const required of [false, true]) {
for (const value of [null, 'null']) {
it(`should retain completions for ${required ? 'required' : 'optional'} properties with default ${JSON.stringify(value)}`, async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
first: { type: 'integer' },
second: {
type: 'object',
required: required ? ['name'] : [],
properties: {
name: { type: ['string', 'null'], default: value },
},
},
third: { type: 'integer' },
},
});

const completion = await parseSetup('', 0, 0);
expect(completion.items.map((item) => item.label)).to.include.members(['first', 'second', 'third']);
const expectedValue = value === null ? 'null' : '"null"';
expect(completion.items.find((item) => item.label === 'second').insertText).to.equal(
`second:\n name: \${1:${expectedValue}}`
);
});
}
}
});

it('should show completion on map under array', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'array',
Expand Down
Loading