@@ -31,6 +31,7 @@ import java.util.function.Function;
|
|||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import net.jodah.typetools.TypeResolver;
|
import net.jodah.typetools.TypeResolver;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
@@ -52,6 +53,8 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set of utility operations to interrogate function definitions.
|
* Set of utility operations to interrogate function definitions.
|
||||||
*
|
*
|
||||||
@@ -82,13 +85,17 @@ public final class FunctionTypeUtils {
|
|||||||
}
|
}
|
||||||
type = getGenericType(type);
|
type = getGenericType(type);
|
||||||
Class<?> rawType = type instanceof ParameterizedType ? getRawType(type) : (Class<?>) type;
|
Class<?> rawType = type instanceof ParameterizedType ? getRawType(type) : (Class<?>) type;
|
||||||
return Collection.class.isAssignableFrom(rawType);
|
return Collection.class.isAssignableFrom(rawType) || JsonNode.class.isAssignableFrom(rawType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isTypeArray(Type type) {
|
public static boolean isTypeArray(Type type) {
|
||||||
return getRawType(type).isArray();
|
return getRawType(type).isArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isJsonNode(Type type) {
|
||||||
|
return getRawType(type).isArray();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A convenience method identical to {@link #getImmediateGenericType(Type, int)}
|
* A convenience method identical to {@link #getImmediateGenericType(Type, int)}
|
||||||
* for cases when provided 'type' is {@link Publisher} or {@link Message}.
|
* for cases when provided 'type' is {@link Publisher} or {@link Message}.
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import java.util.function.Function;
|
|||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
@@ -104,6 +105,15 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
|||||||
System.clearProperty("spring.cloud.function.definition");
|
System.clearProperty("spring.cloud.function.definition");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJsonNodeAsInput() throws Exception {
|
||||||
|
FunctionCatalog catalog = this.configureCatalog(JsonNodeConfiguration.class);
|
||||||
|
Function<Message<String>, Message<byte[]>> f = catalog.lookup("messageAsJsonNode", "application/json");
|
||||||
|
Message<String> m = MessageBuilder.withPayload("[{\"name\":\"bob\"}, {\"name\":\"bob\"}]").setHeader(MessageHeaders.CONTENT_TYPE, "application/json").build();
|
||||||
|
assertThat(new String(f.apply(m).getPayload())).isEqualTo("[{\"name\":\"bob\"},{\"name\":\"bob\"}]");
|
||||||
|
f = catalog.lookup("asJsonNode", "application/json");
|
||||||
|
assertThat(new String(f.apply(m).getPayload())).isEqualTo("[{\"name\":\"bob\"},{\"name\":\"bob\"}]");
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "rawtypes" })
|
@SuppressWarnings({ "rawtypes" })
|
||||||
@Test
|
@Test
|
||||||
@@ -782,6 +792,24 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@Configuration
|
||||||
|
public static class JsonNodeConfiguration {
|
||||||
|
@Bean
|
||||||
|
public Function<Message<JsonNode>, String> messageAsJsonNode() {
|
||||||
|
return v -> {
|
||||||
|
return v.getPayload().toString();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Function<JsonNode, String> asJsonNode() {
|
||||||
|
return v -> {
|
||||||
|
return v.toString();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
public static class EmptyConfiguration {
|
public static class EmptyConfiguration {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user