Skip to content

Commit 01d05cf

Browse files
denoucheCopilot
authored andcommitted
fix: NPE in KafkaSteps when consuming records with null header values (#795)
* Initial plan * Initial plan * Fix NPE when consuming Kafka records with null header values Co-authored-by: denouche <3081451+denouche@users.noreply.github.com> * Fix NullPointerException when Kafka header value is null Co-authored-by: denouche <3081451+denouche@users.noreply.github.com> * Use HashMap collector to support null header values Co-authored-by: denouche <3081451+denouche@users.noreply.github.com> * fix tests --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: denouche <3081451+denouche@users.noreply.github.com>
1 parent 7ec9cb1 commit 01d05cf

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

tzatziki-spring-kafka/src/main/java/com/decathlon/tzatziki/steps/KafkaSteps.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private ProducerRecord<String, GenericRecord> mapToAvroRecord(Schema schema, Str
417417

418418
ProducerRecord<String, GenericRecord> producerRecord = new ProducerRecord<>(topic, messageKey, genericRecordMessage);
419419
((Map<String, String>) avroRecord.get("headers"))
420-
.forEach((key, value) -> producerRecord.headers().add(key, value.getBytes(UTF_8)));
420+
.forEach((key, value) -> producerRecord.headers().add(key, value != null ? value.getBytes(UTF_8) : null));
421421

422422
return producerRecord;
423423
}
@@ -479,7 +479,7 @@ public ProducerRecord<String, String> mapToJsonRecord(String topic, Map<?, Objec
479479
String messageKey = (String) jsonRecord.get("key");
480480
ProducerRecord<String, String> producerRecord = new ProducerRecord<>(topic, messageKey, Mapper.toJson(jsonRecord.get("value")));
481481
((Map<String, String>) jsonRecord.get("headers"))
482-
.forEach((key, value) -> producerRecord.headers().add(key, value.getBytes(UTF_8)));
482+
.forEach((key, value) -> producerRecord.headers().add(key, value!=null ? value.getBytes(UTF_8):null));
483483

484484
return producerRecord;
485485
}
@@ -544,7 +544,9 @@ private List<Map<String, Object>> consumerRecordsToMaps(ConsumerRecords<?, ?> re
544544
return StreamSupport.stream(records.spliterator(), false)
545545
.map(record -> {
546546
Map<String, String> headers = Stream.of(record.headers().toArray())
547-
.collect(Collectors.toMap(Header::key, header -> new String(header.value())));
547+
.collect(HashMap::new,
548+
(map, header) -> map.put(header.key(), header.value() != null ? new String(header.value()) : null),
549+
HashMap::putAll);
548550
Map<String, Object> value = Mapper.read(record.value().toString());
549551
String messageKey = record.key() != null ? String.valueOf(record.key()) : "";
550552
return Map.of("value", value, "headers", headers, "key", messageKey);

tzatziki-spring-kafka/src/test/resources/com/decathlon/tzatziki/steps/kafka.feature

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,44 @@ Feature: to interact with a spring boot service having a connection to a kafka q
426426
"""
427427
And the exposed-users topic contains 1 user
428428

429+
Scenario: we can handle null header values in messages
430+
When this user is consumed from the json-users-with-key topic:
431+
"""yml
432+
headers:
433+
uuid: some-id
434+
nullable-header: null
435+
value:
436+
id: 1
437+
name: bob
438+
key: a-key
439+
"""
440+
Then we have received 1 message on the topic json-users-with-key
441+
442+
Scenario: we can handle null header values sent on a topic
443+
When this json message is published on the json-users topic:
444+
"""yml
445+
headers:
446+
uuid: some-id
447+
nullable-header: null
448+
empty-header: ""
449+
value:
450+
id: 1
451+
name: bob
452+
key: a-key
453+
"""
454+
Then the json-users topic contains only this json message:
455+
"""yml
456+
headers:
457+
uuid: some-id
458+
nullable-header: null
459+
empty-header: ""
460+
value:
461+
id: 1
462+
name: bob
463+
key: a-key
464+
"""
465+
And the json-users topic contains 1 json message
466+
429467
Scenario: we can assert that no message has been sent to a topic
430468
* the exposed-users topic contains 0 user
431469

0 commit comments

Comments
 (0)