Skip to content

Commit 8e9948c

Browse files
committed
GH-1075 Fix conversion of complex types
Resolves #1075
1 parent 74d0d96 commit 8e9948c

File tree

2 files changed

+84
-11
lines changed

2 files changed

+84
-11
lines changed

spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,16 +1325,6 @@ private Type extractActualValueTypeIfNecessary(Type type) {
13251325
return type;
13261326
}
13271327

1328-
/*
1329-
*
1330-
*/
1331-
private boolean isConversionHintRequired(Type actualType, Class<?> rawType) {
1332-
if (Collection.class.isAssignableFrom(rawType) || Map.class.isAssignableFrom(rawType)) {
1333-
return true;
1334-
}
1335-
return rawType != actualType && !FunctionTypeUtils.isMessage(actualType);
1336-
}
1337-
13381328
/*
13391329
*
13401330
*/
@@ -1362,7 +1352,7 @@ private Object convertInputMessageIfNecessary(Message message, Type type) {
13621352
Class<?> rawType = FunctionTypeUtils.isMessage(type)
13631353
? FunctionTypeUtils.getRawType(itemType)
13641354
: FunctionTypeUtils.getRawType(type);
1365-
convertedInput = this.isConversionHintRequired(type, rawType)
1355+
convertedInput = type instanceof ParameterizedType
13661356
? SimpleFunctionRegistry.this.messageConverter.fromMessage(message, rawType, itemType)
13671357
: SimpleFunctionRegistry.this.messageConverter.fromMessage(message, rawType);
13681358

spring-cloud-function-context/src/test/java/org/springframework/cloud/function/userissues/UserIssuesTests.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.function.Function;
2222

23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.assertj.core.util.Arrays;
2325
import org.junit.jupiter.api.BeforeEach;
2426
import org.junit.jupiter.api.Test;
2527
import reactor.core.publisher.Flux;
@@ -36,6 +38,7 @@
3638
import org.springframework.messaging.support.GenericMessage;
3739

3840

41+
3942
import static org.assertj.core.api.Assertions.assertThat;
4043

4144
/**
@@ -66,6 +69,26 @@ public void testIssue602() throws Exception {
6669
assertThat(result).isEqualTo(3);
6770
}
6871

72+
@SuppressWarnings({ "unchecked", "rawtypes" })
73+
@Test
74+
public void testIssue1075() throws Exception {
75+
FunctionCatalog catalog = this.configureCatalog(Issue1075StreamConfiguration.class);
76+
77+
78+
List<Object> list = Arrays.asList(new Product[] {new Product("foo"), new Product("bar")});
79+
Event event = new Event(list);
80+
EventHolder eventHolder = new EventHolder(event);
81+
ObjectMapper mapper = new ObjectMapper();
82+
String message = mapper.writeValueAsString(eventHolder);
83+
Function function = catalog.lookup("somethingYouShouldNeverDo");
84+
boolean result = (boolean) function.apply(
85+
new GenericMessage<String>(message));
86+
assertThat(result).isTrue();
87+
}
88+
89+
90+
91+
6992
@Test
7093
public void testIssue602asPOJO() throws Exception {
7194
FunctionCatalog catalog = this.configureCatalog(Issue602Configuration.class);
@@ -128,6 +151,59 @@ public Function<List<Product>, Integer> consumer() {
128151
}
129152
}
130153

154+
@EnableAutoConfiguration
155+
@Configuration
156+
public static class Issue1075StreamConfiguration {
157+
@Bean
158+
public Function<Message<EventHolder<Event<List<Product>>>>, Boolean> somethingYouShouldNeverDo() {
159+
return message -> {
160+
List<Product> products = message.getPayload().getPayload().getPayload();
161+
assertThat(products.get(0).getName()).isEqualTo("foo");
162+
assertThat(products.get(1).getName()).isEqualTo("bar");
163+
return true;
164+
};
165+
}
166+
}
167+
168+
public static class EventHolder<T> {
169+
private T payload;
170+
171+
public EventHolder() {
172+
}
173+
174+
public EventHolder(T payload) {
175+
this.payload = payload;
176+
}
177+
178+
public T getPayload() {
179+
return payload;
180+
}
181+
182+
public void setPayload(T payload) {
183+
this.payload = payload;
184+
}
185+
186+
}
187+
188+
public static class Event<T> {
189+
private T payload;
190+
191+
public Event() {
192+
}
193+
194+
public Event(T payload) {
195+
this.payload = payload;
196+
}
197+
198+
public T getPayload() {
199+
return payload;
200+
}
201+
202+
public void setPayload(T payload) {
203+
this.payload = payload;
204+
}
205+
}
206+
131207
@EnableAutoConfiguration
132208
@Configuration
133209
public static class Issue601Configuration {
@@ -148,6 +224,13 @@ public Flux<Integer> apply(Flux<String> s) {
148224
public static class Product {
149225
private String name;
150226

227+
public Product() {
228+
}
229+
230+
public Product(String name) {
231+
this.name = name;
232+
}
233+
151234
public String getName() {
152235
return name;
153236
}

0 commit comments

Comments
 (0)