Skip to content

fix: handle null MessageHeaders in MessageBuilderMessageWriter (#689)#747

Open
prashantdotnaagar wants to merge 1 commit into
cloudevents:mainfrom
prashantdotnaagar:fix/689-converter-null-headers
Open

fix: handle null MessageHeaders in MessageBuilderMessageWriter (#689)#747
prashantdotnaagar wants to merge 1 commit into
cloudevents:mainfrom
prashantdotnaagar:fix/689-converter-null-headers

Conversation

@prashantdotnaagar

Copy link
Copy Markdown

Summary

Fixes a NullPointerException that occurs when CloudEventMessageConverter.toMessage() is called with null MessageHeaders — a scenario that happens when using RabbitMessagingTemplate#convertAndSend(String, Object) (and similar Spring messaging templates that don't supply headers).

Root Cause

CloudEventMessageConverter.toMessage() passes the headers argument directly to new MessageBuilderMessageWriter(headers). Inside the constructor, this.headers.putAll(headers) was called unconditionally, throwing an NPE when headers is null.

Fix

Added a null-guard in the MessageBuilderMessageWriter(Map<String, Object> headers) constructor so that a null input is treated as an empty map, while CloudEvent attributes are still written correctly.

Test

Added regression test fromCloudEventWithNullHeaders to CloudEventMessageConverterTests that calls converter.toMessage(event, null) and asserts the returned message contains the correct CloudEvent headers.

All 9 tests in CloudEventMessageConverterTests pass.

Closes #689

Calling CloudEventMessageConverter.toMessage() with null MessageHeaders (as done by RabbitMessagingTemplate#convertAndSend) caused a NullPointerException in MessageBuilderMessageWriter.putAll(null). Added a null-guard in the constructor so null headers are treated as empty, preventing the NPE. Also adds regression test fromCloudEventWithNullHeaders.

Closes cloudevents#689

Signed-off-by: Prashant nagar <prashantnaagar60@gmail.com>
@prashantdotnaagar
prashantdotnaagar force-pushed the fix/689-converter-null-headers branch from 7b5b757 to 8935f21 Compare July 18, 2026 11:00
Comment on lines +131 to +132
// gh-689: toMessage should not throw NPE when the caller provides no headers,
// e.g. RabbitMessagingTemplate#convertAndSend(String, Object) passes null.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: let's either:

  1. not ref the issue in the test case
  2. actually link to the issue rather than using a shorthand

+1 on having the test case though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants