From 3196a2ee8b560a23e84b51098041d1b48bff46a9 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 28 Jan 2019 16:24:45 +0100 Subject: [PATCH] Fixed exception handling in RequestProcessor Fixed exception handling in the RequestProcessor for cases where input can not be determined before function invocation --- .../cloud/function/web/RequestProcessor.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 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 66ceb2624..22db3308e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,8 +131,7 @@ public class RequestProcessor { Object input = body; if (StringUtils.hasText(body)) { - MediaType contentType = wrapper.headers.getContentType(); - if (this.shouldUseJsonConversion(body, contentType)) { + if (this.shouldUseJsonConversion(body, wrapper.headers.getContentType())) { Type jsonType = body.startsWith("[") && Collection.class.isAssignableFrom(inputType) || body.startsWith("{") ? inputType : Collection.class; if (body.startsWith("[")) { jsonType = ResolvableType.forClassWithGenerics((Class)jsonType, (Class) itemType).getType(); @@ -166,19 +165,30 @@ public class RequestProcessor { private Mono> response(FunctionWrapper wrapper, Object body, boolean stream) { - Iterable iterable = body instanceof Collection ? (Collection) body - : (body instanceof Set ? Collections.singleton(body) - : Collections.singletonList(body)); - Function, Publisher> function = wrapper.function(); Consumer> consumer = wrapper.consumer(); - MultiValueMap form = wrapper.params(); + Flux flux; + if (body != null) { + if (Collection.class + .isAssignableFrom(inspector.getInputType(wrapper.handler()))) { + flux = Flux.just(body); + } + else { + Iterable iterable = body instanceof Collection ? (Collection) body + : (body instanceof Set ? Collections.singleton(body) + : Collections.singletonList(body)); + flux = Flux.fromIterable(iterable); + } + } + else if (MultiValueMap.class.isAssignableFrom(inspector.getInputType(wrapper.handler()))) { + flux = Flux.just(wrapper.params()); + } + else { + throw new IllegalStateException("Failed to determine input for function call with parameters: '" + wrapper.params + + "' and headers: `" + wrapper.headers + "`"); + } - boolean inputIsCollection = Collection.class - .isAssignableFrom(inspector.getInputType(wrapper.handler())); - Flux flux = body == null ? Flux.just(form) - : inputIsCollection ? Flux.just(body) : Flux.fromIterable(iterable); if (inspector.isMessage(function)) { flux = messages(wrapper, function == null ? consumer : function, flux); }