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 导致反序列化结构错误 #2367

Closed
He-Pin opened this issue Mar 25, 2024 · 5 comments
Closed

[BUG] autoTypeFilter 导致反序列化结构错误 #2367

He-Pin opened this issue Mar 25, 2024 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@He-Pin
Copy link

He-Pin commented Mar 25, 2024

问题描述

简要描述您碰到的问题。

反序列化出来的结构是错误的。

环境信息

请填写以下信息:

  • OS信息: Windows + Linux
  • JDK信息: Java 21
  • 版本信息:Fastjson2 2.0.48

重现步骤

package com.alibaba.fastjson2.issues_2300;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;



public class Issue230x {

    @Data
    public static final class AttributeKey<T extends Serializable> implements Serializable {
        private static final long serialVersionUID = 1L;
        private final String name;

        public AttributeKey(final String name) {
            this.name = name;
        }
    }

    @Data
    @Slf4j
    public static final class PubSubAttributes implements Serializable {
        private static final long serialVersionUID = 1L;
        private final Map<AttributeKey<?>, Serializable> attributes;

        public PubSubAttributes() {
            this(new ConcurrentHashMap<>());
        }

        PubSubAttributes(Map<AttributeKey<?>, Serializable> attributes) {
            this.attributes = attributes;
        }

        public <T extends Serializable> T setAttribute(
                final @NonNull AttributeKey<T> attributeKey,
                final @NonNull T value) {
            return (T) attributes.put(attributeKey, value);
        }

        public <T extends Serializable> T getAttribute(final @NonNull AttributeKey<T> attributeKey) {
            return (T) attributes.get(attributeKey);
        }
    }

    @Data
    public static final class Subscription {
        private PubSubAttributes attributes;
    }

    @Test
    @SuppressWarnings("deprecated")
    public void testWriteClassName() {
        final Filter filter = JSONReader.autoTypeFilter(
                true,
                "com.alibaba.fastjson2.", "java.");

        final PubSubAttributes attributes = new PubSubAttributes();
        final AttributeKey<String> APP_KEY = new AttributeKey<>("appKey");
        attributes.setAttribute(APP_KEY, "123456");
        //
        final Subscription subscription = new Subscription();
        subscription.setAttributes(attributes);

        final String jsonStr = JSON.toJSONString(subscription, JSONWriter.Feature.WriteClassName);
        //works
        //final Subscription parsedSubscription = JSON.parseObject(jsonStr, Subscription.class, SupportAutoType);
        //does not work
        final Subscription parsedSubscription = JSON.parseObject(jsonStr, Subscription.class, filter);
        Assertions.assertEquals("123456", parsedSubscription.getAttributes().getAttribute(APP_KEY));
    }
}

期待的正确结果

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

单元测试通过

相关日志输出

org.opentest4j.AssertionFailedError: expected: <123456> but was: <null>
预期:123456
实际:null

附加信息

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

image

@He-Pin He-Pin added the bug Something isn't working label Mar 25, 2024
@rowstop
Copy link
Contributor

rowstop commented Mar 26, 2024

AttributeKey这个类需要交给autoTypeFilter

@rowstop
Copy link
Contributor

rowstop commented Mar 26, 2024

另外就是autoTypeFilter不是需要全类名吗 ,我没有像你这么用过 不确定

@He-Pin
Copy link
Author

He-Pin commented Mar 26, 2024

全名也不行的,我把几个类都全名了也不行。

@wenshao wenshao added this to the 2.0.49 milestone Mar 30, 2024
@wenshao
Copy link
Member

wenshao commented Mar 30, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.49-SNAPSHOT/
问题已经修复,请帮忙用2.0.49-SNAPSHOT版本验证,2.0.49版本预计在4月底发布

@wenshao
Copy link
Member

wenshao commented Apr 14, 2024

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

@wenshao wenshao closed this as completed Apr 14, 2024
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

3 participants