From 62a67f2602c515c27ef2b9e0f4fbec5b2e572623 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 2 Nov 2024 16:51:22 +0800 Subject: [PATCH] bug fix for jsonpath set, for issue #3125 --- .../fastjson2/JSONPathSegmentName.java | 2 +- .../fastjson2/issues_3100/Issue3125.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3125.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java b/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java index 49de0e29a8..a0ad4eb78f 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java @@ -378,7 +378,7 @@ public void set(JSONPath.Context context, Object value) { ObjectReader objectReader = provider.getObjectReader(item.getClass()); FieldReader fieldReader = objectReader.getFieldReader(nameHashCode); if (fieldReader != null) { - fieldReader.accept(item, null); + fieldReader.accept(item, value); } } return; diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3125.java b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3125.java new file mode 100644 index 0000000000..1b32393773 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_3100/Issue3125.java @@ -0,0 +1,50 @@ +package com.alibaba.fastjson2.issues_3100; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONPath; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue3125 { + @Getter + @Setter + static class User { + private String name; + private List additionalInfo; + } + + @Getter + @Setter + @ToString + static class NVString { + String name; + String value; + } + + public static final String str = "{\"name\":\"fastjson2\",\"additionalInfo\":[{\"name\":\"srv6-color\",\"value\":\"test\"},{\"name\":\"test2\",\"value\":\"3\"}]}"; + + public static String path = "$.additionalInfo[?(@.name=='srv6-color')].value"; + + @Test + public void test_fastjson2_case1() { + User user = JSON.parseObject(str, User.class); + JSONPath jsonPath = JSONPath.of("$.additionalInfo[?(@.name=='srv6-color')].value"); + jsonPath.set(user, "modify"); + assertEquals(user.getAdditionalInfo().get(0).getValue(), "modify"); + } + + @Test + public void test_fastjson_case1() { + User user = JSON.parseObject(str, User.class); + com.alibaba.fastjson.JSONPath.set(user, "$.additionalInfo[?(@.name=='srv6-color')].value", "modify"); + JSONPath jsonPath = JSONPath.of("$.additionalInfo[?(@.name=='srv6-color')].value"); + jsonPath.set(user, "modify"); + assertEquals(user.getAdditionalInfo().get(0).getValue(), "modify"); + } +}