Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]fastjson2不支持直接对对象进行jpath设置List中的对象 #3125

Closed
majh1 opened this issue Oct 22, 2024 · 2 comments
Closed

[BUG]fastjson2不支持直接对对象进行jpath设置List中的对象 #3125

majh1 opened this issue Oct 22, 2024 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@majh1
Copy link

majh1 commented Oct 22, 2024

测试用例如下

@Getter
    @Setter
    static class User{
        private String name;
        private List<NVString> additionalInfo;
    }
    @Getter
    @Setter
    @ToString
    static class NVString{
        String name;
        String value;
    }

        public static final String JSON = "{\"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 = JsonUtil.fromJson(JSON,User.class);
        JSONPath jsonPath = JSONPath.of("$.additionalInfo[?(@.name=='srv6-color')].value");
        jsonPath.set(user, "modify");
        Assert.assertEquals(user.getAdditionalInfo().get(0).getValue(),"modify");
    }


    @Test
    public void test_fastjson_case1(){
        User user = JsonUtil.fromJson(JSON,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");
        Assert.assertEquals(user.getAdditionalInfo().get(0).getValue(),"modify");
    }

对比fastjson和fastjson2、不支持直接对对象进行设置、会将List中的对象直接设置为null
想请教下: 是故意这么设计不支持List中对象的jpath set对象、还是bug

看源码:再JSONPathSegmentName中的

public void set(JSONPath.Context context, Object value) {
        Object object = context.parent == null
                ? context.root
                : context.parent.value;

        if (object instanceof Map) {
            Map map = (Map) object;
            Object origin = map.put(name, value);
            if (origin != null) {
                if ((context.readerFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
                    if (origin instanceof Collection) {
                        ((Collection) origin).add(value);
                        map.put(name, value);
                    } else {
                        JSONArray array = JSONArray.of(origin, value);
                        map.put(name, array);
                    }
                }
            }
            return;
        }

        if (object instanceof Collection) {
            Collection collection = (Collection) object;
            for (Object item : collection) {
                if (item == null) {
                    continue;
                }

                if (item instanceof Map) {
                    Map map = (Map) item;
                    Object origin = map.put(name, value);
                    if (origin != null) {
                        if ((context.readerFeatures & JSONReader.Feature.DuplicateKeyValueAsArray.mask) != 0) {
                            if (origin instanceof Collection) {
                                ((Collection) origin).add(value);
                                map.put(name, value);
                            } else {
                                JSONArray array = JSONArray.of(origin, value);
                                map.put(name, array);
                            }
                        }
                    }
                    continue;
                }

                ObjectReaderProvider provider = context.path.getReaderContext().getProvider();
                ObjectReader objectReader = provider.getObjectReader(item.getClass());
                FieldReader fieldReader = objectReader.getFieldReader(nameHashCode);
                if (fieldReader != null) {
                    // **此处设置为了null**
                    fieldReader.accept(item, null);
                }
            }
            return;
        }

        ObjectReaderProvider provider = context.path.getReaderContext().getProvider();
        ObjectReader objectReader = provider.getObjectReader(object.getClass());
        FieldReader fieldReader = objectReader.getFieldReader(nameHashCode);
        if (fieldReader == null) {
            return;
        }

        if (value != null) {
            Class<?> valueClass = value.getClass();
            Class fieldClass = fieldReader.fieldClass;
            if (valueClass != fieldClass) {
                java.util.function.Function typeConvert = provider.getTypeConvert(valueClass, fieldClass);
                if (typeConvert != null) {
                    value = typeConvert.apply(value);
                }
            }
        }
        fieldReader.accept(object, value);
    }
@majh1 majh1 added the bug Something isn't working label Oct 22, 2024
@wenshao wenshao added this to the 2.0.54 milestone Nov 2, 2024
@wenshao
Copy link
Member

wenshao commented Nov 2, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.54-SNAPSHOT/
问题已修复,请帮忙用2.0.54-SNAPSHOT快照版本验证,2.0.54正式版本预计在月底发布

@wenshao wenshao added the fixed label Nov 2, 2024
@wenshao
Copy link
Member

wenshao commented Jan 12, 2025

https://github.com/alibaba/fastjson2/releases/tag/2.0.54
问题已修复,请用新版本

@wenshao wenshao closed this as completed Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants