Improved handling of output Message conversion

remove the check for 'accept' header
added test

Resolves #465
This commit is contained in:
Oleg Zhurakousky
2020-03-18 17:01:55 +01:00
parent b28c7c5cfc
commit 919b9902e9
2 changed files with 31 additions and 11 deletions

View File

@@ -735,16 +735,14 @@ public class BeanFactoryAwareFunctionRegistry
Message outputMessage = null;
if (value instanceof Message) {
MessageHeaders headers = ((Message) value).getHeaders();
if (!headers.containsKey("accept")) {
Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils
.getField(this.headersField, headers);
headersMap.put("accept", acceptedContentType);
// Set the contentType header to the value of accept for "legacy" reasons. But, do not set the
// contentType header to the value of accept if it is a wildcard type, as this doesn't make sense.
// This also applies to the else branch below.
if (acceptedContentType.isConcrete()) {
headersMap.put(MessageHeaders.CONTENT_TYPE, acceptedContentType);
}
Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils
.getField(this.headersField, headers);
headersMap.put("accept", acceptedContentType);
// Set the contentType header to the value of accept for "legacy" reasons. But, do not set the
// contentType header to the value of accept if it is a wildcard type, as this doesn't make sense.
// This also applies to the else branch below.
if (acceptedContentType.isConcrete()) {
headersMap.put(MessageHeaders.CONTENT_TYPE, acceptedContentType);
}
}
else {

View File

@@ -364,7 +364,7 @@ public class BeanFactoryAwareFunctionRegistryTests {
}
/**
* This test tests the fallback mechanism when an accept header has several values.
* The following two tests test the fallback mechanism when an accept header has several values.
* The function produces Integer, which cannot be serialized by the default converter supporting text/plain
* (StringMessageConverter) but can by the one supporting application/json, which comes second.
*/
@@ -377,6 +377,16 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(result.getPayload()).isEqualTo("5".getBytes("UTF-8"));
}
@Test
public void testMultipleOrderedAcceptValuesMessageOutput() throws Exception {
FunctionCatalog catalog = this.configureCatalog(MultipleOrderedAcceptValuesAsMessageOutputConfiguration.class);
Function<String, Message<byte[]>> function = catalog.lookup(
"beanFactoryAwareFunctionRegistryTests.MultipleOrderedAcceptValuesAsMessageOutputConfiguration",
"text/plain,application/json");
assertThat(function).isNotNull();
Message<byte[]> result = function.apply("hello");
assertThat(result.getPayload()).isEqualTo("5".getBytes("UTF-8"));
}
@SuppressWarnings("unchecked")
@Test
@@ -775,4 +785,16 @@ public class BeanFactoryAwareFunctionRegistryTests {
return t.length();
}
}
@EnableAutoConfiguration
@Configuration
@Component
public static class MultipleOrderedAcceptValuesAsMessageOutputConfiguration implements Function<String, Message<Integer>> {
@Override
public Message<Integer> apply(String t) {
return MessageBuilder.withPayload(t.length()).build();
}
}
}