Skip to content

Commit ef372f1

Browse files
committed
fix: 通过JsonPath获取基本类型字段错误
1 parent 0f2ceac commit ef372f1

File tree

7 files changed

+89
-8
lines changed

7 files changed

+89
-8
lines changed

forest-core/src/main/java/com/dtflys/forest/handler/ResultHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public Object getResult(Optional<?> resultOpt, ForestRequest request, ForestResp
8686
return null;
8787
}
8888
final Class<?> optValueClass = optValue.getClass();
89-
if (resultClass.isAssignableFrom(optValueClass)) {
89+
if (ReflectUtils.isAssignableFrom(resultClass, optValueClass)) {
9090
return optValue;
9191
}
9292
if (ForestResponse.class.isAssignableFrom(resultClass)) {
@@ -107,7 +107,7 @@ public Object getResult(Optional<?> resultOpt, ForestRequest request, ForestResp
107107
return response;
108108
}
109109
}
110-
if (Charset.class.isAssignableFrom(optValueClass)) {
110+
if (ReflectUtils.isPrimaryType(optValueClass)) {
111111
optStringValue = String.valueOf(optValue);
112112
}
113113
}

forest-core/src/main/java/com/dtflys/forest/http/ResultGetter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.dtflys.forest.http;
22

3+
import com.dtflys.forest.converter.ForestConverter;
34
import com.dtflys.forest.exceptions.ForestRuntimeException;
45
import com.dtflys.forest.handler.ResultHandler;
56
import com.dtflys.forest.mapping.MappingTemplate;
7+
import com.dtflys.forest.utils.ForestDataType;
8+
import com.dtflys.forest.utils.ReflectUtils;
69
import com.dtflys.forest.utils.StringUtils;
710
import com.dtflys.forest.utils.TypeReference;
811
import com.jayway.jsonpath.Configuration;
@@ -95,9 +98,11 @@ public <T> T getByPath(String path, Type type) {
9598
final Object document = Configuration.defaultConfiguration().jsonProvider().parse(getResponse().getInputStream(), charset);
9699
final ReadContext ctx = JsonPath.parse(document);
97100
final Object obj = ctx.read(pathStr);
98-
final String content = JsonPath.parse(obj).jsonString();
99-
return request.getConfiguration()
100-
.getJsonConverter()
101+
final Class objClass = obj.getClass();
102+
final String content = ReflectUtils.isPrimaryType(objClass)
103+
? String.valueOf(obj) : JsonPath.parse(obj).jsonString();
104+
return (T) request.getConfiguration()
105+
.getConverter(ForestDataType.AUTO)
101106
.convertToJavaObject(new ByteArrayInputStream(content.getBytes(charset)), type);
102107
} catch (Throwable th) {
103108
throw new ForestRuntimeException(th);

forest-core/src/main/java/com/dtflys/forest/utils/ReflectUtils.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,36 @@ public static boolean isPrimaryType(final Class<?> type) {
229229
return false;
230230
}
231231

232+
233+
public static boolean isAssignableFrom(final Class<?> type1, final Class<?> type2) {
234+
if (type1.isAssignableFrom(type2)) {
235+
return true;
236+
}
237+
if (isPrimaryType(type1) && isPrimaryType(type2)) {
238+
if (int.class.isAssignableFrom(type1) && Integer.class.isAssignableFrom(type2)) {
239+
return true;
240+
}
241+
if (long.class.isAssignableFrom(type1) && Long.class.isAssignableFrom(type2)) {
242+
return true;
243+
}
244+
if (short.class.isAssignableFrom(type1) && Short.class.isAssignableFrom(type2)) {
245+
return true;
246+
}
247+
if (float.class.isAssignableFrom(type1) && Float.class.isAssignableFrom(type2)) {
248+
return true;
249+
}
250+
if (double.class.isAssignableFrom(type1) && Double.class.isAssignableFrom(type2)) {
251+
return true;
252+
}
253+
if (char.class.isAssignableFrom(type1) && Character.class.isAssignableFrom(type2)) {
254+
return true;
255+
}
256+
if (boolean.class.isAssignableFrom(type1) && Boolean.class.isAssignableFrom(type2)) {
257+
return true;
258+
}
259+
}
260+
return false;
261+
}
232262

233263
/**
234264
* 是否为基本数组类型

forest-core/src/test/java/com/dtflys/forest/test/http/TestDownloadClient.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ public void testDownloadWithExecuteAsStream() throws IOException {
402402
.executeAsStream((in, req, res) -> {
403403
responseBytesRead.set(res.isBytesRead());
404404
System.out.println("Accept stream");
405-
List<UserParam> userParam = new Gson().fromJson(new JsonReader(new InputStreamReader(in)), UserParam.class);
406405
try {
407406
byte[] fileBytes = IOUtils.toByteArray(in);
408407
assertThat(fileBytes)

forest-core/src/test/java/com/dtflys/forest/test/jsonpath/TestJSONPath.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
import java.util.List;
1313

14+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
15+
1416
public class TestJSONPath {
1517

16-
public final static String SINGLE_EXPECTED = "{\"status\":\"ok\", \"data\": {\"name\": \"Foo\", \"age\": 12}}";
18+
public final static String SINGLE_EXPECTED = "{\"status\":\"ok\", \"data\": {\"name\": \"中文\", \"age\": 12}}";
1719

1820
public final static String LIST_EXPECTED = "{\"status\":\"ok\", \"data\": [{\"name\": \"Foo\", \"age\": 12}, {\"name\": \"Bar\", \"age\": 22}]}";
1921

@@ -41,6 +43,21 @@ public void testGetSingleUser() {
4143
System.out.println(JSONObject.toJSONString(user));
4244
}
4345

46+
@Test
47+
public void testGetNameOfSingleUser() {
48+
server.enqueue(new MockResponse().setResponseCode(200).setBody(SINGLE_EXPECTED));
49+
String name = testJSONPathClient.getNameOfSingleUser();
50+
assertThat(name).isEqualTo("中文");
51+
}
52+
53+
@Test
54+
public void testGetAgeOfSingleUser() {
55+
server.enqueue(new MockResponse().setResponseCode(200).setBody(SINGLE_EXPECTED));
56+
int age = testJSONPathClient.getAgeOfSingleUser();
57+
assertThat(age).isEqualTo(12);
58+
}
59+
60+
4461
@Test
4562
public void testGetListOfUser() {
4663
server.enqueue(new MockResponse().setResponseCode(200).setBody(LIST_EXPECTED));

forest-core/src/test/java/com/dtflys/forest/test/jsonpath/TestJSONPathClient.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ public interface TestJSONPathClient {
1313
@JSONPathResult("$.data")
1414
TestUser getSingleUser();
1515

16+
@Get("http://localhost:{port}/test/user")
17+
@JSONPathResult("$.data.name")
18+
String getNameOfSingleUser();
19+
20+
@Get("http://localhost:{port}/test/user")
21+
@JSONPathResult("$.data.age")
22+
int getAgeOfSingleUser();
23+
1624
@Get("http://localhost:{port}/test/user")
1725
@JSONPathResult("$.data")
1826
List<TestUser> getListOfUsers();
19-
27+
2028
@Get("http://localhost:{port}/test/user")
2129
@JSONPathResult("$.data[*].age")
2230
List<Integer> getListOfUserAges();

forest-examples/example-springboot/src/main/java/com/dtflys/forest/example/controller/TestSSEController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import reactor.core.publisher.Flux;
1717

1818
import javax.annotation.Resource;
19+
import java.io.IOException;
1920
import java.time.Duration;
2021
import java.time.LocalDateTime;
2122

@@ -56,4 +57,25 @@ public String testStreamWithMyHandler() {
5657
}
5758

5859

60+
@GetMapping(value = "/sse-emitter-test")
61+
public SseEmitter testSSEEmitterTest() {
62+
SseEmitter sseEmitter = new SseEmitter(-1L);
63+
sseClient.stream()
64+
.addOnData((eventSource, name, value) -> {
65+
log.info("Received event [{}: {}]", name, value);
66+
try {
67+
sseEmitter.send("Received event [" + name + ": " + value + "]");
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
})
72+
.setOnClose((eventSource, res) -> {
73+
log.info("SSE Closed");
74+
eventSource.sse().close();
75+
})
76+
.asyncListen();
77+
return sseEmitter;
78+
}
79+
80+
5981
}

0 commit comments

Comments
 (0)