Skip to content

Commit bf0204b

Browse files
authored
Fix empty_value handling in CsvProcessor (#55649) (#55968)
* Fix empty_value handling in CsvProcessor Due to bug in `CsvProcessor.Factory` it was impossible to specify `empty_value`. This change fixes that and adds relevant test. Closes #55643 * assert changed
1 parent 9cd3175 commit bf0204b

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/CsvProcessor.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ public final class CsvProcessor extends AbstractProcessor {
4545

4646
public static final String TYPE = "csv";
4747

48-
private final String field;
49-
private final String[] headers;
50-
private final boolean trim;
51-
private final char quote;
52-
private final char separator;
53-
private final boolean ignoreMissing;
54-
private final Object emptyValue;
48+
//visible for testing
49+
final String field;
50+
final String[] headers;
51+
final boolean trim;
52+
final char quote;
53+
final char separator;
54+
final boolean ignoreMissing;
55+
final Object emptyValue;
5556

5657
CsvProcessor(String tag, String field, String[] headers, boolean trim, char separator, char quote, boolean ignoreMissing,
5758
Object emptyValue) {
@@ -101,7 +102,7 @@ public CsvProcessor create(Map<String, org.elasticsearch.ingest.Processor.Factor
101102
}
102103
boolean trim = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "trim", false);
103104
Object emptyValue = null;
104-
if(config.containsKey("emptyValue")){
105+
if(config.containsKey("empty_value")){
105106
emptyValue = ConfigurationUtils.readObject(TYPE, processorTag, config, "empty_value");
106107
}
107108
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.ingest.common;
21+
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
import java.util.Collections;
25+
import java.util.HashMap;
26+
27+
import static java.util.Collections.emptyMap;
28+
import static org.hamcrest.Matchers.equalTo;
29+
import static org.hamcrest.Matchers.is;
30+
import static org.hamcrest.Matchers.notNullValue;
31+
32+
public class CsvProcessorFactoryTests extends ESTestCase {
33+
34+
public void testProcessorIsCreated() {
35+
CsvProcessor.Factory factory = new CsvProcessor.Factory();
36+
HashMap<String, Object> properties = new HashMap<>();
37+
properties.put("field", "field");
38+
properties.put("target_fields", Collections.singletonList("target"));
39+
properties.put("quote", "|");
40+
properties.put("separator", "/");
41+
properties.put("empty_value", "empty");
42+
properties.put("trim", true);
43+
properties.put("ignore_missing", true);
44+
CsvProcessor csv = factory.create(null, "csv", properties);
45+
assertThat(csv, notNullValue());
46+
assertThat(csv.field, equalTo("field"));
47+
assertThat(csv.headers, equalTo(new String[]{"target"}));
48+
assertThat(csv.quote, equalTo('|'));
49+
assertThat(csv.separator, equalTo('/'));
50+
assertThat(csv.emptyValue, equalTo("empty"));
51+
assertThat(csv.trim, equalTo(true));
52+
assertThat(csv.ignoreMissing, equalTo(true));
53+
assertThat(properties, is(emptyMap()));
54+
}
55+
}

0 commit comments

Comments
 (0)