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] 配置 autoTypeFilter 之后无法正常反序列化 String[] #749

Closed
AlbumenJ opened this issue Sep 11, 2022 · 1 comment
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@AlbumenJ
Copy link

问题描述

简要描述您碰到的问题。

  • 配置 autoTypeFilter 之后无法正常反序列化 String[]

环境信息

请填写以下信息:

  • OS信息: MacOS
  • JDK信息: JDK 11.0.9
  • 版本信息:Fastjson2 2.0.13

重现步骤

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

  1. 传入 ContextAutoTypeBeforeHandler 为简化后类名导致无法通过类名白名单校验
    public static void main(String[] args) {
        String s = "a.a.a.a";
        String[] split = s.split("\\.");
        byte[] bytes = JSONB.toBytes(split, JSONWriter.Feature.WriteClassName);
        Object o = JSONB.parseObject(bytes, Object.class, JSONReader.autoTypeFilter(String[].class.getName(), String.class.getName()));
        System.out.println(o);
        System.out.println(o instanceof String[]);
        System.out.println(Arrays.equals(split, (String[]) o));
    }
  1. 手动定制 ContextAutoTypeBeforeHandler 使用简化前的类名,可以通过校验,但是在 loadClass 的时候由于对数组处理的问题导致无法加载到 String[]
    public static void main(String[] args) {
        String s = "a.a.a.a";
        String[] split = s.split("\\.");
        byte[] bytes = JSONB.toBytes(split, JSONWriter.Feature.WriteClassName);
        Object o = JSONB.parseObject(bytes, Object.class,
            new ContextAutoTypeBeforeHandler(new String[]{String[].class.getName(), String.class.getName()}){
                @Override
                public Class<?> apply(String typeName, Class<?> expectClass, long features) {
                    Class<?> mappedClass = TypeUtils.getMapping(typeName);
                    if (mappedClass != null) {
                        typeName = mappedClass.getName();
                    }
                    return super.apply(typeName, expectClass, features);
                }
            });
        System.out.println(o);
        System.out.println(o instanceof String[]);
        System.out.println(Arrays.equals(split, (String[]) o));
    }

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

请复制并粘贴任何相关的日志输出。

  1. 白名单校验不通过
Exception in thread "main" com.alibaba.fastjson2.JSONException: autoType not support input [String
	at com.alibaba.fastjson2.JSONReaderJSONB.checkAutoType(JSONReaderJSONB.java:1052)
	at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:437)
	at org.apache.dubbo.common.serialize.fastjson2.Fastjson2SecurityManager.main(Fastjson2SecurityManager.java:121)
  1. 类加载出错
Exception in thread "main" com.alibaba.fastjson2.JSONException: load class error [Ljava.lang.String;
	at com.alibaba.fastjson2.util.TypeUtils.loadClass(TypeUtils.java:1165)
	at com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler.apply(ContextAutoTypeBeforeHandler.java:61)
	at org.apache.dubbo.common.serialize.fastjson2.Fastjson2SecurityManager$1.apply(Fastjson2SecurityManager.java:129)
	at com.alibaba.fastjson2.JSONReaderJSONB.checkAutoType(JSONReaderJSONB.java:1040)
	at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:437)
	at org.apache.dubbo.common.serialize.fastjson2.Fastjson2SecurityManager.main(Fastjson2SecurityManager.java:121)

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@AlbumenJ AlbumenJ added the bug Something isn't working label Sep 11, 2022
@wenshao wenshao added this to the 2.0.14 milestone Sep 11, 2022
@wenshao wenshao added the fixed label Sep 11, 2022
@wenshao
Copy link
Member

wenshao commented Sep 16, 2022

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

@wenshao wenshao closed this as completed Sep 16, 2022
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