`Optional<JsonNode>` deserialization from "absent" value does not work in the expected way
还没有人认领这个 Issue。
评估
调研方向
从 jackson-datatype-jdk8 OptionalDeserializer 开始,通过 PropertyValueBuffer::_findMissing、JsonDeserializer#getAbsentValue 和 JsonNodeDeserializer#getNullValue 跟踪缺失属性的处理。确认从 {} 反序列化的 MyRecord 示例的行为,并确保缺失的 Optional 保持为空,而不是包装一个 NullNode;使用项目现有的测试验证预期结果。
由索引模型根据 Issue 内容生成。
描述
Example:
public record MyRecord(
Optional<JsonNode> myField
) {
}
When deserialized from: {}
Expected:
myField.isPresent() == false
Actual:
myField.isPresent() == true
This is because myField gets set to an Optional of a NullNode
After spending some time looking into the source code of both the jackson-databind and the jackson-datatype-jdk8 libraries, the problem seems to lie in the OptionalDeserializer (or higher).
During deserialization, when a property is missing, the PropertyValueBuffer::_findMissing method is called and in it, this piece of code is called:
https://github.com/FasterXML/jackson-databind/blob/0fe97e0d69b7d5362907b094d5b979bc2216dc4a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java#L203
// Third: NullValueProvider? (22-Sep-2019, [databind#2458])
// 08-Aug-2021, tatu: consider [databind#3214]; not null but "absent" value...
Object absentValue = prop.getNullValueProvider().getAbsentValue(_context);
if (absentValue != null) {
return absentValue;
}
The OptionalDeserializer is not overriding its inherited getAbsentValue method to return Optional.ofNullable(_valueDeserializer.getAbsentValue(ctxt)); (or similar).
Due to the lack of the overriding, the inherited getAbsentValue method actually calls getNullValue instead as can be seen here:
https://github.com/FasterXML/jackson-databind/blob/0fe97e0d69b7d5362907b094d5b979bc2216dc4a/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java#L349
@Override
public Object getAbsentValue(DeserializationContext ctxt) throws JsonMappingException {
return getNullValue(ctxt);
}
In the case of a JsonNode, the JsonNodeDeserializer is used. This deserializer overrides the getNullValue method to return a NullNode.
@Override
public JsonNode getNullValue(DeserializationContext ctxt) {
return ctxt.getNodeFactory().nullNode();
}
- 主要语言
- Java
- 星标
- 425
- 派生
- 125
- 平均合并
- 19 分钟
- 30 天内合并 PR
- 1
环境准备
我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
FasterXML/jackson-modules-java8 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 72/100
-
难度 4/5 3-5 天 新手友好度 25/100
FasterXML/jackson-modules-java8#382 · 3 条评论 ·
-
date-time-config
难度 4/5 3-5 天 新手友好度 35/100
FasterXML/jackson-modules-java8#374 · 31 条评论 · 1 个 reaction ·
-
难度 5/5 一周以上 新手友好度 25/100
FasterXML/jackson-modules-java8#310 · 1 条评论 ·
-
test-needed
难度 5/5 一周以上 新手友好度 35/100
FasterXML/jackson-modules-java8#304 · 3 条评论 ·
查看 FasterXML/jackson-modules-java8 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
ci-failure-cause test-failure
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 65/100
nextcloud/notes-android#3367 ·
维护者通常 1 天内回复
-
:wave: team-triage a:chore in:isolated-projects in:kotlin-dsl
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复