Skip to content

Commit 09ec8ec

Browse files
garyrussellartembilan
authored andcommitted
AMQP-741: Fix Headers Containing Arrays
JIRA: https://jira.spring.io/browse/AMQP-741 The `DefaultMessagePropertiesConverter` incorrectly converts headers containing arrays (e.g. `String[]`) `toString()`. The java client handles arrays in headers. Change the converter to allow arrays to pass down to the client (after converting the element values if necessary). Fix Unit Test Since we don't actually go through rabbit in this test, the converted properties are still `Object[]`.
1 parent 0dc84e8 commit 09ec8ec

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,18 @@ private Object convertHeaderValueIfNecessary(Object value) {
185185
|| (value instanceof LongString) || (value instanceof Integer) || (value instanceof Long)
186186
|| (value instanceof Float) || (value instanceof Double) || (value instanceof BigDecimal)
187187
|| (value instanceof Short) || (value instanceof Byte) || (value instanceof Date)
188-
|| (value instanceof List) || (value instanceof Map);
188+
|| (value instanceof List) || (value instanceof Map) || (value instanceof Object[]);
189189
if (!valid && value != null) {
190190
value = value.toString();
191191
}
192+
else if (value instanceof Object[]) {
193+
Object[] array = (Object[]) value;
194+
Object[] writableArray = new Object[array.length];
195+
for (int i = 0; i < writableArray.length; i++) {
196+
writableArray[i] = convertHeaderValueIfNecessary(array[i]);
197+
}
198+
value = writableArray;
199+
}
192200
else if (value instanceof List<?>) {
193201
List<Object> writableList = new ArrayList<Object>(((List<?>) value).size());
194202
for (Object listValue : (List<?>) value) {

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/core/RabbitTemplateIntegrationTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.hamcrest.Matchers.instanceOf;
2222
import static org.hamcrest.Matchers.not;
2323
import static org.hamcrest.Matchers.startsWith;
24+
import static org.junit.Assert.assertArrayEquals;
2425
import static org.junit.Assert.assertEquals;
2526
import static org.junit.Assert.assertFalse;
2627
import static org.junit.Assert.assertNotNull;
@@ -409,9 +410,21 @@ public void testSendToNonExistentAndThenReceive() throws Exception {
409410

410411
@Test
411412
public void testSendAndReceiveWithPostProcessor() throws Exception {
413+
final String[] strings = new String[] { "1", "2" };
412414
template.convertAndSend(ROUTE, (Object) "message", message -> {
413415
message.getMessageProperties().setContentType("text/other");
414416
// message.getMessageProperties().setUserId("foo");
417+
MessageProperties props = message.getMessageProperties();
418+
props.getHeaders().put("strings", strings);
419+
props.getHeaders().put("objects", new Object[] { new Foo(), new Foo() });
420+
props.getHeaders().put("bytes", "abc".getBytes());
421+
return message;
422+
});
423+
template.setAfterReceivePostProcessors(message -> {
424+
assertEquals(Arrays.asList(strings), message.getMessageProperties().getHeaders().get("strings"));
425+
assertEquals(Arrays.asList(new String[] { "FooAsAString", "FooAsAString" }),
426+
message.getMessageProperties().getHeaders().get("objects"));
427+
assertArrayEquals("abc".getBytes(), (byte[]) message.getMessageProperties().getHeaders().get("bytes"));
415428
return message;
416429
});
417430
String result = (String) template.receiveAndConvert(ROUTE);
@@ -1559,4 +1572,17 @@ public ConnectionFactory defaultCF() {
15591572

15601573
}
15611574

1575+
private static class Foo {
1576+
1577+
Foo() {
1578+
super();
1579+
}
1580+
1581+
@Override
1582+
public String toString() {
1583+
return "FooAsAString";
1584+
}
1585+
1586+
}
1587+
15621588
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.amqp.rabbit.support;
1818

1919
import static org.hamcrest.Matchers.instanceOf;
20+
import static org.junit.Assert.assertArrayEquals;
2021
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertNull;
2223
import static org.junit.Assert.assertThat;
@@ -192,12 +193,30 @@ public void testFromUnsupportedValueInMap() {
192193
public void testInboundDeliveryMode() {
193194
DefaultMessagePropertiesConverter converter = new DefaultMessagePropertiesConverter();
194195
MessageProperties props = new MessageProperties();
196+
String[] strings = new String[] { "1", "2" };
197+
props.getHeaders().put("strings", strings);
198+
props.getHeaders().put("objects", new Object[] { new Foo() });
195199
props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
196200
BasicProperties bProps = converter.fromMessageProperties(props, "UTF-8");
197201
assertEquals(MessageDeliveryMode.toInt(MessageDeliveryMode.NON_PERSISTENT), bProps.getDeliveryMode().intValue());
198202
props = converter.toMessageProperties(bProps, null, "UTF-8");
199203
assertEquals(MessageDeliveryMode.NON_PERSISTENT, props.getReceivedDeliveryMode());
204+
assertArrayEquals(strings, (Object[]) props.getHeaders().get("strings"));
205+
assertEquals("[FooAsAString]", Arrays.asList((Object[]) props.getHeaders().get("objects")).toString());
200206
assertNull(props.getDeliveryMode());
201207
}
202208

209+
private static class Foo {
210+
211+
Foo() {
212+
super();
213+
}
214+
215+
@Override
216+
public String toString() {
217+
return "FooAsAString";
218+
}
219+
220+
}
221+
203222
}

0 commit comments

Comments
 (0)