Skip to content

Commit 5a5cce7

Browse files
authored
Keeping the backward compatibility when empty array added (#6084)
* Keeping the backward compatibility when empty array added to a key using AddEntryProcessor. Signed-off-by: Santhosh Gandhe <1909520+san81@users.noreply.github.com>
1 parent d75ab26 commit 5a5cce7

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

data-prepper-plugins/mutate-event-processors/src/main/java/org/opensearch/dataprepper/plugins/processor/mutateevent/AddEntryProcessor.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,19 +362,14 @@ private void mergeValueToMap(final Map<String, Object> item, final String key, f
362362

363363
private void mergeValue(final Object value, Supplier<Object> getter, Consumer<Object> setter) {
364364
final Object currentValue = getter.get();
365-
final List<Object> mergedValue;
365+
final List<Object> mergedValue = new ArrayList<>();
366366
if (currentValue instanceof List) {
367-
mergedValue = new ArrayList<>((List<Object>)currentValue);
367+
mergedValue.addAll((List<Object>) currentValue);
368368
} else {
369-
mergedValue = new ArrayList<>(2);
370369
mergedValue.add(currentValue);
371370
}
372-
373-
if (value instanceof List) {
374-
mergedValue.addAll((List<Object>)value);
375-
} else {
376-
mergedValue.add(value);
377-
}
371+
372+
mergedValue.add(value);
378373
setter.accept(mergedValue);
379374
}
380375
}

data-prepper-plugins/mutate-event-processors/src/test/java/org/opensearch/dataprepper/plugins/processor/mutateevent/AddEntryProcessorTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.hamcrest.CoreMatchers.equalTo;
3232
import static org.hamcrest.CoreMatchers.is;
3333
import static org.hamcrest.MatcherAssert.assertThat;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
3435
import static org.junit.jupiter.api.Assertions.assertThrows;
3536
import static org.mockito.ArgumentMatchers.any;
3637
import static org.mockito.ArgumentMatchers.eq;
@@ -64,6 +65,39 @@ void invalid_add_when_throws_InvalidPluginConfigurationException() {
6465
assertThrows(InvalidPluginConfigurationException.class, this::createObjectUnderTest);
6566
}
6667

68+
@Test
69+
public void test_add_empty_array_followed_by_list_of_map_to_the_same_key() {
70+
when(mockConfig.getEntries()).thenReturn(createListOfEntries(
71+
createEntry("actor/user/groups", null, new ArrayList<>(), null, null, false, false, null, null, true, null),
72+
createEntry("actor/user/groups", null, List.of(Map.of("name", "")), null, null, false, true, null, null, true, null)));
73+
74+
final AddEntryProcessor processor = createObjectUnderTest();
75+
final List<Map<String, Object>> mapList = List.of(Map.of("testKey", "testValue"));
76+
final Map<String, Object> data = Map.of("message", mapList);
77+
final Record<Event> record = getEvent(mapList);
78+
79+
final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record));
80+
assertEquals("{\"message\":[{\"testKey\":\"testValue\"}],\"actor\":{\"user\":{\"groups\":[[{\"name\":\"\"}]]}}}",
81+
editedRecords.get(0).getData().toJsonString());
82+
}
83+
84+
@Test
85+
public void test_add_list_of_map_followed_by_empty_array_to_the_same_key() {
86+
when(mockConfig.getEntries()).thenReturn(createListOfEntries(
87+
createEntry("actor/user/groups", null, List.of(Map.of("name", "")), null, null, false, true, null, null, true, null),
88+
createEntry("actor/user/groups", null, new ArrayList<>(), null, null, false, false, null, null, true, null),
89+
createEntry("actor/user/groups", null, List.of(Map.of("second", "slistval")), null, null, false, true, null, null, true, null)));
90+
91+
final AddEntryProcessor processor = createObjectUnderTest();
92+
final List<Map<String, Object>> mapList = List.of(Map.of("testKey", "testValue"));
93+
final Map<String, Object> data = Map.of("message", mapList);
94+
final Record<Event> record = getEvent(mapList);
95+
96+
final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record));
97+
assertEquals("{\"message\":[{\"testKey\":\"testValue\"}],\"actor\":{\"user\":{\"groups\":[{\"name\":\"\"},[{\"second\":\"slistval\"}]]}}}",
98+
editedRecords.get(0).getData().toJsonString());
99+
}
100+
67101
@Test
68102
public void testSingleAddProcessorTests() {
69103
when(mockConfig.getEntries()).thenReturn(createListOfEntries(createEntry("newMessage", null, 3, null, null, false, false,null, null, true, null)));
@@ -359,7 +393,7 @@ public void testListMergeOptimization() {
359393
final Record<Event> record = getEvent(currentList);
360394
final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record));
361395

362-
assertThat(editedRecords.get(0).getData().get("message", List.class), equalTo(Arrays.asList(1, 2, 3, 4, 5)));
396+
assertThat(editedRecords.get(0).getData().get("message", List.class), equalTo(Arrays.asList(1, 2, 3, Arrays.asList(4, 5))));
363397
}
364398

365399
@Test

0 commit comments

Comments
 (0)