cloudevents / cloudevents/sdk-java

Spring CloudEventMessageConverter produce a null pointer exception if initial message header map is null

Open Beginner friendly
#689 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
446
Forks
172
PR merge metrics
No merged PRs in 30d

Description

Step to reproduce

Declare a Rabbit templating using converter

@Bean
public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate, CloudEventMessageConverter cloudEventMessageConverter) {
	var rmt = new RabbitMessagingTemplate(rabbitTemplate);
	rmt.setMessageConverter(cloudEventMessageConverter);
	return rmt;
}

Use it to publish a message

rabbitMessagingTemplate.convertAndSend("my.routing.key", CloudEventBuilder.v1()
				.withId(UUID.randomUUID().toString())
				.withType("mytype")
				.withSource(new URI("my:urisource"))
				.withData(new BytesCloudEventData("{\"data\":\"test\"}".getBytes()))
				.build());
Result

NPE

java.lang.NullPointerException: Cannot invoke "java.util.Map.size()" because "m" is null

	at java.base/java.util.HashMap.putMapEntries(HashMap.java:495)
	at java.base/java.util.HashMap.putAll(HashMap.java:783)
	at io.cloudevents.spring.messaging.MessageBuilderMessageWriter.<init>(MessageBuilderMessageWriter.java:45)
	at io.cloudevents.spring.messaging.CloudEventMessageConverter.toMessage(CloudEventMessageConverter.java:54)
	at org.springframework.messaging.core.AbstractMessageSendingTemplate.doConvert(AbstractMessageSendingTemplate.java:177)
	at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:150)
	at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:129)
	at org.springframework.messaging.core.AbstractMessageSendingTemplate.convertAndSend(AbstractMessageSendingTemplate.java:122)
Solution

In call to io.cloudevents.spring.messaging.CloudEventMessageConverter#toMessage, the provided headers field can be null if caller didn't provide custom headers. A null check should be done and MessabeBilderMessageWriter should be called with default constructor in that case.

Workaround

use RabbitMessagingTemplate send methods that take a map as parameter

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at io.cloudevents.spring.messaging.CloudEventMessageConverter#toMessage and MessageBuilderMessageWriter, especially the constructor shown in the stack trace. Reproduce the RabbitMessagingTemplate.convertAndSend call without custom headers, then add coverage showing that a null header map no longer causes an exception and the CloudEvent is converted successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.