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-2924 Fix send null payload with KafkaTemplate#send(Message), and … #2925

Merged
merged 2 commits into from
Dec 8, 2023
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
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.kafka.support.converter;

import org.springframework.kafka.support.KafkaNull;
import org.springframework.messaging.Message;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -29,6 +30,7 @@
* {@code String<->byte[]} conversion is avoided.
*
* @author Gary Russell
* @author Vladimir Loginov
* @since 2.3
*
*/
Expand All @@ -44,7 +46,9 @@ public ByteArrayJsonMessageConverter(ObjectMapper objectMapper) {
@Override
protected Object convertPayload(Message<?> message) {
try {
return getObjectMapper().writeValueAsBytes(message.getPayload());
return message.getPayload() instanceof KafkaNull
? null
: getObjectMapper().writeValueAsBytes(message.getPayload());
}
catch (JsonProcessingException e) {
throw new ConversionException("Failed to convert to JSON", message, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import org.apache.kafka.common.utils.Bytes;

import org.springframework.kafka.support.KafkaNull;
import org.springframework.messaging.Message;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -31,6 +32,7 @@
* {@code String<->byte[]} conversion is avoided.
*
* @author Gary Russell
* @author Vladimir Loginov
* @since 2.1.7
*
*/
Expand All @@ -46,7 +48,9 @@ public BytesJsonMessageConverter(ObjectMapper objectMapper) {
@Override
protected Object convertPayload(Message<?> message) {
try {
return Bytes.wrap(getObjectMapper().writeValueAsBytes(message.getPayload()));
return message.getPayload() instanceof KafkaNull
? null
: Bytes.wrap(getObjectMapper().writeValueAsBytes(message.getPayload()));
}
catch (JsonProcessingException e) {
throw new ConversionException("Failed to convert to JSON", message, e);
Expand Down
pswrdf marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package org.springframework.kafka.support.converter;

import org.springframework.kafka.support.KafkaNull;
import org.springframework.messaging.Message;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -31,6 +32,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Dariusz Szablinski
* @author Vladimir Loginov
*/
public class StringJsonMessageConverter extends JsonMessageConverter {

Expand All @@ -44,12 +46,12 @@ public StringJsonMessageConverter(ObjectMapper objectMapper) {
@Override
protected Object convertPayload(Message<?> message) {
try {
return getObjectMapper()
.writeValueAsString(message.getPayload());
return message.getPayload() instanceof KafkaNull
? null
: getObjectMapper().writeValueAsString(message.getPayload());
}
catch (JsonProcessingException e) {
throw new ConversionException("Failed to convert to JSON", message, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.kafka.annotation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -56,6 +57,7 @@
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer;
import org.springframework.kafka.listener.DefaultErrorHandler;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.support.KafkaNull;
import org.springframework.kafka.support.converter.BatchMessagingMessageConverter;
import org.springframework.kafka.support.converter.BytesJsonMessageConverter;
import org.springframework.kafka.support.converter.ConversionException;
Expand All @@ -75,6 +77,7 @@
/**
* @author Gary Russell
* @author Artem Bilan
* @author Vladimir Loginov
*
* @since 1.3.2
*
Expand Down Expand Up @@ -131,6 +134,8 @@ public void testBatchOfPojoMessages(@Autowired KafkaAdmin admin) throws Exceptio
assertThat(listener.received.size()).isGreaterThan(0);
assertThat(listener.received.get(0).getPayload()).isInstanceOf(Foo.class);
assertThat(listener.received.get(0).getPayload().getBar()).isEqualTo("bar");
assertThatNoException().isThrownBy(() -> this.template.send(
new GenericMessage<>(KafkaNull.INSTANCE, Collections.singletonMap(KafkaHeaders.TOPIC, topic))));
verify(admin, never()).clusterId();
}

Expand Down