Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NPE in CloudEventDeserializer when deserializing with optional header with null value #415

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public <T extends CloudEventWriter<V>, V> V read(CloudEventWriterFactory<T, V> w
// 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -60,6 +61,26 @@ public void deserializerWithMapper() {
);
}

@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";

Expand Down