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 373ec963f..35cda1457 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 @@ -64,7 +64,6 @@ import org.springframework.messaging.Message; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebInputException; import org.springframework.web.server.UnsupportedMediaTypeStatusException; @@ -130,10 +129,10 @@ public class RequestProcessor { Class inputType = this.inspector.getInputType(function); Type itemType = getItemType(function); - Object input = body; + Object input = body == null && inputType.isAssignableFrom(String.class) ? "" : body; - if (StringUtils.hasText(body)) { - if (this.shouldUseJsonConversion(body, wrapper.headers.getContentType())) { + if (input != null) { + if (this.shouldUseJsonConversion((String) input, wrapper.headers.getContentType())) { Type jsonType = body.startsWith("[") && Collection.class.isAssignableFrom(inputType) || body.startsWith("{") ? inputType : Collection.class; @@ -141,10 +140,10 @@ public class RequestProcessor { jsonType = ResolvableType.forClassWithGenerics((Class) jsonType, (Class) itemType).getType(); } - input = this.mapper.toObject(body, jsonType); + input = this.mapper.toObject((String) input, jsonType); } else { - input = this.converter.convert(function, body); + input = this.converter.convert(function, (String) input); } } diff --git a/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/function/UserSubmittedTests.java b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/function/UserSubmittedTests.java new file mode 100644 index 000000000..8ea679b7b --- /dev/null +++ b/spring-cloud-function-web/src/test/java/org/springframework/cloud/function/web/function/UserSubmittedTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2019-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.function.web.function; + +import java.net.URI; +import java.util.function.Function; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.util.SocketUtils; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author Oleg Zhurakousky + * @since 2.1 + * + */ +public class UserSubmittedTests { + + @Before + public void init() throws Exception { + String port = "" + SocketUtils.findAvailableTcpPort(); + System.setProperty("server.port", port); + } + + @After + public void close() throws Exception { + System.clearProperty("server.port"); + } + + @Test + public void testIssue274() throws Exception { + SpringApplication.run(Issue274Configuration.class); + TestRestTemplate testRestTemplate = new TestRestTemplate(); + String port = System.getProperty("server.port"); + Thread.sleep(200); + ResponseEntity response = testRestTemplate + .postForEntity(new URI("http://localhost:" + port + "/echo"), "", String.class); + assertThat(response.getBody()).isNull(); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + } + + + @SpringBootApplication + protected static class Issue274Configuration { + + @Bean + public Function echo() { + return s -> s.toUpperCase(); + } + } + +}