diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java b/core/src/main/java/com/alibaba/fastjson2/JSONPathSegmentName.java index 49de0e29a..a0ad4eb78 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 000000000..1b3239377 --- /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"); + } +}