[BUG]JSON.parseObject将json转成对象时出现空字段的异常
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
这个异常在1.x版本和2.0.2版本均没有出现,在2.0.3版本出现了
下面是我的代码
```java
Admin admin = JSON.parseObject(JSON.toJSONString(adminLoginForm), Admin.class);
adminLoginForm:
public class AdminLoginForm {
/**
* 管理员账号
*/
@NotBlank(message = "用户名不能为空")
@Length(min = 2, max = 20, message = "用户名长度为{min}-{max}位")
@Pattern(regexp = "^\\w+$", message = "用户名只能由数字、字母或下划线组成")
public String username;
/**
* 密码
* 为保证安全,password不进行序列化
*/
@NotBlank(message = "密码不能为空")
@Length(min = 4, max = 20, message = "密码长度为{min}-{max}位")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
public String password;
}
admin:
public class Admin extends BaseModel implements Serializable {
/**
* 管理员账号
*/
@NotBlank(message = "管理员账号不能为空")
@Length(min = 2, max = 20, message = "管理员账号长度为{min}-{max}位")
@Pattern(regexp = "^\\w+$", message = "管理员账号只能由数字、字母或下划线组成")
@Column(name = "username", nullable = false, unique = true)
private String username;
/**
* 密码
* 为保证安全,password不进行序列化
*/
@NotBlank(message = "密码不能为空")
@Column(name = "password", nullable = false)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
}
```
以上代码在2.0.2及以前运行良好,在2.0.3会报password空异常
Contributor guide
Research direction
Reproduce the issue with the provided AdminLoginForm and Admin classes, comparing fastjson2 versions 2.0.2 and 2.0.3. Start by tracing JSON.toJSONString and JSON.parseObject handling of the password field with WRITE_ONLY access, then add a regression test showing that password is not unexpectedly null after conversion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100