Tighten up multi-valued inputs to "lite" HTTP endpoint

If the user function is single valued but the input is an array,
we should output an array (just like the full webflux version).
This commit is contained in:
Dave Syer
2018-10-25 14:06:34 +01:00
parent 2e5c6d22b5
commit 2691b20a4b
3 changed files with 21 additions and 12 deletions

View File

@@ -107,9 +107,9 @@ public class RequestProcessor {
if (body.startsWith("[")) {
input = Collection.class.isAssignableFrom(inputType)
? mapper.toObject(body, inputType)
: mapper.toObject(body, ResolvableType
.forClassWithGenerics(ArrayList.class, (Class<?>) inputType)
.getType());
: mapper.toObject(body,
ResolvableType.forClassWithGenerics(ArrayList.class,
(Class<?>) inputType).getType());
}
else {
if (inputType == String.class) {
@@ -126,8 +126,7 @@ public class RequestProcessor {
}
}
}
return post(wrapper, input, null, stream,
!Collection.class.isAssignableFrom(inputType));
return post(wrapper, input, null, stream);
}
public Mono<ResponseEntity<?>> stream(FunctionWrapper request) {
@@ -138,8 +137,7 @@ public class RequestProcessor {
}
private Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, Object body,
MultiValueMap<String, String> params, boolean stream,
boolean shouldFluxAsIterable) {
MultiValueMap<String, String> params, boolean stream) {
Iterable<?> iterable = body instanceof Collection ? (List<?>) body
: Collections.singletonList(body);
@@ -152,8 +150,10 @@ public class RequestProcessor {
form.putAll(params);
}
boolean inputIsCollection =
Collection.class.isAssignableFrom(inspector.getInputType(wrapper.handler()));
Flux<?> flux = body == null ? Flux.just(form)
: shouldFluxAsIterable ? Flux.fromIterable(iterable) : Flux.just(body);
: inputIsCollection ? Flux.just(body) : Flux.fromIterable(iterable);
if (inspector.isMessage(function)) {
flux = messages(wrapper, function == null ? consumer : function, flux);
}
@@ -229,6 +229,9 @@ public class RequestProcessor {
}
private boolean isInputMultiple(Object handler) {
if (handler instanceof FluxWrapper) {
handler = ((FluxWrapper<?>) handler).getTarget();
}
Class<?> type = inspector.getInputType(handler);
Class<?> wrapper = inspector.getInputWrapper(handler);
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper);

View File

@@ -44,11 +44,17 @@ public class PojoTests {
private WebTestClient client;
@Test
public void words() throws Exception {
public void single() throws Exception {
client.post().uri("/").body(Mono.just("{\"value\":\"foo\"}"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void multiple() throws Exception {
client.post().uri("/").body(Mono.just("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("[{\"value\":\"FOO\"},{\"value\":\"BAR\"}]");
}
@SpringBootConfiguration
protected static class TestConfiguration implements Function<Foo, Foo> {
@Override