Skip to content

Commit 5f13da4

Browse files
[Ingest pipelines] Accept 0, false and '' in the set processor value field (elastic#246032)
Fixes elastic#220572 ## Summary This PR fixes a bug in the Set processor form in the Ingest Pipelines plugin. Previously, the value field incorrectly rejected falsy values like 0, false, and empty strings (''), treating them as invalid/empty. ### How to test * Navigate to edit pipelines UI and create a Set processor. * Verify that you can save the processor adding the following to the `Value` field: `0`, `false` or empty. * Verify that the `copy_from` still not accept empty values. * Import a processor like the following value and verify that the info is correctly displayed when editing the processor (try also to change the value field to `false` and `''`): ``` { "description": "test", "processors": [ { "set": { "field": "test", "value": 0 } } ] } ``` Co-authored-by: Elena Stoeva <[email protected]>
1 parent 5944170 commit 5f13da4

File tree

2 files changed

+72
-20
lines changed
  • x-pack/platform/plugins/shared/ingest_pipelines/public/application/components/pipeline_editor

2 files changed

+72
-20
lines changed

x-pack/platform/plugins/shared/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/set.test.tsx

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ describe('Processor: Set', () => {
5555
await saveNewProcessor();
5656

5757
// Expect form error as "field" is required parameter
58-
expect(form.getErrorsMessages()).toEqual([
59-
'A field value is required.',
60-
'A value is required.',
61-
]);
58+
expect(form.getErrorsMessages()).toEqual(['A field value is required.']);
6259
});
6360

6461
test('saves with default parameter value', async () => {
@@ -193,4 +190,59 @@ describe('Processor: Set', () => {
193190
value: { value_1: 'aaa\"bbb', value_2: 'aaa(bbb' },
194191
});
195192
});
193+
194+
test('saves with empty string as value', async () => {
195+
const {
196+
actions: { saveNewProcessor },
197+
form,
198+
} = testBed;
199+
200+
form.setInputValue('fieldNameField.input', 'field_1');
201+
202+
form.setInputValue('textValueField.input', '');
203+
204+
await saveNewProcessor();
205+
206+
const processors = getProcessorValue(onUpdate, SET_TYPE);
207+
expect(processors[0][SET_TYPE]).toEqual({
208+
field: 'field_1',
209+
value: '',
210+
});
211+
});
212+
213+
test('saves with "0" as value', async () => {
214+
const {
215+
actions: { saveNewProcessor },
216+
form,
217+
} = testBed;
218+
219+
form.setInputValue('fieldNameField.input', 'field_1');
220+
221+
form.setInputValue('textValueField.input', '0');
222+
223+
await saveNewProcessor();
224+
225+
const processors = getProcessorValue(onUpdate, SET_TYPE);
226+
// "0" is JSON-parsed to the number 0 during serialization
227+
expect(processors[0][SET_TYPE].field).toEqual('field_1');
228+
expect(processors[0][SET_TYPE].value).toEqual(0);
229+
});
230+
231+
test('saves with "false" as value', async () => {
232+
const {
233+
actions: { saveNewProcessor },
234+
form,
235+
} = testBed;
236+
237+
form.setInputValue('fieldNameField.input', 'field_1');
238+
239+
form.setInputValue('textValueField.input', 'false');
240+
241+
await saveNewProcessor();
242+
243+
const processors = getProcessorValue(onUpdate, SET_TYPE);
244+
// "false" is JSON-parsed to the boolean false during serialization
245+
expect(processors[0][SET_TYPE].field).toEqual('field_1');
246+
expect(processors[0][SET_TYPE].value).toEqual(false);
247+
});
196248
});

x-pack/platform/plugins/shared/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/set.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ const fieldsConfig: FieldsConfig = {
9494
deserializer: (value: string | object) => {
9595
return isXJsonValue(value) ? to.xJsonString(value) : value;
9696
},
97-
serializer: (value: string) => {
98-
return isXJsonValue(value) ? value : from.emptyStringToUndefined(value);
99-
},
10097
label: i18n.translate('xpack.ingestPipelines.pipelineEditor.setForm.valueFieldLabel', {
10198
defaultMessage: 'Value',
10299
}),
@@ -109,7 +106,9 @@ const fieldsConfig: FieldsConfig = {
109106
validations: [
110107
{
111108
validator: ({ value, path, formData }) => {
112-
if (isEmpty(value) && isUndefined(formData['fields.copy_from'])) {
109+
// Only require a value if it's undefined and copy_from is also not defined.
110+
// Empty strings, 0, and false are valid values.
111+
if (isUndefined(value) && isUndefined(formData['fields.copy_from'])) {
113112
return {
114113
path,
115114
message: i18n.translate('xpack.ingestPipelines.pipelineEditor.requiredValue', {
@@ -220,19 +219,20 @@ export const SetProcessor: FunctionComponent = () => {
220219
})}
221220
/>
222221

223-
<UseField
224-
config={fieldsConfig.value}
225-
component={XJsonToggle}
226-
path="fields.value"
227-
componentProps={{
228-
disabled: isCopyFromEnabled,
229-
handleIsJson: getIsJsonValue,
230-
fieldType: 'text',
231-
}}
232-
validationData={isDefineAsJson}
233-
/>
222+
{!isCopyFromEnabled && (
223+
<UseField
224+
config={fieldsConfig.value}
225+
component={XJsonToggle}
226+
path="fields.value"
227+
componentProps={{
228+
handleIsJson: getIsJsonValue,
229+
fieldType: 'text',
230+
}}
231+
validationData={isDefineAsJson}
232+
/>
233+
)}
234234

235-
{hasTemplateSnippet(fields?.value) && (
235+
{!isCopyFromEnabled && hasTemplateSnippet(fields?.value) && (
236236
<UseField
237237
componentProps={{
238238
euiFieldProps: {

0 commit comments

Comments
 (0)