From 417a6a75c12edafbbfebcef00a3dd3ef06298002 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Fri, 16 Nov 2018 14:12:50 +0100 Subject: [PATCH] Clean up tidy up RequestProcessor --- .../cloud/function/web/RequestProcessor.java | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RequestProcessor.java b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RequestProcessor.java index 444a6652e..66ceb2624 100644 --- a/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RequestProcessor.java +++ b/spring-cloud-function-web/src/main/java/org/springframework/cloud/function/web/RequestProcessor.java @@ -132,19 +132,12 @@ public class RequestProcessor { if (StringUtils.hasText(body)) { MediaType contentType = wrapper.headers.getContentType(); - if (contentType != null && "text".equalsIgnoreCase(contentType.getType())) { - input = converter.convert(function, body); - } - else if (body.startsWith("[")) { - Class collectionType = Collection.class.isAssignableFrom(inputType) ? inputType : Collection.class; - input = mapper.toObject(body, - ResolvableType.forClassWithGenerics(collectionType, (Class) itemType).getType()); - } - else if (body.startsWith("{")) { - input = mapper.toObject(body, inputType); - } - else if (body.startsWith("\"")) { - input = body.substring(1, body.length() - 2); + if (this.shouldUseJsonConversion(body, contentType)) { + Type jsonType = body.startsWith("[") && Collection.class.isAssignableFrom(inputType) || body.startsWith("{") ? inputType : Collection.class; + if (body.startsWith("[")) { + jsonType = ResolvableType.forClassWithGenerics((Class)jsonType, (Class) itemType).getType(); + } + input = mapper.toObject(body, jsonType); } else { input = converter.convert(function, body); @@ -154,6 +147,11 @@ public class RequestProcessor { return response(wrapper, input, stream); } + private boolean shouldUseJsonConversion(String body, MediaType contentType) { + return (body.startsWith("[") || body.startsWith("{")) + && (contentType == null || (contentType != null && !"text".equalsIgnoreCase(contentType.getType()))); + } + public Mono> stream(FunctionWrapper request) { Publisher result = request.function() != null ? value(request.function(), request.argument()) @@ -161,6 +159,10 @@ public class RequestProcessor { return stream(request, result); } + private List> getMessageReaders() { + return this.messageReaders; + } + private Mono> response(FunctionWrapper wrapper, Object body, boolean stream) { @@ -347,10 +349,6 @@ public class RequestProcessor { return ReflectionUtils.findMethod(handler.getClass(), "apply", (Class[]) null); } - public List> getMessageReaders() { - return this.messageReaders; - } - private Throwable handleReadError(MethodParameter parameter, Throwable ex) { return (ex instanceof DecodingException ? new ServerWebInputException("Failed to read HTTP message", parameter,