Skip to content

Commit

Permalink
fix: 通过JsonPath获取基本类型字段错误
Browse files Browse the repository at this point in the history
  • Loading branch information
mySingleLive committed Dec 24, 2024
1 parent 0f2ceac commit ef372f1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Object getResult(Optional<?> resultOpt, ForestRequest request, ForestResp
return null;
}
final Class<?> optValueClass = optValue.getClass();
if (resultClass.isAssignableFrom(optValueClass)) {
if (ReflectUtils.isAssignableFrom(resultClass, optValueClass)) {
return optValue;
}
if (ForestResponse.class.isAssignableFrom(resultClass)) {
Expand All @@ -107,7 +107,7 @@ public Object getResult(Optional<?> resultOpt, ForestRequest request, ForestResp
return response;
}
}
if (Charset.class.isAssignableFrom(optValueClass)) {
if (ReflectUtils.isPrimaryType(optValueClass)) {
optStringValue = String.valueOf(optValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.dtflys.forest.http;

import com.dtflys.forest.converter.ForestConverter;
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.handler.ResultHandler;
import com.dtflys.forest.mapping.MappingTemplate;
import com.dtflys.forest.utils.ForestDataType;
import com.dtflys.forest.utils.ReflectUtils;
import com.dtflys.forest.utils.StringUtils;
import com.dtflys.forest.utils.TypeReference;
import com.jayway.jsonpath.Configuration;
Expand Down Expand Up @@ -95,9 +98,11 @@ public <T> T getByPath(String path, Type type) {
final Object document = Configuration.defaultConfiguration().jsonProvider().parse(getResponse().getInputStream(), charset);
final ReadContext ctx = JsonPath.parse(document);
final Object obj = ctx.read(pathStr);
final String content = JsonPath.parse(obj).jsonString();
return request.getConfiguration()
.getJsonConverter()
final Class objClass = obj.getClass();
final String content = ReflectUtils.isPrimaryType(objClass)
? String.valueOf(obj) : JsonPath.parse(obj).jsonString();
return (T) request.getConfiguration()
.getConverter(ForestDataType.AUTO)
.convertToJavaObject(new ByteArrayInputStream(content.getBytes(charset)), type);
} catch (Throwable th) {
throw new ForestRuntimeException(th);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,36 @@ public static boolean isPrimaryType(final Class<?> type) {
return false;
}


public static boolean isAssignableFrom(final Class<?> type1, final Class<?> type2) {
if (type1.isAssignableFrom(type2)) {
return true;
}
if (isPrimaryType(type1) && isPrimaryType(type2)) {
if (int.class.isAssignableFrom(type1) && Integer.class.isAssignableFrom(type2)) {
return true;
}
if (long.class.isAssignableFrom(type1) && Long.class.isAssignableFrom(type2)) {
return true;
}
if (short.class.isAssignableFrom(type1) && Short.class.isAssignableFrom(type2)) {
return true;
}
if (float.class.isAssignableFrom(type1) && Float.class.isAssignableFrom(type2)) {
return true;
}
if (double.class.isAssignableFrom(type1) && Double.class.isAssignableFrom(type2)) {
return true;
}
if (char.class.isAssignableFrom(type1) && Character.class.isAssignableFrom(type2)) {
return true;
}
if (boolean.class.isAssignableFrom(type1) && Boolean.class.isAssignableFrom(type2)) {
return true;
}
}
return false;
}

/**
* 是否为基本数组类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ public void testDownloadWithExecuteAsStream() throws IOException {
.executeAsStream((in, req, res) -> {
responseBytesRead.set(res.isBytesRead());
System.out.println("Accept stream");
List<UserParam> userParam = new Gson().fromJson(new JsonReader(new InputStreamReader(in)), UserParam.class);
try {
byte[] fileBytes = IOUtils.toByteArray(in);
assertThat(fileBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class TestJSONPath {

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

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

Expand Down Expand Up @@ -41,6 +43,21 @@ public void testGetSingleUser() {
System.out.println(JSONObject.toJSONString(user));
}

@Test
public void testGetNameOfSingleUser() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(SINGLE_EXPECTED));
String name = testJSONPathClient.getNameOfSingleUser();
assertThat(name).isEqualTo("中文");
}

@Test
public void testGetAgeOfSingleUser() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(SINGLE_EXPECTED));
int age = testJSONPathClient.getAgeOfSingleUser();
assertThat(age).isEqualTo(12);
}


@Test
public void testGetListOfUser() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(LIST_EXPECTED));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ public interface TestJSONPathClient {
@JSONPathResult("$.data")
TestUser getSingleUser();

@Get("http://localhost:{port}/test/user")
@JSONPathResult("$.data.name")
String getNameOfSingleUser();

@Get("http://localhost:{port}/test/user")
@JSONPathResult("$.data.age")
int getAgeOfSingleUser();

@Get("http://localhost:{port}/test/user")
@JSONPathResult("$.data")
List<TestUser> getListOfUsers();

@Get("http://localhost:{port}/test/user")
@JSONPathResult("$.data[*].age")
List<Integer> getListOfUserAges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDateTime;

Expand Down Expand Up @@ -56,4 +57,25 @@ public String testStreamWithMyHandler() {
}


@GetMapping(value = "/sse-emitter-test")
public SseEmitter testSSEEmitterTest() {
SseEmitter sseEmitter = new SseEmitter(-1L);
sseClient.stream()
.addOnData((eventSource, name, value) -> {
log.info("Received event [{}: {}]", name, value);
try {
sseEmitter.send("Received event [" + name + ": " + value + "]");
} catch (IOException e) {
e.printStackTrace();
}
})
.setOnClose((eventSource, res) -> {
log.info("SSE Closed");
eventSource.sse().close();
})
.asyncListen();
return sseEmitter;
}


}

0 comments on commit ef372f1

Please sign in to comment.