GH-274 Fixed empty body request handling

Resolves #274
This commit is contained in:
Oleg Zhurakousky
2019-03-20 08:45:26 +01:00
parent edb4b99be4
commit 7042394bd2
2 changed files with 82 additions and 6 deletions

View File

@@ -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);
}
}

View File

@@ -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<String> 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<String, String> echo() {
return s -> s.toUpperCase();
}
}
}