dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

provider泛型类型为Float时返回Double类型

Open natsumm opened this issue 3 years ago • 6 comments

  • [X] I have searched the issues of this repository and believe that this is not a duplicate.

Environment

  • Dubbo version: 3.0.7
  • Operating System version: Linux Ubuntu-20.04
  • Java version: 1.8

Steps to reproduce this issue

  1. provider返回泛型为Float
  2. consumer以Float接收返回参数

Demo地址: https://github.com/natsumm/dubbo-generic-float-issue

Expected Behavior

返回Float类型参数

Actual Behavior

返回了Double类型参数,并抛出java.lang.ClassCastException

Detail

  1. provider端代码
@DubboService
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }

    //此处定义为Float类型
    @Override
    public List<Float> getFloats() {
        List<Float> floats = new ArrayList<>();
        floats.add(35.3F);
        floats.add(45.1F);
        return floats;
    }
}
  1. consumer端代码
@SpringBootApplication
@Service
@EnableDubbo
public class ConsumerApplication {

    @DubboReference
    private DemoService demoService;

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
        ConsumerApplication application = context.getBean(ConsumerApplication.class);
        String result = application.doSayHello("world");
        System.out.println("result: " + result);

        //此处返回了Double类型
        List<Float> floats = application.getFloats();
        System.out.println("floats = " + floats);
        Float aFloat = floats.get(0);
    }

    public String doSayHello(String name) {
        return demoService.sayHello(name);
    }

    public List<Float> getFloats() {
        return demoService.getFloats();
    }
}
  1. 预期返回Float类型实际Double image

natsumm avatar Feb 02 '23 02:02 natsumm

经验证,dubbo 3.0.0~3.1.5均存在此问题

natsumm avatar Feb 02 '23 02:02 natsumm

Yes, the problem is caused by serialization of float by hessian.

mytang0 avatar Feb 02 '23 07:02 mytang0

However, when I used other serialization protocols(like kryo and sft), it still returned Double type except nativejava.

natsumm avatar Feb 02 '23 07:02 natsumm

However, when I used other serialization protocols(like kryo and sft), it still returned Double type except nativejava.

Thank you for your feedback. Let's wait for the fix.

mytang0 avatar Feb 02 '23 08:02 mytang0

Thank you for your feedback. Let's wait for the fix.

Ok, thank you for the reply.

natsumm avatar Feb 02 '23 09:02 natsumm

I try to use fastjson2, kryo and sft that it return float. I think the problem is hessian serialization. org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcResult#handleValue http://hessian.caucho.com/doc/hessian-serialization.html

dubbo version: 3.1.5

        <dependency>
            <groupId>org.apache.dubbo.extensions</groupId>
            <artifactId>dubbo-serialization-kryo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo.extensions</groupId>
            <artifactId>dubbo-serialization-fst</artifactId>
            <version>1.0.0</version>
        </dependency>

KamToHung avatar Feb 02 '23 14:02 KamToHung

我目前是这么处理的 dubbo.version = 3.1.5

1 application.yml

dubbo:
  protocols:
    dubbo:
      name: dubbo
      serialization: fst

2 pom.xml

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-serialization-fst</artifactId>
</dependency>

natsumm avatar Apr 19 '23 02:04 natsumm

我目前是这么处理的 dubbo.version = 3.1.5

1 application.yml

dubbo:
  protocols:
    dubbo:
      name: dubbo
      serialization: fst

2 pom.xml

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-serialization-fst</artifactId>
</dependency>

使用fst插件的版本为 2.7.22

natsumm avatar Apr 19 '23 02:04 natsumm