Added support for input Collection param

Added support for function parameter type Collection
Updated both Jackson and Gson mappers
Defined a new toObject() operation on JsonMapper and deprecated the old

Resolves #210
This commit is contained in:
Oleg Zhurakousky
2018-10-24 19:48:41 +02:00
committed by Dave Syer
parent 435e4d0438
commit 2e5c6d22b5
6 changed files with 137 additions and 59 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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.
@@ -15,16 +15,13 @@
*/
package org.springframework.cloud.function.json;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.core.ResolvableType;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
*
*/
public class GsonMapper implements JsonMapper {
@@ -36,13 +33,7 @@ public class GsonMapper implements JsonMapper {
}
@Override
public <T> List<T> toList(String json, Class<T> type) {
return gson.fromJson(json,
ResolvableType.forClassWithGenerics(ArrayList.class, type).getType());
}
@Override
public <T> T toSingle(String json, Class<T> type) {
public <T> T toObject(String json, Type type) {
return gson.fromJson(json, type);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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.
@@ -15,17 +15,15 @@
*/
package org.springframework.cloud.function.json;
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.function.json.JsonMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
* @author Dave Syer
*
* @author Oleg Zhurakousky
*/
public class JacksonMapper implements JsonMapper {
@@ -36,23 +34,12 @@ public class JacksonMapper implements JsonMapper {
}
@Override
public <T> List<T> toList(String json, Class<T> type) {
public <T> T toObject(String json, Type type) {
try {
return mapper.readValue(json, mapper.getTypeFactory()
.constructCollectionLikeType(ArrayList.class, type));
return mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));
}
catch (Exception e) {
throw new IllegalArgumentException("Cannot convert JSON", e);
}
}
@Override
public <T> T toSingle(String json, Class<T> type) {
try {
return mapper.readValue(json, type);
}
catch (Exception e) {
throw new IllegalArgumentException("Cannot convert JSON", e);
throw new IllegalArgumentException("Cannot convert JSON " + json, e);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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.
@@ -15,17 +15,41 @@
*/
package org.springframework.cloud.function.json;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.ResolvableType;
/**
* @author Dave Syer
*
* @author Oleg Zhurakousky
*/
public interface JsonMapper {
<T> List<T> toList(String json, Class<T> type);
/**
* @deprecated in favor of {@link #toObject(String, Type)}
*/
@Deprecated
default <T> List<T> toList(String json, Class<T> type) {
Type actualType = (json.startsWith("[") && !List.class.isAssignableFrom(type))
? ResolvableType.forClassWithGenerics(ArrayList.class, (Class<?>) type)
.getType()
: type;
return toObject(json, actualType);
}
<T> T toSingle(String json, Class<T> type);
/**
* @since 2.0
*/
<T> T toObject(String json, Type type);
/**
* @deprecated in favor of {@link #toObject(String, Type)}
*/
default <T> T toSingle(String json, Class<T> type) {
return toObject(json, type);
}
String toString(Object value);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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.
@@ -29,11 +29,13 @@ import org.junit.runners.Parameterized.Parameters;
import org.springframework.cloud.function.json.GsonMapper;
import org.springframework.cloud.function.json.JacksonMapper;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.core.ResolvableType;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
*
*/
@RunWith(Parameterized.class)
@@ -54,8 +56,8 @@ public class JsonMapperTests {
@Test
public void vanillaArray() {
String json = "[{\"value\":\"foo\"},{\"value\":\"foo\"}]";
List<Foo> list = mapper.toList(json,
Foo.class);
List<Foo> list = mapper.toObject(json,
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
assertThat(list).hasSize(2);
assertThat(list.get(0).getValue()).isEqualTo("foo");
assertThat(mapper.toString(list)).isEqualTo(json);
@@ -63,34 +65,36 @@ public class JsonMapperTests {
@Test
public void intArray() {
List<Integer> list = mapper.toList("[123,456]", Integer.class);
List<Integer> list = mapper.toObject("[123,456]",
ResolvableType.forClassWithGenerics(List.class, Integer.class).getType());
assertThat(list).hasSize(2);
assertThat(list.get(0)).isEqualTo(123);
}
@Test
public void emptyArray() {
List<Foo> list = mapper.toList("[]", Foo.class);
List<Foo> list = mapper.toObject("[]",
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
assertThat(list).hasSize(0);
}
@Test
public void vanillaObject() {
String json = "{\"value\":\"foo\"}";
Foo foo = mapper.toSingle(json, Foo.class);
Foo foo = mapper.toObject(json, Foo.class);
assertThat(foo.getValue()).isEqualTo("foo");
assertThat(mapper.toString(foo)).isEqualTo(json);
}
@Test
public void intValue() {
int foo = mapper.toSingle("123", Integer.class);
int foo = mapper.toObject("123", Integer.class);
assertThat(foo).isEqualTo(123);
}
@Test
public void empty() {
Foo foo = mapper.toSingle("{}", Foo.class);
Foo foo = mapper.toObject("{}", Foo.class);
assertThat(foo.getValue()).isNull();
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.function.web;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -36,6 +37,7 @@ import org.springframework.cloud.function.context.message.MessageUtils;
import org.springframework.cloud.function.core.FluxWrapper;
import org.springframework.cloud.function.json.JsonMapper;
import org.springframework.cloud.function.web.util.HeaderUtils;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -72,12 +74,15 @@ public class RequestProcessor {
this.converter = converter;
}
public static FunctionWrapper wrapper(Function<? extends Publisher<?>, ? extends Publisher<?>> function,
Consumer<? extends Publisher<?>> consumer, Supplier<? extends Publisher<?>> supplier) {
public static FunctionWrapper wrapper(
Function<? extends Publisher<?>, ? extends Publisher<?>> function,
Consumer<? extends Publisher<?>> consumer,
Supplier<? extends Publisher<?>> supplier) {
return new FunctionWrapper(function, consumer, supplier);
}
public static FunctionWrapper wrapper(Function<? extends Publisher<?>, ? extends Publisher<?>> function) {
public static FunctionWrapper wrapper(
Function<? extends Publisher<?>, ? extends Publisher<?>> function) {
return new FunctionWrapper(function, null, null);
}
@@ -94,21 +99,24 @@ public class RequestProcessor {
public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body,
boolean stream) {
Mono<ResponseEntity<?>> responseEntityMono;
Object function = wrapper.handler();
Class<?> inputType = inspector.getInputType(function);
Object input = null;
if (StringUtils.hasText(body)) {
Class<?> inputType = inspector.getInputType(function);
if (body.startsWith("[")) {
input = mapper.toList(body, inputType);
input = Collection.class.isAssignableFrom(inputType)
? mapper.toObject(body, inputType)
: mapper.toObject(body, ResolvableType
.forClassWithGenerics(ArrayList.class, (Class<?>) inputType)
.getType());
}
else {
if (inputType == String.class) {
input = body;
}
else if (body.startsWith("{")) {
input = mapper.toSingle(body, inputType);
input = mapper.toObject(body, inputType);
}
else if (body.startsWith("\"")) {
input = body.substring(1, body.length() - 2);
@@ -118,8 +126,8 @@ public class RequestProcessor {
}
}
}
responseEntityMono = post(wrapper, input, null, stream);
return responseEntityMono;
return post(wrapper, input, null, stream,
!Collection.class.isAssignableFrom(inputType));
}
public Mono<ResponseEntity<?>> stream(FunctionWrapper request) {
@@ -130,7 +138,8 @@ public class RequestProcessor {
}
private Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, Object body,
MultiValueMap<String, String> params, boolean stream) {
MultiValueMap<String, String> params, boolean stream,
boolean shouldFluxAsIterable) {
Iterable<?> iterable = body instanceof Collection ? (List<?>) body
: Collections.singletonList(body);
@@ -143,7 +152,8 @@ public class RequestProcessor {
form.putAll(params);
}
Flux<?> flux = body == null ? Flux.just(form) : Flux.fromIterable(iterable);
Flux<?> flux = body == null ? Flux.just(form)
: shouldFluxAsIterable ? Flux.fromIterable(iterable) : Flux.just(body);
if (inspector.isMessage(function)) {
flux = messages(wrapper, function == null ? consumer : function, flux);
}
@@ -239,7 +249,8 @@ public class RequestProcessor {
}
}
private Publisher<?> value(Function<Publisher<?>, Publisher<?>> function, Publisher<String> value) {
private Publisher<?> value(Function<Publisher<?>, Publisher<?>> function,
Publisher<String> value) {
Flux<?> input = Flux.from(value).map(body -> converter.convert(function, body));
return Mono.from(function.apply(input));
}
@@ -259,8 +270,10 @@ public class RequestProcessor {
private Publisher<String> argument;
@SuppressWarnings("unchecked")
public FunctionWrapper(Function<? extends Publisher<?>, ? extends Publisher<?>> function,
Consumer<? extends Publisher<?>> consumer, Supplier<? extends Publisher<?>> supplier) {
public FunctionWrapper(
Function<? extends Publisher<?>, ? extends Publisher<?>> function,
Consumer<? extends Publisher<?>> consumer,
Supplier<? extends Publisher<?>> supplier) {
this.function = (Function<Publisher<?>, Publisher<?>>) function;
this.consumer = (Consumer<Publisher<?>>) consumer;
this.supplier = (Supplier<Publisher<?>>) supplier;

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2012-2015 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.test;
import java.util.List;
import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
@FunctionalSpringBootTest("spring.main.web-application-type=reactive")
@AutoConfigureWebTestClient
public class FunctionalWithInputCollectionTests {
@Autowired
private WebTestClient client;
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("[\"foo\", \"bar\"]"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("[FOO, BAR]");
}
@SpringBootConfiguration
protected static class TestConfiguration implements Function<List<String>, String> {
@Override
public String apply(List<String> value) {
return value.toString().toUpperCase();
}
}
}