File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
main/java/org/springframework/kafka/support
test/java/org/springframework/kafka/support Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change 4444 * @author Gary Russell
4545 * @author Artem Bilan
4646 * @author Sanghyeok An
47+ * @author Soby Chacko
4748 *
4849 * @since 2.1.3
4950 *
@@ -268,11 +269,11 @@ else if (value instanceof String) {
268269 * @return the value to add.
269270 */
270271 protected Object headerValueToAddIn (Header header ) {
271- Object mapped = mapRawIn (header .key (), header .value ());
272- if (mapped == null ) {
273- mapped = header .value ();
272+ if (header == null || header .value () == null ) {
273+ return null ;
274274 }
275- return mapped ;
275+ String mapped = mapRawIn (header .key (), header .value ());
276+ return mapped != null ? mapped : header .value ();
276277 }
277278
278279 @ Nullable
Original file line number Diff line number Diff line change 4747
4848import static org .assertj .core .api .Assertions .assertThat ;
4949import static org .assertj .core .api .Assertions .entry ;
50+ import static org .mockito .BDDMockito .given ;
51+ import static org .mockito .Mockito .mock ;
52+ import static org .mockito .Mockito .never ;
53+ import static org .mockito .Mockito .verify ;
5054
5155/**
5256 * @author Gary Russell
@@ -360,6 +364,20 @@ void deserializationExceptionHeadersAreMappedAsNonByteArray() {
360364 assertThat (headers .lastHeader (SerializationUtils .VALUE_DESERIALIZER_EXCEPTION_HEADER )).isNull ();
361365 }
362366
367+ @ Test
368+ void ensureNullHeaderValueHandledGraciously () {
369+ DefaultKafkaHeaderMapper mapper = new DefaultKafkaHeaderMapper ();
370+
371+ Header mockHeader = mock (Header .class );
372+ given (mockHeader .value ()).willReturn (null );
373+
374+ Object result = mapper .headerValueToAddIn (mockHeader );
375+
376+ assertThat (result ).isNull ();
377+ verify (mockHeader ).value ();
378+ verify (mockHeader , never ()).key ();
379+ }
380+
363381 public static final class Foo {
364382
365383 private String bar = "bar" ;
You can’t perform that action at this time.
0 commit comments