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] Fastjson JSON.toJSON异常 #2527

Open
Cooper-Zhong opened this issue May 6, 2024 · 1 comment
Open

[BUG] Fastjson JSON.toJSON异常 #2527

Cooper-Zhong opened this issue May 6, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Cooper-Zhong
Copy link

问题描述

类似#2187,但代码使用maven依赖在mac上运行时,fastjson和fastjson2的结果不一样。在clone下来的源码中运行时,出现stackoverflow error。

环境信息

Environment of cloned source code:

  • OS信息: [Ubuntu 22.04.3 LTS (GNU/Linux 6.2.0-36-generic x86_64)]
  • JDK信息: [Openjdk 1.8.0_382]
  • 版本信息:[Fastjson 2.0.50-SNAPSHOT] (perhaps?)

On laptop:

  • OS信息: [MacOS 12.7.4 M1 Pro 16 GB]
  • JDK信息: [Openjdk 17.0.6]
  • 版本信息:[Fastjson 2.0.49]

重现步骤

import com.alibaba.fastjson2.*;
import com.google.common.collect.Lists;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class Issue2187Mutated_119 {

    @Test
    public void testMutated() throws Exception {
        BeanMutated bean = new BeanMutated();
        bean.value = bean;
        JSONObject json = (JSONObject) JSON.toJSON(bean);
        assertSame(json, json.get("value"));
    }

    @Test
    public void testMutatedfj() throws Exception {
        BeanMutated bean = new BeanMutated();
        bean.value = bean;
        com.alibaba.fastjson.JSONObject json = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSON.toJSON(bean);
        assertSame(json, json.get("value"));
    }

    public static class BeanMutated {
        public BeanMutated value;
    }

    @Test
    public void testMutated1() throws Exception {
        Bean1Mutated bean = new Bean1Mutated();
        bean.values = Arrays.asList(bean);
        JSONObject json = (JSONObject) JSON.toJSON(bean);
        assertSame(json, json.getJSONArray("values").get(0));
    }

    @Test
    public void testMutated1fj() throws Exception {
        Bean1Mutated bean = new Bean1Mutated();
        bean.values = Arrays.asList(bean);
        com.alibaba.fastjson.JSONObject json = (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSON.toJSON(bean);
        assertSame(json, json.getJSONArray("values").get(0));
    }

    public static class Bean1Mutated {
        public List<Bean1Mutated> values;
    }

    @Data
    public class DynFormDtoMutated {
        private String sysName;
        private String name;
        private boolean enableTab;
        private int labelColSize;
        private String title;
        private String hint;
        private List<DynFormComponentDtoMutated> componentList;
        private String notes;
    }

    @Data
    public class DynFormComponentDtoMutated {
        private String name;
        private String nameText;
        private String functionName;
        private String compTypeName;
        private JSONObject props; // 组件完整配置
        private int widthColSize;
        protected int widthPercentage; //宽度百分比
        private String tabName;
        private boolean enabled;
        private long uid;
        private long parentFieldId;

        private List<DynFormComponentDtoMutated> subComponentList; //子组件集合

        public List<DynFormComponentDtoMutated> getFlatComponentDtosMutated() {
            List<DynFormComponentDtoMutated> flatList = Lists.newArrayList();
            flatList.add(this);
            if (this.subComponentList!= null) {
                for (DynFormComponentDtoMutated tmpDto : this.subComponentList) {
                    tmpDto.setParentFieldId(this.uid);
                    flatList.addAll(tmpDto.getFlatComponentDtosMutated());
                }
            }
            return flatList;
        }
    }
}

期待的正确结果

能够成功解析

相关日志输出

  • On Mac:
    Expected :{"values":[null]}
    Actual :null
    at Issue2187Mutated_119.testMutated1fj(Issue2187Mutated_119.java:50)

    Expected :{"value":{"value":{"$ref":".."}}}
    Actual :{value=(this Map)}
    at Issue2187Mutated_119.testMutated1fj(Issue2187Mutated_119.java:50)

  • Run in cloned repository:
    java.lang.StackOverflowError
    at java.lang.reflect.Field.get(Field.java:393)
    at com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:572)
    at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValue(FieldSerializer.java:151)
    at com.alibaba.fastjson.serializer.JavaBeanSerializer.getFieldValuesMap(JavaBeanSerializer.java:799)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:1221)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:1168)

@Cooper-Zhong Cooper-Zhong added the bug Something isn't working label May 6, 2024
@Cooper-Zhong
Copy link
Author

fastjson的test在2.0.49能返回结果,在2.0.51-SNAPSHOT会报StackOverflowError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant