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 4646
4747import static org .assertj .core .api .Assertions .assertThat ;
4848import static org .assertj .core .api .Assertions .entry ;
49+ import static org .mockito .BDDMockito .given ;
50+ import static org .mockito .Mockito .mock ;
51+ import static org .mockito .Mockito .never ;
52+ import static org .mockito .Mockito .verify ;
4953
5054/**
5155 * @author Gary Russell
@@ -358,6 +362,20 @@ void deserializationExceptionHeadersAreMappedAsNonByteArray() {
358362 assertThat (headers .lastHeader (SerializationUtils .VALUE_DESERIALIZER_EXCEPTION_HEADER )).isNull ();
359363 }
360364
365+ @ Test
366+ void ensureNullHeaderValueHandledGraciously () {
367+ DefaultKafkaHeaderMapper mapper = new DefaultKafkaHeaderMapper ();
368+
369+ Header mockHeader = mock (Header .class );
370+ given (mockHeader .value ()).willReturn (null );
371+
372+ Object result = mapper .headerValueToAddIn (mockHeader );
373+
374+ assertThat (result ).isNull ();
375+ verify (mockHeader ).value ();
376+ verify (mockHeader , never ()).key ();
377+ }
378+
361379 public static final class Foo {
362380
363381 private String bar = "bar" ;
You can’t perform that action at this time.
0 commit comments