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

[QUESTION]使用Mybatis-plus的泛型枚举IEnum遇到的问题 #2682

Closed
cyij2006 opened this issue Jun 9, 2024 · 1 comment
Closed

[QUESTION]使用Mybatis-plus的泛型枚举IEnum遇到的问题 #2682

cyij2006 opened this issue Jun 9, 2024 · 1 comment
Labels
fixed question Further information is requested
Milestone

Comments

@cyij2006
Copy link

cyij2006 commented Jun 9, 2024

请描述您的问题

在实现IEnum接口的自定义枚举类中定义静态方法,导致json解析异常。

环境信息

  • OS :macOS 13.6.4
  • JDK:17.0.10
  • spring-boot:3.2.6
  • mybatis-plus:3.5.6
  • fastjson2、fastjson2-extension、fastjson2-extension-spring6:2.0.51

枚举类:

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.baomidou.mybatisplus.annotation.IEnum;

public enum BizType implements IEnum<String> {

    COMMON("common", "通用");

    BizType(String value, String name) {
        this.value = value;
        this.name = name;
    }

    @EnumValue
    private final String value;
    private final String name;

    @JSONField(value = true)
    public String getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public static void test123() {}
}

POJO:

import com.demo.test.entity.type.BizType;
import lombok.Data;

@Data
public class VM {

    private BizType bizType;
}

调用:

String str = "{\"bizType\":\"common\"}"; 

VM vm = JSON.parseObject(str, VM.class);

报错:
WechatIMG2992

报错时的调试信息:
WechatIMG2991

BizType.getValue()方法的返回类型被解析为java.io.Serializable,导致字段解析时,当成intValue在解析

尝试的修正方法

  • 将BizType.test123这个静态方法删除,或者将静态方法名改成test,可以正常解析
  • 去除对IEnum接口的实现,无论有无静态方法,可以正常解析

正常解析时的调试信息
WechatIMG2993

项目压缩包
test.zip

补充信息
实际项目环境是,fastjson2-2.0.43 + jdk17 + mybatis-plus-3.5.4.1
半年前把jdk从8升级到17,当时测试没有问题,之后没有再运行过项目
本周再启动环境,出现上述报错
升级到新的版本fastjson2-2.0.51和mybatis-plus-3.5.6后,还是会出现上述报错

不确定是否由mybatis-plus引起,
调试时,由于com.alibaba.fastjson2.reader.ORG_1_35_VM.readObject(Unknown Source)无法跟进,问题定位不太方便

请项目维护人员帮忙看看,感谢感谢!

@cyij2006 cyij2006 added the question Further information is requested label Jun 9, 2024
@wenshao wenshao added this to the 2.0.52 milestone Jun 10, 2024
@wenshao wenshao added the fixed label Jun 10, 2024
@wenshao
Copy link
Member

wenshao commented Jul 14, 2024

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

@wenshao wenshao closed this as completed Jul 14, 2024
wenshao pushed a commit that referenced this issue Jul 23, 2024
* fix BeanUtils#getEnumValueField, for issue #2682

* fix #2821

* fix #2821
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants