apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.51k stars 26.43k forks source link

Dubbo 3.2.0 fastjson2 generic type deserialize failed #12257

Closed CodePlayer closed 10 months ago

CodePlayer commented 1 year ago

Environment

Steps to reproduce this issue

我们的应用中存在如下代码: Consider the following code:

public interface Person extends Serializable {

  String getName();

}

@Getter
@Setter
public class Student implements Person {

  String name;

}

@Getter
@Setter
public class Teacher implements Person {

  String name;

}

public interface RemoteService {

  /** return an instance of Student or Teacher */
  Person findById(Long id);

}

我们想要通过 Dubbo 远程加载一个 Person 实例对象( 注意 Person 只是一个接口,它有两个实现类:StudentTeacher)。 当使用 tri + hessian2 时,一切正常。当使用 tri + fastjson2 时,应用抛出异常。

We want to remotely load a Person instance object through Dubbo (note that Person is just an interface, and it has two implementation classes: Student and Teacher). When using tri + hessian2, it works. When using tri + fastjson2, the application throws the following exception.

Expected Behavior

使用 tri + fastjson2 时,如果返回值声明为 父类/接口,都应该能够正确返回。

When using tri + fastjson2, if the return value is declared as a parent class/interface, it should be able to return correctly.

Actual Behavior

org.apache.dubbo.rpc.StatusRpcException: INTERNAL : Deserialize response failed
    at org.apache.dubbo.rpc.TriRpcStatus.asException(TriRpcStatus.java:214) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.call.UnaryClientCallListener.onClose(UnaryClientCallListener.java:51) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.call.TripleClientCall.onMessage(TripleClientCall.java:86) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.stream.TripleClientStream$ClientTransportListener$1.onRawMessage(TripleClientStream.java:316) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.frame.TriDecoder.processBody(TriDecoder.java:140) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.frame.TriDecoder.deliver(TriDecoder.java:87) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.frame.TriDecoder.deframe(TriDecoder.java:57) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.stream.TripleClientStream$ClientTransportListener.lambda$onData$2(TripleClientStream.java:468) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.common.threadpool.serial.SerializingExecutor.run(SerializingExecutor.java:102) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.common.threadpool.ThreadlessExecutor$RunnableWrapper.run(ThreadlessExecutor.java:141) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.common.threadpool.ThreadlessExecutor.waitAndDrain(ThreadlessExecutor.java:70) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.AsyncRpcResult.get(AsyncRpcResult.java:202) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.AbstractInvoker.waitForResultIfSync(AbstractInvoker.java:286) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:189) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:71) ~[dubbo-3.2.0.jar!/:3.2.0]
    at cn.dengta.context.rpc.DubboSessionFilter.invoke(DubboSessionFilter.java:61) ~[base-1.0.0.jar!/:?]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.metrics.filter.MetricsFilter.invoke(MetricsFilter.java:56) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CallbackRegistrationInvoker.invoke(FilterChainBuilder.java:194) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.ReferenceCountInvokerWrapper.invoke(ReferenceCountInvokerWrapper.java:78) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker.invokeWithContext(AbstractClusterInvoker.java:380) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:81) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:341) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.router.RouterSnapshotFilter.invoke(RouterSnapshotFilter.java:46) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:101) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.support.MetricsClusterFilter.invoke(MetricsClusterFilter.java:51) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:52) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.spring.security.filter.ContextHolderParametersSelectedTransferFilter.invoke(ContextHolderParametersSelectedTransferFilter.java:41) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.support.ConsumerClassLoaderFilter.invoke(ConsumerClassLoaderFilter.java:40) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.support.ConsumerContextFilter.invoke(ConsumerContextFilter.java:118) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:331) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CallbackRegistrationInvoker.invoke(FilterChainBuilder.java:194) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.wrapper.AbstractCluster$ClusterFilterInvoker.invoke(AbstractCluster.java:91) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:103) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.cluster.support.wrapper.ScopeClusterInvoker.invoke(ScopeClusterInvoker.java:131) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.registry.client.migration.MigrationInvoker.invoke(MigrationInvoker.java:284) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.proxy.InvocationUtil.invoke(InvocationUtil.java:57) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:75) ~[dubbo-3.2.0.jar!/:3.2.0]
    at cn.dengta.context.auth.AutoLoginHandler$AutoLoginSupportDubboProxy2.load(AutoLoginHandler$AutoLoginSupportDubboProxy2.java) ~[base-1.0.0.jar!/:?]
    at jdk.internal.reflect.GeneratedMethodAccessor476.invoke(Unknown Source) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.27.jar!/:5.3.27]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-5.3.27.jar!/:5.3.27]
    at jdk.proxy2.$Proxy247.findById(Unknown Source) ~[?:?]
    ......
    at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:148) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:529) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.27.jar!/:5.3.27]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:623) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:209) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at cn.dengta.context.web.GlobalLogFilter.doFilter(GlobalLogFilter.java:33) ~[base-1.0.0.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at cn.dengta.common.context.RequestContextFilter.doFilter(RequestContextFilter.java:23) ~[common-context-1.0.0.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:142) ~[spring-session-core-2.5.6.jar!/:2.5.6]
    at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:82) ~[spring-session-core-2.5.6.jar!/:2.5.6]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96) ~[spring-boot-actuator-2.6.14.jar!/:2.6.14]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.27.jar!/:5.3.27]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.27.jar!/:5.3.27]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.27.jar!/:5.3.27]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:178) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:153) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:481) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:130) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:768) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:389) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1791) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.74.jar!/:?]
    at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.NullPointerException
    at com.alibaba.fastjson2.TypeReference.<init>(TypeReference.java:58) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.TypeReference.<init>(TypeReference.java:34) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.TypeReference$1.<init>(TypeReference.java:229) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.TypeReference.get(TypeReference.java:229) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.util.BeanUtils.getFieldType(BeanUtils.java:1634) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReaderMethod(ObjectReaderCreator.java:2200) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReader(ObjectReaderCreator.java:1460) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreator.lambda$createFieldReaders$5(ObjectReaderCreator.java:1568) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.util.BeanUtils.setters(BeanUtils.java:577) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreator.createFieldReaders(ObjectReaderCreator.java:1564) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreator.createObjectReader(ObjectReaderCreator.java:951) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderCreatorASM.createObjectReader(ObjectReaderCreatorASM.java:258) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.reader.ObjectReaderProvider.getObjectReader(ObjectReaderProvider.java:992) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at com.alibaba.fastjson2.JSONB.parseObject(JSONB.java:546) ~[fastjson2-2.0.30-SNAPSHOT.jar!/:?]
    at org.apache.dubbo.common.serialize.fastjson2.FastJson2ObjectInput.readObject(FastJson2ObjectInput.java:118) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.common.serialize.DefaultMultipleSerialization.deserialize(DefaultMultipleSerialization.java:42) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.ReflectionPackableMethod$WrapResponseUnpack.unpack(ReflectionPackableMethod.java:385) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.model.PackableMethod.parseResponse(PackableMethod.java:72) ~[dubbo-3.2.0.jar!/:3.2.0]
    at org.apache.dubbo.rpc.protocol.tri.call.TripleClientCall.onMessage(TripleClientCall.java:80) ~[dubbo-3.2.0.jar!/:3.2.0]

FastJson2ObjectInput#readObject() 方法在调用 JSONB.parseObject( ) 时都应该额外传入 Feature: JSONReader.Feature.SupportAutoType 即可解决此问题。

oooopl commented 1 year ago

12254 你看看这个,也是泛化调用出的问题

AlbumenJ commented 1 year ago

https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/upgrades-and-compatibility/3.1-to-3.2-compatibility-guide/#1-%E5%BA%8F%E5%88%97%E5%8C%96%E6%A3%80%E6%9F%A5%E6%A8%A1%E5%BC%8F%E9%87%8D%E8%A6%81

看一下这个文档加下配置呢

CodePlayer commented 1 year ago

12254 你看看这个,也是泛化调用出的问题

你升级到 fastjson 2.0.32-SNAPSHOT 试试。我这边在升级后,貌似没有报错了。