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

枚举只有2个元素时,无法反序列化[BUG] #1106

Closed
ArrenQ opened this issue Feb 10, 2023 · 2 comments
Closed

枚举只有2个元素时,无法反序列化[BUG] #1106

ArrenQ opened this issue Feb 10, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@ArrenQ
Copy link

ArrenQ commented Feb 10, 2023

问题描述

当枚举只有2个元素时,反序列化会报 cast 错误

环境信息

请填写以下信息:

  • OS信息: [e.g.:Windows10 19043.1237 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 17]
  • 版本信息:[e.g.:Fastjson2 2.0.22]

重现步骤

如何操作可以重现该问题:

  1. 使用 JSONObject.parseObject(str).toJavaObject(clazz, new JavaReader.Future[0]) 内部调用 TypeUtils.cast 方法

假设枚举定义如下

public enum UserStatus {
  NORMAL(0),  LOCKED(1);
 
  private final byte code;

  UserStatus(int code) { this.code = (byte) code; }
  public byte getCode()  { return this.code; }
}

public class User {
   private UserStatus state;
   .......
}

反序列化时 TypeUtils.cast 方法 1263 行代码如下

if (targetClass.isEnum()) {
    if (obj instanceof Integer) {
        int intValue = ((Integer) obj).intValue();
        ObjectReader objectReader = JSONFactory.getDefaultObjectReaderProvider().getObjectReader(targetClass);
       // TODO 当枚举只有2个元素时, objectReader 的类型是 ObjectReaderImplEnum2X4,且这个类型没有继承ObjectReaderImplEnum,导致这里判断失败。代码直接往下走,最终直接抛出 can not cast to xxxxxx from xxxxxx
        if (objectReader instanceof ObjectReaderImplEnum) {
            Enum e = ((ObjectReaderImplEnum) objectReader).of(intValue);
            return (T) e;
        }
    }
}

if (obj instanceof Collection) {
    ObjectReader objectReader = provider.getObjectReader(targetClass);
    return (T) objectReader.createInstance((Collection) obj);
}

throw new JSONException("can not cast to " + targetClass.getName() + ", from " + obj.getClass());

期待的正确结果

枚举2个元素时能正确反序列化

相关日志输出

com.alibaba.fastjson2.JSONException: can not cast to com.xxxxx.enums.UserStataus, from class java.lang.Integer

附带说明

目前工作环境是内外网分离,只能使用虚拟浏览器。因此,没办法贴图和赋值全部日志,一上内容都是手打,还请见谅,也请务必帮忙看看这个问题。非常感谢。

@ArrenQ ArrenQ added the bug Something isn't working label Feb 10, 2023
wenshao added a commit that referenced this issue Feb 10, 2023
@wenshao
Copy link
Member

wenshao commented Feb 10, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.24-SNAPSHOT/
问题已修复,请用2.0.24-SNAPSHOT版本帮忙验证,2.0.24-SNAPSHOT预计在2月中旬发布。

@wenshao wenshao added this to the 2.0.24 milestone Feb 10, 2023
@wenshao wenshao added the fixed label Feb 10, 2023
@wenshao
Copy link
Member

wenshao commented Feb 18, 2023

@wenshao wenshao closed this as completed Feb 18, 2023
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