Skip to content

Commit a00de94

Browse files
committed
Addressed review comments
Signed-off-by: Kondaka <krishkdk@amazon.com>
1 parent dc647d3 commit a00de94

7 files changed

Lines changed: 47 additions & 9 deletions

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/Event.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public interface Event extends Serializable {
3535
*/
3636
void put(EventKey key, Object value);
3737

38+
/**
39+
* Adds or updates the key with a given value in the Event
40+
*
41+
* @param key where the value will be set
42+
* @param value value to set the key to
43+
* @param repalceInvalidCharacters flag indicating if invalid characters should be replaced or not
44+
* @since 2.13
45+
*/
46+
void put(String key, Object value, boolean replaceInvalidCharacters);
47+
3848
/**
3949
* Adds or updates the key with a given value in the Event
4050
*

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/event/JacksonEvent.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ public void put(EventKey key, Object value) {
166166
}
167167
}
168168

169+
@Override
170+
public void put(String key, final Object value, final boolean replaceInvalidCharacters) {
171+
if (replaceInvalidCharacters) {
172+
key = JacksonEventKey.replaceInvalidCharacters(key);
173+
}
174+
put(key, value);
175+
}
176+
169177
/**
170178
* Adds or updates the key with a given value in the Event.
171179
*
@@ -261,7 +269,6 @@ public <T> T get(final String key, final Class<T> clazz) {
261269
return get(jacksonEventKey, clazz);
262270
}
263271

264-
265272
public static String replaceInvalidKeyChars(final String key) {
266273
return JacksonEventKey.replaceInvalidCharacters(key);
267274
}

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/JacksonEventKeyTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ public Stream<? extends Arguments> provideArguments(final ExtensionContext exten
196196
}
197197
}
198198

199+
@ParameterizedTest
200+
@ValueSource(strings = {"key 1", "key$1", "key&1", "key^1", "key%1", "key_1"})
201+
public void testReplaceInvalidKeyChars(final String key) {
202+
assertThat(JacksonEventKey.replaceInvalidCharacters(key), equalTo("key_1"));
203+
assertThat(JacksonEventKey.replaceInvalidCharacters(null), equalTo(null));
204+
}
205+
199206
@ParameterizedTest
200207
@EnumSource(EventKeyFactory.EventAction.class)
201208
void equals_returns_true_for_same_key_and_actions(final EventKeyFactory.EventAction eventAction) {

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/event/JacksonEventTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,30 @@ public void testPutKeyCannotBeEmptyString() {
230230
}
231231

232232
@ParameterizedTest
233-
@ValueSource(strings = {"key 1", "key$1", "key&1", "key^1", "key%1"})
233+
@ValueSource(strings = {"key 1", "key$1", "key&1", "key^1", "key%1", "key_1"})
234234
public void testReplaceInvalidKeyChars(final String key) {
235235
assertThat(JacksonEvent.replaceInvalidKeyChars(key), equalTo("key_1"));
236236
assertThat(JacksonEvent.replaceInvalidKeyChars(key.substring(0,3)), equalTo("key"));
237237
assertThat(JacksonEvent.replaceInvalidKeyChars(null), equalTo(null));
238238
}
239239

240+
@ParameterizedTest
241+
@ValueSource(strings = {"key 1", "key$1", "key&1", "key^1", "key%1", "key_1"})
242+
public void testPutWithReplaceInvalidKeyChars(final String key) {
243+
final String value = UUID.randomUUID().toString();
244+
245+
event.put(key, value, true);
246+
assertThat(event.get("key_1", String.class), equalTo(value));
247+
}
248+
249+
@ParameterizedTest
250+
@ValueSource(strings = {"key 1", "key$1", "key&1", "key^1", "key%1"})
251+
public void testPutWithoutReplaceInvalidKeyChars(final String key) {
252+
final String value = UUID.randomUUID().toString();
253+
254+
assertThrows(IllegalArgumentException.class, () -> event.put(key, value, false));
255+
}
256+
240257
@Test
241258
public void testPutAndGet_withMultiLevelKey() {
242259
final String key = "foo/bar";
@@ -1104,7 +1121,7 @@ void testJsonStringBuilderWithIncludeKeys() {
11041121

11051122

11061123
}
1107-
1124+
11081125
@Test
11091126
void testJsonStringBuilderWithExcludeKeys() {
11101127
final String jsonString = "{\"id\":1,\"foo\":\"bar\",\"info\":{\"name\":\"hello\",\"foo\":\"bar\"},\"tags\":[{\"key\":\"a\",\"value\":\"b\"},{\"key\":\"c\",\"value\":\"d\"}]}";

data-prepper-plugins/csv-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/csv/CsvProcessor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,7 @@ private void putDataInEvent(final Event event, final List<String> header, final
214214
int providedHeaderColIdx = 0;
215215
for (; providedHeaderColIdx < header.size() && providedHeaderColIdx < data.size(); providedHeaderColIdx++) {
216216
String key = header.get(providedHeaderColIdx);
217-
if (normalizeKeys) {
218-
key = JacksonEvent.replaceInvalidKeyChars(key);
219-
}
220-
event.put(key, data.get(providedHeaderColIdx));
217+
event.put(key, data.get(providedHeaderColIdx), normalizeKeys);
221218
}
222219
for (int remainingColIdx = providedHeaderColIdx; remainingColIdx < data.size(); remainingColIdx++) {
223220
event.put(generateColumnHeader(remainingColIdx), data.get(remainingColIdx));

data-prepper-plugins/csv-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/csv/CsvProcessorConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class CsvProcessorConfig {
4343
private Boolean deleteHeader = DEFAULT_DELETE_HEADERS;
4444

4545
@JsonProperty(value = "normalize_keys", defaultValue = "false")
46-
@JsonPropertyDescription("If specified, replaces invalid characters with underscore character")
46+
@JsonPropertyDescription("If set to true, replaces invalid characters with underscore character")
4747
private Boolean normalizeKeys = false;
4848

4949

data-prepper-plugins/parse-json-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/parse/AbstractParseProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected void replaceInvalidChars(Map<String, Object> map) {
127127
if (value instanceof Map) {
128128
replaceInvalidChars((Map<String, Object>)value);
129129
}
130-
if (newKey != key) {
130+
if (!newKey.equals(key)) {
131131
toAdd.put(newKey, value);
132132
iterator.remove();
133133
}

0 commit comments

Comments
 (0)