From 35a69489c2f8621c8aa258ddd8dda105cd67a9e4 Mon Sep 17 00:00:00 2001 From: kkondaka <41027584+kkondaka@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:19:18 -0800 Subject: [PATCH] Fix Null Pointer Exception in KeyValue Processor (#3927) * Fix Null Pointer Exception in KeyValue Processor Signed-off-by: Krishna Kondaka * Added a test case Signed-off-by: Krishna Kondaka --------- Signed-off-by: Krishna Kondaka Co-authored-by: Krishna Kondaka --- .../processor/keyvalue/KeyValueProcessor.java | 3 +++ .../processor/keyvalue/KeyValueProcessorTests.java | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java b/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java index a062e90e48..e7121456ce 100644 --- a/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java +++ b/data-prepper-plugins/key-value-processor/src/main/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessor.java @@ -236,6 +236,9 @@ public Collection> doExecute(final Collection> recor final Map outputMap = new HashMap<>(); final Event recordEvent = record.getData(); final String groupsRaw = recordEvent.get(keyValueProcessorConfig.getSource(), String.class); + if (groupsRaw == null) { + continue; + } final String[] groups = fieldDelimiterPattern.split(groupsRaw, 0); if (keyValueProcessorConfig.getRecursive()) { diff --git a/data-prepper-plugins/key-value-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessorTests.java b/data-prepper-plugins/key-value-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessorTests.java index 1ebb910895..5558b4d450 100644 --- a/data-prepper-plugins/key-value-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessorTests.java +++ b/data-prepper-plugins/key-value-processor/src/test/java/org/opensearch/dataprepper/plugins/processor/keyvalue/KeyValueProcessorTests.java @@ -87,6 +87,18 @@ void testSingleKvToObjectKeyValueProcessor() { assertThatKeyEquals(parsed_message, "key1", "value1"); } + @Test + void testKeyValueProcessorWithoutMessage() { + final Map testData = new HashMap(); + testData.put("notMessage", "not a message"); + final Record record = buildRecordWithEvent(testData); + final List> editedRecords = (List>) keyValueProcessor.doExecute(Collections.singletonList(record)); + assertThat(editedRecords.size(), equalTo(1)); + assertThat(editedRecords.get(0), notNullValue()); + LinkedHashMap parsed_message = editedRecords.get(0).getData().get("parsed_message", LinkedHashMap.class); + assertThat(parsed_message, equalTo(null)); + } + @Test void testMultipleKvToObjectKeyValueProcessor() { final Record record = getMessage("key1=value1&key2=value2"); @@ -851,4 +863,4 @@ private void assertThatKeyEquals(final LinkedHashMap parsed_mess assertThat(parsed_message.containsKey(key), is(true)); assertThat(parsed_message.get(key), equalTo(value)); } -} \ No newline at end of file +}