Skip to content

Commit 05ef5a9

Browse files
garyrussellartembilan
authored andcommitted
Convert Class header values using getName()
1 parent 5da725b commit 05ef5a9

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/support/DefaultMessagePropertiesConverter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private Map<String, Object> convertHeadersIfNecessary(Map<String, Object> header
184184
private Object convertHeaderValueIfNecessary(@Nullable Object valueArg) {
185185
Object value = valueArg;
186186
boolean valid = (value instanceof String) || (value instanceof byte[]) // NOSONAR boolean complexity
187-
|| (value instanceof Boolean)
187+
|| (value instanceof Boolean) || (value instanceof Class)
188188
|| (value instanceof LongString) || (value instanceof Integer) || (value instanceof Long)
189189
|| (value instanceof Float) || (value instanceof Double) || (value instanceof BigDecimal)
190190
|| (value instanceof Short) || (value instanceof Byte) || (value instanceof Date)
@@ -216,6 +216,9 @@ else if (value instanceof Map<?, ?>) {
216216
}
217217
value = writableMap;
218218
}
219+
else if (value instanceof Class) {
220+
value = ((Class<?>) value).getName();
221+
}
219222
return value;
220223
}
221224

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/support/DefaultMessagePropertiesConverterTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ public void testInboundDeliveryMode() {
193193
assertThat(props.getDeliveryMode()).isNull();
194194
}
195195

196+
@Test
197+
public void testClassHeader() {
198+
MessageProperties props = new MessageProperties();
199+
props.setHeader("aClass", getClass());
200+
BasicProperties basic = new DefaultMessagePropertiesConverter().fromMessageProperties(props, "UTF8");
201+
assertThat(basic.getHeaders().get("aClass")).isEqualTo(getClass().getName());
202+
}
203+
196204
private static class Foo {
197205

198206
Foo() {

src/reference/asciidoc/amqp.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,6 +3655,9 @@ Also, the inbound `userId` property is no longer mapped to `MessageProperties.us
36553655
It is mapped to `MessageProperties.receivedUserId` instead.
36563656
These changes are to avoid unexpected propagation of these properties if the same `MessageProperties` object is used for an outbound message.
36573657

3658+
Starting with version 2.2, the `DefaultMessagePropertiesConverter` converts any custom headers with values of type `Class<?>` using `getName()` instead of `toString()`; this avoids consuming application having to parse the class name out of the `toString()` representation.
3659+
For rolling upgrades, you may need to change your consumers to understand both formats until all producers are upgraded.
3660+
36583661
[[post-processing]]
36593662
==== Modifying Messages - Compression and More
36603663

src/reference/asciidoc/whats-new.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ See <<cluster>> for more information.
6060

6161
The `Declarables` object (for declaring multiple queues, exchanges, bindings) now has a filtered getter for each type.
6262
See <<collection-declaration>> for more information.
63+
64+
Outbound headers with values of type `Class<?>` are now mapped using `getName()` instead of `toString()`.
65+
See <<message-properties-converters>> for more information.

0 commit comments

Comments
 (0)