Skip to content

Commit e286f04

Browse files
authored
FIX: NPE in JsonCodec with keyName (#5659)
Signed-off-by: George Chen <qchea@amazon.com>
1 parent 52a1e6d commit e286f04

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/JsonDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void parse(InputStream inputStream, Instant timeReceived, Consumer<Record
7272
}
7373

7474
if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
75-
if (keyName != null && !nodeName.equals(keyName)) {
75+
if (keyName != null && !keyName.equals(nodeName)) {
7676
continue;
7777
}
7878
parseRecordsArray(jsonParser, timeReceived, eventConsumer, includeKeysMap, includeMetadataKeysMap);

data-prepper-api/src/test/java/org/opensearch/dataprepper/model/codec/JsonDecoderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ void test_basicJsonDecoder_withInputConfig() throws IOException {
171171
assertThat(receivedTime, equalTo(now));
172172
}
173173

174+
@Test
175+
void test_JsonDecoder_withKeyName_when_parsing_json_array_should_skip() throws IOException {
176+
final Instant now = Instant.now();
177+
String inputString = "[]";
178+
List<Record<Event>> records = new ArrayList<>();
179+
jsonDecoder = new JsonDecoder(key_name, null, null, maxEventLength);
180+
jsonDecoder.parse(new ByteArrayInputStream(inputString.getBytes()), now, (record) -> {
181+
records.add(record);
182+
receivedTime = record.getData().getEventHandle().getInternalOriginationTime();
183+
});
184+
185+
assertTrue(records.isEmpty());
186+
}
187+
174188
@Test
175189
void test_basicJsonDecoder_withInputConfig_withoutEvents_empty_metadata_keys() throws IOException {
176190
final Instant now = Instant.now();

0 commit comments

Comments
 (0)