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

Unable to send null payload with KafkaTemplate#send(Message) #2924

Closed
pswrdf opened this issue Dec 7, 2023 · 3 comments · Fixed by #2925
Closed

Unable to send null payload with KafkaTemplate#send(Message) #2924

pswrdf opened this issue Dec 7, 2023 · 3 comments · Fixed by #2925

Comments

@pswrdf
Copy link
Contributor

pswrdf commented Dec 7, 2023

In what version(s) of Spring for Apache Kafka are you seeing this issue?
3.0.11

Describe the bug

Unable to send null message through KafkaTemplate#send(Message).
Unable to create GenericMessage with null payload.

To Reproduce

spring:
  kafka:
    producer:
      value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
    @Bean
    public RecordMessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
        return new ByteArrayJsonMessageConverter(objectMapper);
    }
       final var Object myValue = null;
       final var message = MessageBuilder.withPayload(myValue).setHeader(TOPIC, topic).build();
        kafkaTemplate.send(message);

Expected behavior

Kafka sends null message.
But we get: IllegalArgumentException: Payload must not be null

We could set myValue=KafkaNull.INSTANCE;
But JsonMessageConverter inherited classes cannot proccess KafkaNull.

@artembilan
Copy link
Member

I confirm this as a bug.
All those MessagingMessageConverter extensions must support KafkaNull and just short-circuit same way as their super class does.
I would say that JsonMessageConverter.convertPayload() must call super and throw that UnsupportedOperationException only if result not null.
The rest of impls should also call super and perform JSON conversion only of not null.

As a workaround I suggestion to extend that ByteArrayJsonMessageConverter and override its convertPayload() the way we just discussed:

	protected Object convertPayload(Message<?> message) {
		Object payload = message.getPayload();
		if (payload instanceof KafkaNull) {
			return null;
		}
		else {
			try {
				return getObjectMapper().writeValueAsBytes(message.getPayload());
			}
			catch (JsonProcessingException e) {
				throw new ConversionException("Failed to convert to JSON", message, e);
			}
		}
	}

Would be great if you can contribute the fix.

Thank you!

@artembilan artembilan added this to the 3.1.1 milestone Dec 7, 2023
pswrdf pushed a commit to pswrdf/spring-kafka that referenced this issue Dec 7, 2023
pswrdf pushed a commit to pswrdf/spring-kafka that referenced this issue Dec 7, 2023
…d(Message) and JsonMessageConverter

 Fixes: spring-projects#2924

 Add posibility to send KafkaNull message payload with JsonMessageConverter.
pswrdf pushed a commit to pswrdf/spring-kafka that referenced this issue Dec 7, 2023
…d(Message) and JsonMessageConverter

Fixes: spring-projects#2924

 Add posibility to send KafkaNull message payload with JsonMessageConverter.
 **Cherry-pick to 3.0.x**
sobychacko pushed a commit that referenced this issue Dec 8, 2023
 Fixes: gh-2924

 Add the possibility of sending KafkaNull message payload with JsonMessageConverter.

 **Cherry-pick to 3.0.x**
sobychacko pushed a commit that referenced this issue Dec 8, 2023
 Fixes: gh-2924

 Add the possibility of sending KafkaNull message payload with JsonMessageConverter.

 **Cherry-pick to 3.0.x**
@jakabgyonci97
Copy link

Hello, I have a somewhat similar issue in version 3.1.1.
I'm writing a kafka message using KafkaTemplate#send(Message). In my case there are different headers that I attach to the message, some value value null. When the send operation is performed I get the following error:

java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "rawValue" is null

@artembilan
Copy link
Member

In my case there are different headers that I attach to the message, some value value null

So, don't add those null headers.
In fact use a MessageBuilder API to create that Message instead.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants