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

GH-3581: Address NPE in AbstractKafkaHeaderMapper #3582

Merged
merged 1 commit into from
Oct 21, 2024
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 @@ -44,6 +44,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Sanghyeok An
* @author Soby Chacko
*
* @since 2.1.3
*
Expand Down Expand Up @@ -268,11 +269,11 @@ else if (value instanceof String) {
* @return the value to add.
*/
protected Object headerValueToAddIn(Header header) {
Object mapped = mapRawIn(header.key(), header.value());
if (mapped == null) {
mapped = header.value();
if (header == null || header.value() == null) {
return null;
}
return mapped;
String mapped = mapRawIn(header.key(), header.value());
return mapped != null ? mapped : header.value();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
* @author Gary Russell
Expand Down Expand Up @@ -360,6 +364,20 @@ void deserializationExceptionHeadersAreMappedAsNonByteArray() {
assertThat(headers.lastHeader(SerializationUtils.VALUE_DESERIALIZER_EXCEPTION_HEADER)).isNull();
}

@Test
void ensureNullHeaderValueHandledGraciously() {
DefaultKafkaHeaderMapper mapper = new DefaultKafkaHeaderMapper();

Header mockHeader = mock(Header.class);
given(mockHeader.value()).willReturn(null);

Object result = mapper.headerValueToAddIn(mockHeader);

assertThat(result).isNull();
verify(mockHeader).value();
verify(mockHeader, never()).key();
}

public static final class Foo {

private String bar = "bar";
Expand Down
Loading