Skip to content

Commit 28b1c86

Browse files
committed
GH-745 Ensure RSocket support is ready to receive non-Message
1 parent acc5548 commit 28b1c86

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

Diff for: spring-cloud-function-context/src/main/java/org/springframework/cloud/function/json/JsonMapper.java

+12
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,16 @@ public static boolean isJsonStringRepresentsCollection(Object value) {
158158
}
159159
return isJson;
160160
}
161+
162+
public static boolean isJsonStringRepresentsMap(Object value) {
163+
boolean isJson = false;
164+
if (value instanceof byte[]) {
165+
value = new String((byte[]) value, StandardCharsets.UTF_8);
166+
}
167+
if (value instanceof String) {
168+
String str = ((String) value).trim();
169+
isJson = isJsonString(value) && str.startsWith("{") && str.endsWith("}");
170+
}
171+
return isJson;
172+
}
161173
}

Diff for: spring-cloud-function-rsocket/src/main/java/org/springframework/cloud/function/rsocket/MessageAwareJsonDecoder.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
2323
import java.lang.reflect.Type;
24+
import java.nio.charset.StandardCharsets;
25+
import java.util.HashMap;
2426
import java.util.Map;
2527

2628
import org.reactivestreams.Publisher;
@@ -70,8 +72,19 @@ public Object decode(DataBuffer dataBuffer, ResolvableType targetType,
7072
mimeType, hints);
7173
if (messageMap.containsKey(FunctionRSocketUtils.PAYLOAD)) {
7274
Type requestedType = FunctionTypeUtils.getGenericType(targetType.getType());
73-
Object payload = this.jsonMapper.fromJson(
74-
messageMap.get(FunctionRSocketUtils.PAYLOAD), requestedType);
75+
Object payload;
76+
if (String.class.isAssignableFrom(FunctionTypeUtils.getRawType(targetType.getType()))) {
77+
Object rawPayload = messageMap.get(FunctionRSocketUtils.PAYLOAD);
78+
if (rawPayload instanceof byte[]) {
79+
payload = new String((byte[]) rawPayload, StandardCharsets.UTF_8);
80+
}
81+
else {
82+
payload = rawPayload;
83+
}
84+
}
85+
else {
86+
payload = this.jsonMapper.fromJson(messageMap.get(FunctionRSocketUtils.PAYLOAD), requestedType);
87+
}
7588

7689
if (FunctionTypeUtils.isMessage(targetType.getType())) {
7790
return MessageBuilder.withPayload(payload).copyHeaders(
@@ -93,7 +106,14 @@ private Object doDecode(DataBuffer dataBuffer, ResolvableType targetType,
93106

94107
try {
95108
byte[] data = toByteArray(dataBuffer.asInputStream());
96-
return this.jsonMapper.fromJson(data, targetType.getType());
109+
if (JsonMapper.isJsonStringRepresentsMap(data)) {
110+
return this.jsonMapper.fromJson(data, targetType.getType());
111+
}
112+
else {
113+
Map<String, Object> messageMap = new HashMap<>();
114+
messageMap.put(FunctionRSocketUtils.PAYLOAD, data);
115+
return messageMap;
116+
}
97117
}
98118
catch (IOException ex) {
99119
throw new IllegalStateException(ex);

0 commit comments

Comments
 (0)