alibaba / alibaba/fastjson2

[BUG] com.alibaba:fastjson:2.0.62 lombok + null/isNull 字段 序列化与旧版本一致,但反序列化失败, 不兼容

Open
#7,683 5 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

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

```xml

com.alibaba
fastjson
1.2.83

```
升级到
```xml

com.alibaba
fastjson
2.0.62

```

com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer 反序列化预期不一致

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

- OS信息: [e.g.:macOS tahoe 26.3.1(a) M5 32 GB]
- JDK信息: [e.g.:Amazon Corretto 17.0.17]
- 版本信息:[e.g.:Fastjson2 2.0.62]

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

```java

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.Data;
import lombok.ToString;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicReference;

class FastjsonCompatibilityTest {

@Test
void parseError() {

String location = GenericFastJsonRedisSerializer.class.getProtectionDomain()
.getCodeSource()
.getLocation().getPath();

int lastSep = location.lastIndexOf('/');

String fastjsonVersion = location.substring(lastSep + 1);
System.out.println("Fastjson version: " + fastjsonVersion);

boolean isFastjson2 = fastjsonVersion.startsWith("fastjson-2");

doTest(LombokFieldNameNull.class, isFastjson2);
doTest(LombokFieldNameIsNull.class, isFastjson2);
doTest(LombokFieldNameNotNull.class, false);

doTest(FieldNameNullNoGet.class, false);
doTest(FieldNameIsNullNoGet.class, false);
doTest(FieldNameNotNullNoGet.class, false);

doTest(FieldNameNullGet.class, isFastjson2);
doTest(FieldNameNullSet.class, false);

}

void doTest(Class cls, boolean expectedExceptionOn2ButOkOn1) {
GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();

Object obj = null;
try {
obj = cls.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}

byte[] jsonBytes = genericFastJsonRedisSerializer.serialize(obj);
System.out.println("JSONSTR" + new String(jsonBytes));

AtomicReference deserializeObj = new AtomicReference<>();

if (expectedExceptionOn2ButOkOn1) {
Assertions.assertThrows(Exception.class, () -> {
deserializeObj.set(genericFastJsonRedisSerializer.deserialize(jsonBytes));
System.out.println("DESERIALIZED OBJ: " + deserializeObj.get());
});
} else {
deserializeObj.set(genericFastJsonRedisSerializer.deserialize(jsonBytes));
System.out.println("DESERIALIZED OBJ: " + deserializeObj.get());
org.assertj.core.api.Assertions.assertThat(deserializeObj.get())
.usingRecursiveComparison()
.isEqualTo(obj);
}

}
}

@Data
class LombokFieldNameNull {
boolean Null;
}

@Data
class LombokFieldNameIsNull {
boolean isNull;
}

@Data
class LombokFieldNameNotNull {
boolean nul;
}

@ToString
class FieldNameNullNoGet {
boolean Null;
}

@ToString
class FieldNameIsNullNoGet {
boolean isNull;
}

@ToString
class FieldNameNotNullNoGet {
boolean nul;
}

@ToString
class FieldNameNullGet {
boolean Null;

public boolean isNull() {
return Null;
}
}

@ToString
class FieldNameNullSet {
boolean Null;

public void setNull(boolean Null) {
this.Null = Null;
}
}
```
切换1.2.83到2.0.62, 变量isFastjson2的表现会不一致

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
com.alibaba:fastjson:2.0.62能真正兼容com.alibaba:fastjson:1.2.83

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

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

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied FastjsonCompatibilityTest with fastjson 1.2.83 and 2.0.62, focusing on GenericFastJsonRedisSerializer and the LombokFieldNameNull, LombokFieldNameIsNull, and FieldNameNullGet cases. Compare the serialized output and deserialization results; done means fastjson 2.0.62 deserializes these cases compatibly with 1.2.83 without breaking the existing passing cases.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.