Rename body(Object) to bodyValue

The recently added body(Object) variant can be confused easily with
body(Publisher, Class) forgetting to provide the element type and
only running into the IllegalArgumentException at runtime.

See gh-23212
This commit is contained in:
Rossen Stoyanchev 2019-08-20 03:03:27 +03:00
parent 5cfe491602
commit 008687d5ae
24 changed files with 54 additions and 54 deletions

View File

@ -261,8 +261,8 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public RequestHeadersSpec<?> body(Object body) {
this.bodySpec.body(body);
public RequestHeadersSpec<?> bodyValue(Object body) {
this.bodySpec.bodyValue(body);
return this;
}
@ -299,7 +299,7 @@ class DefaultWebTestClient implements WebTestClient {
@Override
@Deprecated
public RequestHeadersSpec<?> syncBody(Object body) {
return body(body);
return bodyValue(body);
}
@Override

View File

@ -642,7 +642,7 @@ public interface WebTestClient {
* @return spec for decoding the response
* @since 5.2
*/
RequestHeadersSpec<?> body(Object body);
RequestHeadersSpec<?> bodyValue(Object body);
/**
* Set the body of the request to the given producer.
@ -713,7 +713,7 @@ public interface WebTestClient {
* @throws IllegalArgumentException if {@code body} is a {@link Publisher} or an
* instance of a type supported by {@link ReactiveAdapterRegistry#getSharedInstance()},
* for which {@link #body(Publisher, Class)} or {@link #body(Object, Class)} should be used.
* @deprecated as of Spring Framework 5.2 in favor of {@link #body(Object)}
* @deprecated as of Spring Framework 5.2 in favor of {@link #bodyValue(Object)}
*/
@Deprecated
RequestHeadersSpec<?> syncBody(Object body);

View File

@ -61,7 +61,7 @@ public class ApplicationContextSpecTests {
.GET("/sessionClassName", request ->
request.session().flatMap(session -> {
String className = session.getClass().getSimpleName();
return ServerResponse.ok().body(className);
return ServerResponse.ok().bodyValue(className);
}))
.build();
}

View File

@ -63,7 +63,7 @@ public class ErrorTests {
EntityExchangeResult<Void> result = this.client.post()
.uri("/post")
.contentType(MediaType.APPLICATION_JSON)
.body(new Person("Dan"))
.bodyValue(new Person("Dan"))
.exchange()
.expectStatus().isBadRequest()
.expectBody().isEmpty();

View File

@ -82,7 +82,7 @@ public class JsonContentTests {
public void postJsonContent() {
this.client.post().uri("/persons")
.contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"John\"}")
.bodyValue("{\"name\":\"John\"}")
.exchange()
.expectStatus().isCreated()
.expectBody().isEmpty();

View File

@ -145,7 +145,7 @@ public class ResponseEntityTests {
@Test
public void postEntity() {
this.client.post()
.body(new Person("John"))
.bodyValue(new Person("John"))
.exchange()
.expectStatus().isCreated()
.expectHeader().valueEquals("location", "/persons/John")

View File

@ -116,7 +116,7 @@ public class XmlContentTests {
this.client.post().uri("/persons")
.contentType(MediaType.APPLICATION_XML)
.body(content)
.bodyValue(content)
.exchange()
.expectStatus().isCreated()
.expectHeader().valueEquals(HttpHeaders.LOCATION, "/persons/John")

View File

@ -45,7 +45,7 @@ public class HttpServerTests {
@BeforeEach
public void start() throws Exception {
HttpHandler httpHandler = RouterFunctions.toHttpHandler(
route(GET("/test"), request -> ServerResponse.ok().body("It works!")));
route(GET("/test"), request -> ServerResponse.ok().bodyValue("It works!")));
this.server = new ReactorHttpServer();
this.server.setHandler(httpHandler);

View File

@ -41,7 +41,7 @@ public class RouterFunctionTests {
public void setUp() throws Exception {
RouterFunction<?> route = route(GET("/test"), request ->
ServerResponse.ok().body("It works!"));
ServerResponse.ok().bodyValue("It works!"));
this.testClient = WebTestClient.bindToRouterFunction(route).build();
}

View File

@ -70,7 +70,7 @@ class WebTestClientExtensionsTests {
@Test
fun `KotlinBodySpec#isEqualTo`() {
WebTestClient
.bindToRouterFunction( router { GET("/") { ok().body("foo") } } )
.bindToRouterFunction( router { GET("/") { ok().bodyValue("foo") } } )
.build()
.get().uri("/").exchange().expectBody<String>().isEqualTo("foo")
}
@ -78,7 +78,7 @@ class WebTestClientExtensionsTests {
@Test
fun `KotlinBodySpec#consumeWith`() {
WebTestClient
.bindToRouterFunction( router { GET("/") { ok().body("foo") } } )
.bindToRouterFunction( router { GET("/") { ok().bodyValue("foo") } } )
.build()
.get().uri("/").exchange().expectBody<String>().consumeWith { assertEquals("foo", it.responseBody) }
}
@ -86,7 +86,7 @@ class WebTestClientExtensionsTests {
@Test
fun `KotlinBodySpec#returnResult`() {
WebTestClient
.bindToRouterFunction( router { GET("/") { ok().body("foo") } } )
.bindToRouterFunction( router { GET("/") { ok().bodyValue("foo") } } )
.build()
.get().uri("/").exchange().expectBody<String>().returnResult().apply { assertEquals("foo", responseBody) }
}

View File

@ -78,7 +78,7 @@ public abstract class BodyInserters {
/**
* Inserter to write the given object.
* <p>Alternatively, consider using the {@code body(Object)} shortcuts on
* <p>Alternatively, consider using the {@code bodyValue(Object)} shortcuts on
* {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
* {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
* @param body the body to write to the response
@ -227,7 +227,7 @@ public abstract class BodyInserters {
* Return a {@link FormInserter} to write the given {@code MultiValueMap}
* as URL-encoded form data. The returned inserter allows for additional
* entries to be added via {@link FormInserter#with(String, Object)}.
* <p>Note that you can also use the {@code body(Object)} method in the
* <p>Note that you can also use the {@code bodyValue(Object)} method in the
* request builders of both the {@code WebClient} and {@code WebTestClient}.
* In that case the setting of the request content type is also not required,
* just be sure the map contains String values only or otherwise it would be
@ -259,7 +259,7 @@ public abstract class BodyInserters {
* Object or an {@link HttpEntity}.
* <p>Note that you can also build the multipart data externally with
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
* {@code body(Object)} shortcut method in {@code WebClient}.
* {@code bodyValue(Object)} shortcut method in {@code WebClient}.
* @param multipartData the form data to write to the output message
* @return the inserter that allows adding more parts
* @see MultipartBodyBuilder
@ -275,7 +275,7 @@ public abstract class BodyInserters {
* {@link HttpEntity}.
* <p>Note that you can also build the multipart data externally with
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
* {@code body(Object)} shortcut method in {@code WebClient}.
* {@code bodyValue(Object)} shortcut method in {@code WebClient}.
* @param name the part name
* @param value the part value, an Object or {@code HttpEntity}
* @return the inserter that allows adding more parts
@ -291,7 +291,7 @@ public abstract class BodyInserters {
* as multipart data.
* <p>Note that you can also build the multipart data externally with
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
* {@code body(Object)} shortcut method in {@code WebClient}.
* {@code bodyValue(Object)} shortcut method in {@code WebClient}.
* @param name the part name
* @param publisher the publisher that forms the part value
* @param elementClass the class contained in the {@code publisher}
@ -309,7 +309,7 @@ public abstract class BodyInserters {
* allows specifying generic type information.
* <p>Note that you can also build the multipart data externally with
* {@link MultipartBodyBuilder}, and pass the resulting map directly to the
* {@code body(Object)} shortcut method in {@code WebClient}.
* {@code bodyValue(Object)} shortcut method in {@code WebClient}.
* @param name the part name
* @param publisher the publisher that forms the part value
* @param typeReference the type contained in the {@code publisher}

View File

@ -288,7 +288,7 @@ class DefaultWebClient implements WebClient {
}
@Override
public RequestHeadersSpec<?> body(Object body) {
public RequestHeadersSpec<?> bodyValue(Object body) {
this.inserter = BodyInserters.fromObject(body);
return this;
}
@ -327,7 +327,7 @@ class DefaultWebClient implements WebClient {
@Override
@Deprecated
public RequestHeadersSpec<?> syncBody(Object body) {
return body(body);
return bodyValue(body);
}
@Override

View File

@ -59,7 +59,7 @@ import org.springframework.web.util.UriBuilderFactory;
* </ul>
* <p>For examples with a request body see:
* <ul>
* <li>{@link RequestBodySpec#body(Object) body(Object)}
* <li>{@link RequestBodySpec#bodyValue(Object) bodyValue(Object)}
* <li>{@link RequestBodySpec#body(Publisher, Class) body(Publisher,Class)}
* <li>{@link RequestBodySpec#body(Object, Class) body(Object,Class)}
* <li>{@link RequestBodySpec#body(BodyInserter) body(BodyInserter)}
@ -528,7 +528,7 @@ public interface WebClient {
* Mono&lt;Void&gt; result = client.post()
* .uri("/persons/{id}", id)
* .contentType(MediaType.APPLICATION_JSON)
* .body(person)
* .bodyValue(person)
* .retrieve()
* .bodyToMono(Void.class);
* </pre>
@ -547,7 +547,7 @@ public interface WebClient {
* for which {@link #body(Publisher, Class)} or {@link #body(Object, Class)} should be used.
* @since 5.2
*/
RequestHeadersSpec<?> body(Object body);
RequestHeadersSpec<?> bodyValue(Object body);
/**
* A shortcut for {@link #body(BodyInserter)} with a
@ -657,7 +657,7 @@ public interface WebClient {
* @throws IllegalArgumentException if {@code body} is a {@link Publisher} or an
* instance of a type supported by {@link ReactiveAdapterRegistry#getSharedInstance()},
* for which {@link #body(Publisher, Class)} or {@link #body(Object, Class)} should be used.
* @deprecated as of Spring Framework 5.2 in favor of {@link #body(Object)}
* @deprecated as of Spring Framework 5.2 in favor of {@link #bodyValue(Object)}
*/
@Deprecated
RequestHeadersSpec<?> syncBody(Object body);

View File

@ -223,7 +223,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
}
@Override
public Mono<ServerResponse> body(Object body) {
public Mono<ServerResponse> bodyValue(Object body) {
return new DefaultEntityResponseBuilder<>(body,
BodyInserters.fromObject(body))
.status(this.statusCode)
@ -286,7 +286,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
@Override
@Deprecated
public Mono<ServerResponse> syncBody(Object body) {
return body(body);
return bodyValue(body);
}
@Override

View File

@ -408,7 +408,7 @@ public interface ServerResponse {
* for which {@link #body(Publisher, Class)} or {@link #body(Object, Class)} should be used.
* @since 5.2
*/
Mono<ServerResponse> body(Object body);
Mono<ServerResponse> bodyValue(Object body);
/**
* Set the body of the response to the given asynchronous {@code Publisher} and return it.
@ -479,7 +479,7 @@ public interface ServerResponse {
* @throws IllegalArgumentException if {@code body} is a {@link Publisher} or an
* instance of a type supported by {@link ReactiveAdapterRegistry#getSharedInstance()},
* for which {@link #body(Publisher, Class)} or {@link #body(Object, Class)} should be used.
* @deprecated as of Spring Framework 5.2 in favor of {@link #body(Object)}
* @deprecated as of Spring Framework 5.2 in favor of {@link #bodyValue(Object)}
*/
@Deprecated
Mono<ServerResponse> syncBody(Object body);

View File

@ -75,7 +75,7 @@ inline fun <reified T : Any> ServerResponse.BodyBuilder.bodyWithType(publisher:
* instance of a type supported by [org.springframework.core.ReactiveAdapterRegistry.getSharedInstance],
*/
suspend fun ServerResponse.BodyBuilder.bodyAndAwait(body: Any): ServerResponse =
body(body).awaitSingle()
bodyValue(body).awaitSingle()
/**
* Coroutines variant of [ServerResponse.BodyBuilder.body] with [Any] and

View File

@ -63,7 +63,7 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
Mono<ClientResponse> result = webClient
.post()
.uri("http://localhost:" + this.port + "/multipartData")
.body(generateBody())
.bodyValue(generateBody())
.exchange();
StepVerifier
@ -79,7 +79,7 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
Mono<ClientResponse> result = webClient
.post()
.uri("http://localhost:" + this.port + "/parts")
.body(generateBody())
.bodyValue(generateBody())
.exchange();
StepVerifier
@ -95,7 +95,7 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
Mono<String> result = webClient
.post()
.uri("http://localhost:" + this.port + "/transferTo")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);
@ -176,7 +176,7 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
Path tempFile = Files.createTempFile("MultipartIntegrationTests", null);
return part.transferTo(tempFile)
.then(ServerResponse.ok()
.body(tempFile.toString()));
.bodyValue(tempFile.toString()));
}
catch (Exception e) {
return Mono.error(e);

View File

@ -186,7 +186,7 @@ public class DefaultWebClientTests {
WebClient client = this.builder.build();
assertThatIllegalArgumentException().isThrownBy(() ->
client.post().uri("https://example.com").body(mono));
client.post().uri("https://example.com").bodyValue(mono));
}
@Test

View File

@ -514,7 +514,7 @@ class WebClientIntegrationTests {
.uri("/pojo/capitalize")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.body(new Pojo("foofoo", "barbar"))
.bodyValue(new Pojo("foofoo", "barbar"))
.retrieve()
.bodyToMono(Pojo.class);

View File

@ -309,7 +309,7 @@ public class DefaultServerResponseBuilderTests {
public void copyCookies() {
Mono<ServerResponse> serverResponse = ServerResponse.ok()
.cookie(ResponseCookie.from("foo", "bar").build())
.body("body");
.bodyValue("body");
assertThat(serverResponse.block().cookies().isEmpty()).isFalse();
@ -361,7 +361,7 @@ public class DefaultServerResponseBuilderTests {
Mono<Void> mono = Mono.empty();
assertThatIllegalArgumentException().isThrownBy(() ->
ServerResponse.ok().body(mono));
ServerResponse.ok().bodyValue(mono));
}
@Test
@ -369,7 +369,7 @@ public class DefaultServerResponseBuilderTests {
String etag = "\"foo\"";
ServerResponse responseMono = ServerResponse.ok()
.eTag(etag)
.body("bar")
.bodyValue("bar")
.block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com")
@ -393,7 +393,7 @@ public class DefaultServerResponseBuilderTests {
ServerResponse responseMono = ServerResponse.ok()
.lastModified(oneMinuteBeforeNow)
.body("bar")
.bodyValue("bar")
.block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com")

View File

@ -32,8 +32,8 @@ class InvalidHttpMethodIntegrationTests extends AbstractRouterFunctionIntegratio
@Override
protected RouterFunction<?> routerFunction() {
return RouterFunctions.route(RequestPredicates.GET("/"),
request -> ServerResponse.ok().body("FOO"))
.andRoute(RequestPredicates.all(), request -> ServerResponse.ok().body("BAR"));
request -> ServerResponse.ok().bodyValue("FOO"))
.andRoute(RequestPredicates.all(), request -> ServerResponse.ok().bodyValue("BAR"));
}
@ParameterizedHttpServerTest

View File

@ -137,7 +137,7 @@ class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrationTests
public Mono<ServerResponse> pattern(ServerRequest request) {
String pattern = matchingPattern(request).getPatternString();
return ServerResponse.ok().body(pattern);
return ServerResponse.ok().bodyValue(pattern);
}
@SuppressWarnings("unchecked")

View File

@ -85,7 +85,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<ClientResponse> result = webClient
.post()
.uri("/requestPart")
.body(generateBody())
.bodyValue(generateBody())
.exchange();
StepVerifier
@ -101,7 +101,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<String> result = webClient
.post()
.uri("/requestBodyMap")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);
@ -117,7 +117,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<String> result = webClient
.post()
.uri("/requestBodyFlux")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);
@ -133,7 +133,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<String> result = webClient
.post()
.uri("/filePartFlux")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);
@ -149,7 +149,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<String> result = webClient
.post()
.uri("/filePartMono")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);
@ -165,7 +165,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Flux<String> result = webClient
.post()
.uri("/transferTo")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToFlux(String.class);
@ -183,7 +183,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests {
Mono<String> result = webClient
.post()
.uri("/modelAttribute")
.body(generateBody())
.bodyValue(generateBody())
.retrieve()
.bodyToMono(String.class);

View File

@ -67,12 +67,12 @@ class ServerResponseExtensionsTests {
fun `BodyBuilder#bodyAndAwait with object parameter`() {
val response = mockk<ServerResponse>()
val body = "foo"
every { bodyBuilder.body(ofType<String>()) } returns Mono.just(response)
every { bodyBuilder.bodyValue(ofType<String>()) } returns Mono.just(response)
runBlocking {
bodyBuilder.bodyAndAwait(body)
}
verify {
bodyBuilder.body(ofType<String>())
bodyBuilder.bodyValue(ofType<String>())
}
}