Do not convert text/plain content to JSON

Fixes bug and removes test comments from previous commit.

Resolves #1056
This commit is contained in:
Chris Bono
2023-07-15 12:12:01 -05:00
committed by Oleg Zhurakousky
parent c97c0b2a96
commit 7bc499ddea
3 changed files with 25 additions and 15 deletions

View File

@@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
@@ -61,6 +62,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
@@ -69,6 +71,7 @@ import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.MessageBuilder; import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@@ -82,7 +85,7 @@ import org.springframework.util.StringUtils;
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Roman Samarev * @author Roman Samarev
* @author Soby Chacko * @author Soby Chacko
* * @author Chris Bono
*/ */
public class SimpleFunctionRegistry implements FunctionRegistry { public class SimpleFunctionRegistry implements FunctionRegistry {
protected Log logger = LogFactory.getLog(this.getClass()); protected Log logger = LogFactory.getLog(this.getClass());
@@ -805,9 +808,6 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
return sanitizedHeaders; return sanitizedHeaders;
} }
/*
*
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Object fluxifyInputIfNecessary(Object input) { private Object fluxifyInputIfNecessary(Object input) {
if (input instanceof Message && !((Message) input).getHeaders().containsKey("user-agent") && this.isConsumer() && !this.isInputTypePublisher()) { if (input instanceof Message && !((Message) input).getHeaders().containsKey("user-agent") && this.isConsumer() && !this.isInputTypePublisher()) {
@@ -819,16 +819,20 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
if (!this.isRoutingFunction() && !(input instanceof Publisher)) { if (!this.isRoutingFunction() && !(input instanceof Publisher)) {
Object payload = input; Object payload = input;
if (input instanceof Message) { var treatPayloadAsPlainText = false;
if (((Message) input).getHeaders().containsKey("payload")) { if (input instanceof Message msg) {
payload = ((Message) input).getHeaders().get("payload"); if (msg.getHeaders().containsKey("payload")) {
payload = msg.getHeaders().get("payload");
} }
else { else {
payload = ((Message) input).getPayload(); payload = msg.getPayload();
} }
treatPayloadAsPlainText = contentTypeHeaderValue(msg).equals(MimeTypeUtils.TEXT_PLAIN_VALUE);
} }
if (JsonMapper.isJsonStringRepresentsCollection(payload)
&& !FunctionTypeUtils.isTypeCollection(this.inputType) && !FunctionTypeUtils.isTypeArray(this.inputType)) { if ((!treatPayloadAsPlainText && JsonMapper.isJsonStringRepresentsCollection(payload))
&& !FunctionTypeUtils.isTypeCollection(this.inputType)
&& !FunctionTypeUtils.isTypeArray(this.inputType)) {
MessageHeaders headers = ((Message) input).getHeaders(); MessageHeaders headers = ((Message) input).getHeaders();
Collection collectionPayload = jsonMapper.fromJson(payload, Collection.class); Collection collectionPayload = jsonMapper.fromJson(payload, Collection.class);
Class inputClass = FunctionTypeUtils.getRawType(this.inputType); Class inputClass = FunctionTypeUtils.getRawType(this.inputType);
@@ -872,9 +876,17 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
return input; return input;
} }
/* private String contentTypeHeaderValue(Message<?> msg) {
* var contentType = msg.getHeaders().get(MessageHeaders.CONTENT_TYPE);
*/ if (contentType == null) {
contentType = msg.getHeaders().get(HttpHeaders.CONTENT_TYPE);
if (contentType == null) {
contentType = msg.getHeaders().get(HttpHeaders.CONTENT_TYPE.toLowerCase());
}
}
return Objects.toString(contentType);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Object invokeFunction(Object convertedInput) { private Object invokeFunction(Object convertedInput) {
Object result; Object result;

View File

@@ -193,7 +193,6 @@ public class SimpleFunctionRegistryTests {
assertThat(result).isEqualTo("{\"HELLO\":\"WORLD\"}"); assertThat(result).isEqualTo("{\"HELLO\":\"WORLD\"}");
} }
// TODO: Once bug is fixed this test (last entry) will fully pass and this COMMENT should be removed
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = {"[hello", "hello]", "[hello]"}) @ValueSource(strings = {"[hello", "hello]", "[hello]"})
void textContentTypeWithValueWrappedBracketsIsOk(String inputMessagePayloadValue) { void textContentTypeWithValueWrappedBracketsIsOk(String inputMessagePayloadValue) {

View File

@@ -128,7 +128,6 @@ public class HttpGetIntegrationTests {
assertThat(result.getBody()).isEqualTo("foo"); assertThat(result.getBody()).isEqualTo("foo");
} }
// TODO: Once bug is fixed this test (last entry) will fully pass and this COMMENT should be removed
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = {"[hello", "hello]", "[hello]"}) @ValueSource(strings = {"[hello", "hello]", "[hello]"})
void textContentTypeWithValueWrappedBracketsIsOk(String inputMessagePayloadValue) throws URISyntaxException { void textContentTypeWithValueWrappedBracketsIsOk(String inputMessagePayloadValue) throws URISyntaxException {