Fix NPE in CloudEventDeserializer when deserializing header with null value (#415)

Signed-off-by: Dmitrii Bocharov <dmitrii.bocharov@embedit.cz>
This commit is contained in:
Dmitrii Bocharov 2021-11-03 08:28:54 +01:00 committed by GitHub
parent 0277ee4ae4
commit 32adfe9123
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -56,6 +56,9 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
// This implementation avoids to use visitAttributes and visitExtensions
// in order to complete the visit in one loop
this.forEachHeader((key, value) -> {
if (value == null) {
return;
}
if (isContentTypeHeader(key)) {
visitor.withContextAttribute(CloudEventV1.DATACONTENTTYPE, toCloudEventsValue(value));
} else if (isCloudEventsHeader(key)) {

View File

@ -23,6 +23,7 @@ import io.cloudevents.core.mock.MyCloudEventData;
import io.cloudevents.core.test.Data;
import io.cloudevents.rw.CloudEventDataMapper;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Headers;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
@ -60,6 +61,26 @@ public class CloudEventDeserializerTest {
);
}
@Test
public void deserializerShouldWorkWithNullableManuallyDefinedHeaders() {
String topic = "test";
CloudEvent testCloudEvent = Data.V1_MIN;
CloudEventDeserializer cloudEventDeserializer = new CloudEventDeserializer();
// Serialize the event first
ProducerRecord<Void, byte[]> inRecord = KafkaMessageFactory
.createWriter(topic)
.writeBinary(testCloudEvent);
// add optional subject header with null value
Headers headers = inRecord.headers();
headers.add("ce_subject", null);
CloudEvent outEvent = cloudEventDeserializer.deserialize(topic, headers, inRecord.value());
assertThat(outEvent)
.isEqualTo(testCloudEvent);
}
private void testDeserialize(CloudEventDeserializer deserializer, CloudEvent input, CloudEvent expected) {
String topic = "test";