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]java.util.Collections$UnmodifiableSet反序列化异常 #1509

Closed
cycle2zhou opened this issue May 27, 2023 · 1 comment
Closed

[BUG]java.util.Collections$UnmodifiableSet反序列化异常 #1509

cycle2zhou opened this issue May 27, 2023 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@cycle2zhou
Copy link

cycle2zhou commented May 27, 2023

问题描述

不可变集合反序列化异常

环境信息

请填写以下信息:

  • OS信息: MacBookPro18,2 32 GB
  • JDK信息: OpenJDK Runtime Environment (Zulu 8.70.0.23-CA-macos-aarch64) (build 1.8.0_372-b07)
  • 版本信息:2.0.31

重现步骤

模型:

@Data
@Schema(description = "登录用户信息")
public class LoginUser implements Serializable {
    private static final long serialVersionUID = 1462777779589266417L;

    @Schema(title = "登录名")
    private String name;

    @Schema(title = "真实名称")
    private String realName;

    @Schema(title = "所属组织ID")
    private Long orgId;

    @Schema(title = "用户权限角色列表")
    private Set<Long> authorities;

    /**
     * 设置权限角色列表
     *
     * @param authorities 权限角色集合
     */
    public void setAuthorities(Set<Long> authorities) {
        if (ObjectUtil.isEmpty(authorities)) {
            return;
        }
        this.authorities = Collections.unmodifiableSet(authorities);
    }
}

测试方法:

public static void main(String[] args) {
        JSON.mixIn(LoginUser.class, LoginUserMixin.class);
        LoginUser loginUser = new LoginUser();
        loginUser.setName("张三");
        loginUser.setRealName("zhangsan");
        loginUser.setAuthorities(CollUtil.newHashSet(1L, 2L));
        byte[] asd = JSONB.toBytes(loginUser, JSONWriter.Feature.WriteClassName,
                                   JSONWriter.Feature.FieldBased,
                                   JSONWriter.Feature.ErrorOnNoneSerializable,
                                   JSONWriter.Feature.ReferenceDetection,
                                   JSONWriter.Feature.WriteNulls,
                                   JSONWriter.Feature.NotWriteDefaultValue,
                                   JSONWriter.Feature.NotWriteHashMapArrayListClassName,
                                   JSONWriter.Feature.WriteNameAsSymbol);
        ContextAutoTypeBeforeHandler contextAutoTypeBeforeHandler = new ContextAutoTypeBeforeHandler("java.util.Collections$UnmodifiableSet",
                                                                                                     "com.kt.data.base.models.app.LoginUser");
        LoginUser as = JSONB.parseObject(asd, LoginUser.class, contextAutoTypeBeforeHandler,
                                         JSONReader.Feature.UseDefaultConstructorAsPossible,
                                         JSONReader.Feature.ErrorOnNoneSerializable,
                                         JSONReader.Feature.IgnoreAutoTypeNotMatch,
                                         JSONReader.Feature.UseNativeObject,
                                         JSONReader.Feature.FieldBased);
        System.out.println("aaa");
    }

尝试过使用mixin为属性独立设置反序列化方法,无效
反序列化类

public class LoginUserFastJsonDeserializer implements ObjectReader<Set<Long>> {


    @Override
    public Set<Long> readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        if (jsonReader.isNull()) {
            return null;
        }
        if (jsonReader.nextIfNull()) {
            return null;
        }
        for (; ; ) {
            if (jsonReader.nextIfObjectEnd()) {
                break;
            }
            String field = jsonReader.readFieldName();
            if (ObjectUtil.isEmpty(field)) {
                continue;
            }


            String aa = jsonReader.readString();
            System.out.println("");

        }
        return null;
    }

    @Override
    public Set<Long> readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        return readObject(jsonReader, fieldType, fieldName, features);
    }
}

mixin类

public abstract class LoginUserMixin {
    @JSONField(deserializeUsing = LoginUserFastJsonDeserializer.class)
    private Set<Long> authorities;
}

目前是设置到字段上上反序列化无效,根本不进方法;设置到类上有效果但是没有反序列化对象的demo不知道该怎么做

期待的正确结果

正常序列化LoginUser对象中authorities属性

相关日志输出

image

附加信息

dubbo3.2.1开始使用fastjson2,重升级后开始出现该问题,测试方法摘自FastJson2Serialization

@wenshao
Copy link
Member

wenshao commented May 29, 2023

@wenshao wenshao closed this as completed May 29, 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
Projects
None yet
Development

No branches or pull requests

2 participants