From 477b5530f7502aac5882789ee1956df124848370 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 5 Nov 2020 16:23:14 +0100 Subject: [PATCH] GH-602 Add test to validate issue --- .../catalog/SimpleFunctionRegistry.java | 23 ---- .../function/userissues/UserIssuesTests.java | 124 ++++++++++++++++++ 2 files changed, 124 insertions(+), 23 deletions(-) create mode 100644 spring-cloud-function-context/src/test/java/org/springframework/cloud/function/userissues/UserIssuesTests.java diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java index b9256738c..18737a1da 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/SimpleFunctionRegistry.java @@ -789,9 +789,6 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect if (this.skipInputConversion && !(value instanceof Publisher)) { if (!FunctionTypeUtils.isMessage(type) && value instanceof Message) { value = ((Message) value).getPayload(); -// input = this.isFunction() -// ? new OriginalMessageHolder(((Message) input).getPayload(), (Message) input) -// : input; } return value; } @@ -834,14 +831,9 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect convertWithHint = true; } else if (!rawType.equals(type)) { -// hint = FunctionTypeUtils.getGenericType(type); convertWithHint = true; } - - - - convertedValue = convertWithHint ? this.fromMessage((Message) value, (Class) rawType, FunctionTypeUtils.getGenericType(type)) : this.fromMessage((Message) value, (Class) rawType, null); @@ -865,21 +857,6 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect } else { //if (rawType instanceof Class) { // see AWS adapter with WildardTypeImpl and Azure with Voids convertedValue = this.convertNonMessageInputIfNecessary(type, value); -// if (this.isJson(value)) { -// convertedValue = messageConverter -// .fromMessage(new GenericMessage(value), (Class) rawType); -// } -// else { -// try { -// convertedValue = conversionService.convert(value, (Class) rawType); -// } -// catch (Exception e) { -// if (value instanceof String || value instanceof byte[]) { -// convertedValue = messageConverter -// .fromMessage(new GenericMessage(value), (Class) rawType); -// } -// } -// } } } if (logger.isDebugEnabled()) { diff --git a/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/userissues/UserIssuesTests.java b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/userissues/UserIssuesTests.java new file mode 100644 index 000000000..ef1bd77a7 --- /dev/null +++ b/spring-cloud-function-context/src/test/java/org/springframework/cloud/function/userissues/UserIssuesTests.java @@ -0,0 +1,124 @@ +/* + * Copyright 2020-2020 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 + * + * https://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.userissues; + + +import java.lang.reflect.Type; +import java.util.List; +import java.util.function.Function; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.function.context.FunctionCatalog; +import org.springframework.cloud.function.json.JsonMapper; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.Message; +import org.springframework.messaging.converter.AbstractMessageConverter; +import org.springframework.messaging.support.GenericMessage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * + * @author Oleg Zhurakousky + * + */ +public class UserIssuesTests { + + private FunctionCatalog configureCatalog(Class... configClass) { + ApplicationContext context = new SpringApplicationBuilder(configClass) + .run("--logging.level.org.springframework.cloud.function=DEBUG", + "--spring.main.lazy-initialization=true"); + FunctionCatalog catalog = context.getBean(FunctionCatalog.class); + return catalog; + } + + @Before + public void before() { + System.clearProperty("spring.cloud.function.definition"); + } + + @Test + public void testIssue602() throws Exception { + FunctionCatalog catalog = this.configureCatalog(Issue602Configuration.class); + Function, Integer> function = catalog.lookup("consumer"); + int result = function.apply(new GenericMessage("[{\"name\":\"julien\"},{\"name\":\"ricky\"},{\"name\":\"bubbles\"}]")); + assertThat(result).isEqualTo(3); + + } + + @EnableAutoConfiguration + @Configuration + public static class Issue602Configuration { + @Bean + public Function, Integer> consumer() { + return v -> { + assertThat(v.get(0).getName()).isEqualTo("julien"); + assertThat(v.get(1).getName()).isEqualTo("ricky"); + assertThat(v.get(2).getName()).isEqualTo("bubbles"); + return v.size(); + }; + } + + @Bean + public ProductMessageConverter productMessageConverter(JsonMapper mapper) { + return new ProductMessageConverter(mapper); + } + } + + private static class Product { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + public static class ProductMessageConverter extends AbstractMessageConverter { + + private final JsonMapper mapper; + + ProductMessageConverter(JsonMapper mapper) { + this.mapper = mapper; + } + + @Override + protected boolean canConvertFrom(Message message, Class targetClass) { + return true; + } + + @Override + protected boolean supports(Class clazz) { + return true; + } + + @Override + protected Object convertFromInternal(Message message, Class targetClass, Object conversionHint) { + Object result = mapper.fromJson((String) message.getPayload(), (Type) conversionHint); + return result; + } + + } +}