GH-499,498 Add support for SupplierExporter to control output content-type
- Add 'contentType' property to ExporterProperties to assist SupplierExporter with delegating it to function catalog - Add additional logging and testing - Change JsonMapper to abstract class providing special handling of conversion of Json Sting to byte[]
This commit is contained in:
@@ -319,7 +319,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
|
||||
registrationsByFunction.putIfAbsent(function, registration);
|
||||
registrationsByName.putIfAbsent(name, registration);
|
||||
function = new FunctionInvocationWrapper(function, currentFunctionType, name, acceptedOutputTypes);
|
||||
function = new FunctionInvocationWrapper(function, currentFunctionType, name, names.length > 1 ? new String[] {} : acceptedOutputTypes);
|
||||
|
||||
if (originFunctionType == null) {
|
||||
originFunctionType = currentFunctionType;
|
||||
@@ -447,6 +447,10 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
this.headersField.setAccessible(true);
|
||||
}
|
||||
|
||||
public String getFunctionDefinition() {
|
||||
return this.functionDefinition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Object input) {
|
||||
this.doApply(input, true, null);
|
||||
@@ -505,6 +509,11 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "definition: " + this.functionDefinition + "; type: " + this.functionType;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private Object invokeFunction(Object input) {
|
||||
Object invocationResult = null;
|
||||
|
||||
@@ -27,7 +27,7 @@ import com.google.gson.JsonElement;
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class GsonMapper implements JsonMapper {
|
||||
public class GsonMapper extends JsonMapper {
|
||||
|
||||
private final Gson gson;
|
||||
|
||||
@@ -65,7 +65,11 @@ public class GsonMapper implements JsonMapper {
|
||||
|
||||
@Override
|
||||
public byte[] toJson(Object value) {
|
||||
return this.gson.toJson(value).getBytes(StandardCharsets.UTF_8);
|
||||
byte[] jsonBytes = super.toJson(value);
|
||||
if (jsonBytes == null) {
|
||||
jsonBytes = this.gson.toJson(value).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
return jsonBytes;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,12 +23,16 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class JacksonMapper implements JsonMapper {
|
||||
public class JacksonMapper extends JsonMapper {
|
||||
|
||||
private static Log logger = LogFactory.getLog(JsonMapper.class);
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
@@ -58,20 +62,23 @@ public class JacksonMapper implements JsonMapper {
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//ignore and let other converters have a chance
|
||||
logger.warn("Failed to convert. Possible bug as the conversion probably shouldn't have been attampted here", e);
|
||||
}
|
||||
return convertedValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toJson(Object value) {
|
||||
try {
|
||||
return this.mapper.writeValueAsBytes(value);
|
||||
byte[] jsonBytes = super.toJson(value);
|
||||
if (jsonBytes == null) {
|
||||
try {
|
||||
jsonBytes = this.mapper.writeValueAsBytes(value);
|
||||
}
|
||||
catch (Exception e) {
|
||||
//ignore and let other converters have a chance
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
//ignore and let other converters have a chance
|
||||
}
|
||||
return null;
|
||||
return jsonBytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -17,35 +17,41 @@
|
||||
package org.springframework.cloud.function.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public interface JsonMapper {
|
||||
public abstract class JsonMapper {
|
||||
|
||||
private static Log logger = LogFactory.getLog(JsonMapper.class);
|
||||
|
||||
/**
|
||||
* @param <T> type for list arguments
|
||||
* @param <T> type for list arguments
|
||||
* @param json JSON input
|
||||
* @param type type of list arguments
|
||||
* @return list of elements
|
||||
* @deprecated since v2.0 in favor of {@link #toObject(String, Type)}
|
||||
*/
|
||||
@Deprecated
|
||||
default <T> List<T> toList(String json, Class<T> type) {
|
||||
<T> List<T> toList(String json, Class<T> type) {
|
||||
Type actualType = (json.startsWith("[") && !List.class.isAssignableFrom(type))
|
||||
? ResolvableType.forClassWithGenerics(ArrayList.class, (Class<?>) type)
|
||||
.getType()
|
||||
? ResolvableType.forClassWithGenerics(ArrayList.class, (Class<?>) type).getType()
|
||||
: type;
|
||||
return toObject(json, actualType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T> return type
|
||||
* @param <T> return type
|
||||
* @param json JSON input
|
||||
* @param type type
|
||||
* @return object
|
||||
@@ -53,24 +59,39 @@ public interface JsonMapper {
|
||||
* @deprecated since v3.0.4 in favor of {@link #fromJson(Object, Type)}
|
||||
*/
|
||||
@Deprecated
|
||||
<T> T toObject(String json, Type type);
|
||||
abstract <T> T toObject(String json, Type type);
|
||||
|
||||
<T> T fromJson(Object json, Type type);
|
||||
public abstract <T> T fromJson(Object json, Type type);
|
||||
|
||||
byte[] toJson(Object value);
|
||||
public byte[] toJson(Object value) {
|
||||
if (value instanceof String) {
|
||||
try {
|
||||
new JSONObject((String) value);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
"String already represents JSON. Skipping conversion in favor of 'getBytes(StandardCharsets.UTF_8'.");
|
||||
}
|
||||
return ((String) value).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T> type for list arguments
|
||||
* @param <T> type for list arguments
|
||||
* @param json JSON input
|
||||
* @param type type of list arguments
|
||||
* @return single object
|
||||
* @deprecated since v2.0 in favor of {@link #toObject(String, Type)}
|
||||
*/
|
||||
@Deprecated
|
||||
default <T> T toSingle(String json, Class<T> type) {
|
||||
<T> T toSingle(String json, Class<T> type) {
|
||||
return toObject(json, type);
|
||||
}
|
||||
|
||||
String toString(Object value);
|
||||
public abstract String toString(Object value);
|
||||
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class JsonMapperTests {
|
||||
@Test
|
||||
public void vanillaArray() {
|
||||
String json = "[{\"value\":\"foo\"},{\"value\":\"foo\"}]";
|
||||
List<Foo> list = this.mapper.toObject(json,
|
||||
List<Foo> list = this.mapper.fromJson(json,
|
||||
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0).getValue()).isEqualTo("foo");
|
||||
@@ -65,7 +65,7 @@ public class JsonMapperTests {
|
||||
|
||||
@Test
|
||||
public void intArray() {
|
||||
List<Integer> list = this.mapper.toObject("[123,456]",
|
||||
List<Integer> list = this.mapper.fromJson("[123,456]",
|
||||
ResolvableType.forClassWithGenerics(List.class, Integer.class).getType());
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(list.get(0)).isEqualTo(123);
|
||||
@@ -73,7 +73,7 @@ public class JsonMapperTests {
|
||||
|
||||
@Test
|
||||
public void emptyArray() {
|
||||
List<Foo> list = this.mapper.toObject("[]",
|
||||
List<Foo> list = this.mapper.fromJson("[]",
|
||||
ResolvableType.forClassWithGenerics(List.class, Foo.class).getType());
|
||||
assertThat(list).hasSize(0);
|
||||
}
|
||||
@@ -81,20 +81,27 @@ public class JsonMapperTests {
|
||||
@Test
|
||||
public void vanillaObject() {
|
||||
String json = "{\"value\":\"foo\"}";
|
||||
Foo foo = this.mapper.toObject(json, Foo.class);
|
||||
Foo foo = this.mapper.fromJson(json, Foo.class);
|
||||
assertThat(foo.getValue()).isEqualTo("foo");
|
||||
assertThat(this.mapper.toString(foo)).isEqualTo(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringRepresentingJson() {
|
||||
String json = "{\"value\":\"foo\"}";
|
||||
byte[] bytes = this.mapper.toJson(json);
|
||||
assertThat(new String(bytes)).isEqualTo(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void intValue() {
|
||||
int foo = this.mapper.toObject("123", Integer.class);
|
||||
int foo = this.mapper.fromJson("123", Integer.class);
|
||||
assertThat(foo).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void empty() {
|
||||
Foo foo = this.mapper.toObject("{}", Foo.class);
|
||||
Foo foo = this.mapper.fromJson("{}", Foo.class);
|
||||
assertThat(foo.getValue()).isNull();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user