Skip to content

Commit 99b4a2c

Browse files
CodeCasterXclaude
andcommitted
fix: 改进 FastJsonValueHandler 错误处理并支持 JSON 字符串
- 为 fetch/set 操作添加显式的 JSONException 错误处理 - 当 object 为 String 类型时,防止自动解析 JSON 字符串 - 支持 "$" 作为空路径的别名来表示对象自身 - 添加 JSON 字符串行为的完整测试用例 - 更新版权年份为 2024-2025 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent cd8e071 commit 99b4a2c

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fit.value.fastjson;
88

9+
import com.alibaba.fastjson2.JSONException;
910
import com.alibaba.fastjson2.JSONPath;
1011

1112
import modelengine.fitframework.annotation.Component;
@@ -21,15 +22,25 @@
2122
*/
2223
@Component
2324
public class FastJsonValueHandler implements ValueFetcher, ValueSetter {
25+
private static final String $ = "$";
26+
2427
@Override
2528
public Object fetch(Object object, String propertyPath) {
2629
if (object == null) {
2730
return null;
2831
}
29-
if (StringUtils.isBlank(propertyPath)) {
32+
if (isObjectSelf(propertyPath)) {
3033
return object;
31-
} else {
34+
}
35+
if (object instanceof String) {
36+
return null;
37+
}
38+
try {
3239
return JSONPath.eval(object, this.getParsedPath(propertyPath));
40+
} catch (JSONException e) {
41+
throw new IllegalArgumentException(StringUtils.format(
42+
"Failed to fetch value by JSONPath. [propertyPath={0}]",
43+
propertyPath), e);
3344
}
3445
}
3546

@@ -38,20 +49,32 @@ public Object set(Object object, String propertyPath, Object value) {
3849
if (object == null) {
3950
return null;
4051
}
41-
if (StringUtils.isBlank(propertyPath)) {
52+
if (isObjectSelf(propertyPath)) {
4253
return value;
4354
}
44-
JSONPath.set(object, this.getParsedPath(propertyPath), value);
45-
return object;
55+
if (object instanceof String) {
56+
return object;
57+
}
58+
try {
59+
JSONPath.set(object, this.getParsedPath(propertyPath), value);
60+
return object;
61+
} catch (JSONException e) {
62+
throw new IllegalArgumentException(StringUtils.format("Failed to set value by JSONPath. [propertyPath={0}]",
63+
propertyPath), e);
64+
}
65+
}
66+
67+
private static boolean isObjectSelf(String propertyPath) {
68+
return StringUtils.isBlank(propertyPath) || $.equals(propertyPath);
4669
}
4770

4871
private String getParsedPath(String propertyPath) {
49-
if (propertyPath.startsWith("$")) {
72+
if (propertyPath.startsWith($)) {
5073
return propertyPath;
5174
} else if (propertyPath.startsWith("[")) {
52-
return "$" + propertyPath;
75+
return $ + propertyPath;
5376
} else {
54-
return "$." + propertyPath;
77+
return $ + "." + propertyPath;
5578
}
5679
}
5780
}

framework/fit/java/fit-builtin/plugins/fit-value-fastjson/src/test/java/modelengine/fit/value/fastjson/FastJsonValueFetcherTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fit.value.fastjson;
88

@@ -79,6 +79,29 @@ void shouldReturnNullGivenPropertyPathIsNotEmpty() {
7979
}
8080
}
8181

82+
@Nested
83+
@DisplayName("当 object 为 JSON 字符串时")
84+
class GivenObjectIsJsonString {
85+
private Object object;
86+
87+
@BeforeEach
88+
void setup() {
89+
this.object = "{\"k1\":{\"k2\":\"v\"}}";
90+
}
91+
92+
@AfterEach
93+
void teardown() {
94+
this.object = null;
95+
}
96+
97+
@Test
98+
@DisplayName("当 propertyPath 不为空时,不自动解析 JSON 字符串")
99+
void shouldReturnNullGivenPropertyPathIsNotEmpty() {
100+
Object actual = FastJsonValueFetcherTest.this.fetcher.fetch(this.object, "k1.k2");
101+
assertThat(actual).isNull();
102+
}
103+
}
104+
82105
@Nested
83106
@DisplayName("当 object 为键值对时")
84107
class GivenObjectIsKeyValuePair {

framework/fit/java/fit-builtin/plugins/fit-value-fastjson/src/test/java/modelengine/fit/value/fastjson/FastJsonValueSetterTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fit.value.fastjson;
88

@@ -71,6 +71,29 @@ void shouldReturnReplacedValueGivenPropertyPathIsEmpty() {
7171
}
7272
}
7373

74+
@Nested
75+
@DisplayName("当 object 为 JSON 字符串时")
76+
class GivenObjectIsJsonString {
77+
private Object object;
78+
79+
@BeforeEach
80+
void setup() {
81+
this.object = "{\"k1\":{\"k2\":\"v\"}}";
82+
}
83+
84+
@AfterEach
85+
void teardown() {
86+
this.object = null;
87+
}
88+
89+
@Test
90+
@DisplayName("当 propertyPath 不为空时,不自动解析 JSON 字符串")
91+
void shouldReturnOriginalStringGivenPropertyPathIsNotEmpty() {
92+
Object actual = FastJsonValueSetterTest.this.setter.set(this.object, "k1.k2", "v1");
93+
assertThat(actual).isEqualTo(this.object);
94+
}
95+
}
96+
7497
@Nested
7598
@DisplayName("当 object 为键值对时")
7699
class GivenObjectIsKeyValuePair {

0 commit comments

Comments
 (0)