alibaba / alibaba/fastjson2

[BUG]在使用 Dubbo 使用 Fastjson2 做序列化时,遇到字段为枚举类对象的无法完成序列化

Open
#1,343 12 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*

类:
```java
public UserDubboService {
LoginAccountVo getAccountById(String id);
}

@Data
public LoginAccountVo {
private String id;
private LoginType type;
}

@Data
public enum LoginType implements DictEnum, IEnum, Serializable{
/**
* 用户名
*/
USERNAME(1, "用户名", true),
/**
* 手机号
*/
PHONE(2, "手机号", true),
/**
* 电子邮件
*/
EMAIL(3, "电子邮件", true)
;
private final Integer value;
private final String title;
// 是否是本地账号
private final boolean local;

@JsonCreator
public static LoginType create(Integer value) {
LoginType item = null;
// .....
return item;
}
}

```

错误位置:`com.alibaba.fastjson2.writer.FieldWriter#writeEnumJSONB`

异常信息
```
org.apache.dubbo.rpc.RpcException: Failed to invoke the method getLocalIdentifier in the service com.houkunlin.rpc.UserDubboService. Tried 3 times of the providers [172.29.237.2:20886] (1/1) from the registry 192.168.0.5:8848 on the consumer 172.29.237.2 using the dubbo version 3.2.0-beta.6. Last error is: Failed to invoke remote method: getLocalIdentifier, provider: DefaultServiceInstance{serviceName='system-user-server', host='172.29.237.2', port=20886, enabled=true, healthy=true, metadata={dubbo.metadata-service.url-params={"prefer.serialization":"fastjson2,hessian2","version":"1.0.0","dubbo":"2.0.2","release":"3.2.0-beta.6","side":"provider","port":"20886","protocol":"dubbo"}, dubbo.endpoints=[{"port":20886,"protocol":"dubbo"}], dubbo.metadata.revision=8d596daa6565c6bfebb097f1bf926a68, dubbo.metadata.storage-type=local, timestamp=1681116858869}}, service{name='com.houkunlin.cloud.micro.rpc.UserDubboService',group='null',version='null',protocol='dubbo',port='20886',params={executor-management-mode=default, side=provider, file-cache=false, release=3.2.0-beta.6, methods=getAssignAuthorities,getLeaderIds,getLocalIdentifier,getUserAllPermissionValues,getUserByUserId,getUserByUsername,loginFailureAction,loginSuccessAction,saveUserLog,updatePassword, deprecated=false, dubbo=2.0.2, interface=com.houkunlin.rpc.UserDubboService, service-name-mapping=true, register-mode=instance, generic=false, revision=0.0.7-SNAPSHOT-plain, application=system-user-server, prefer.serialization=fastjson2,hessian2, background=false, dynamic=true, anyhost=true},}, cause: org.apache.dubbo.remoting.RemotingException: Failed to send response: Response [id=136, version=2.0.2, status=20, event=false, error=null, result=AppResponse [value=LoginAccountVo(id=1, userId=1, identifier=admin, identifierDisplay=admin, credential=$2a$10$HUM4HAoPIPPKfe.ldNaue.cmuT3HOI9ocaBXzw1Qj/NhFg7IkHSQu, type=USERNAME, local=true, lastLoginIp=127.0.0.1, lastLoginTime=2023-03-27T17:01:48.941657), exception=null]], cause: java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
at com.alibaba.fastjson2.writer.FieldWriter.writeEnumJSONB(FieldWriter.java:156)
at com.alibaba.fastjson2.writer.OWG_9_15_LoginAccountVo.writeJSONB(Unknown Source)
at com.alibaba.fastjson2.JSONB.toBytes(JSONB.java:1136)
at org.apache.dubbo.common.serialize.fastjson2.FastJson2ObjectOutput.writeObject(FastJson2ObjectOutput.java:106)
at org.apache.dubbo.rpc.protocol.dubbo.DubboCodec.encodeResponseData(DubboCodec.java:251)
at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.encodeResponse(ExchangeCodec.java:317)
at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.encode(ExchangeCodec.java:78)
at org.apache.dubbo.rpc.protocol.dubbo.DubboCountCodec.encode(DubboCountCodec.java:51)
at org.apache.dubbo.remoting.transport.netty4.NettyChannel.send(NettyChannel.java:192)
```

### 环境信息
*请填写以下信息:*

- OS信息: Win 11
- JDK信息: Java 17
- 版本信息:
- `org.apache.dubbo.extensions:dubbo-serialization-fastjson:1.0.1`
- `org.apache.dubbo:dubbo-bom:3.2.0-beta.6`
- `com.alibaba.fastjson2:fastjson2:2.0.23` / `spring boot 3.0.5`
- `org.springframework.cloud:spring-cloud-dependencies:2022.0.1`
- `com.alibaba.cloud:spring-cloud-alibaba-dependencies:2022.0.0.0-RC1`

![image](https://user-images.githubusercontent.com/12333292/230880972-6f65b270-aacf-4931-ba33-a481402d1bf5.png)

### 重现步骤
*如何操作可以重现该问题:*

1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
```java
//可在此输入示例代码
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*

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

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

Contributor guide

Open the contributing guide

Research direction

Start at com.alibaba.fastjson2.writer.FieldWriter#writeEnumJSONB and review the supplied LoginAccountVo/LoginType example with fastjson2 2.0.23 and Dubbo 3.2.0-beta.6. Reproduce the response serialization failure and verify that an enum-valued field can be serialized successfully without the UnsupportedOperationException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.