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 4343 *
4444 * @author Gary Russell
4545 * @author Artem Bilan
46+ * @author Soby Chacko
4647 *
4748 * @since 2.1.3
4849 *
@@ -267,11 +268,11 @@ else if (value instanceof String) {
267268 * @return the value to add.
268269 */
269270 protected Object headerValueToAddIn (Header header ) {
270- Object mapped = mapRawIn (header .key (), header .value ());
271- if (mapped == null ) {
272- mapped = header .value ();
271+ if (header == null || header .value () == null ) {
272+ return null ;
273273 }
274- return mapped ;
274+ String mapped = mapRawIn (header .key (), header .value ());
275+ return mapped != null ? mapped : header .value ();
275276 }
276277
277278 @ 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