XmlMapper with `FAIL_ON_UNKNOWN_PROPERTIES` fails on whitespace-only element
评估
调研方向
从 JacksonXmlWhitespaceOnlyContentTest.deserializesWhitespaceOnlyElementContent reproduction 以及配置了 FAIL_ON_UNKNOWN_PROPERTIES 的 XmlMapper builder 开始。跟踪 Item 内仅包含空白字符的内容如何变成一个名称为空的属性。完成的标准是测试通过,同时真正的未知属性仍然失败。
由索引模型根据 Issue 内容生成。
描述
Search before asking
- I searched in the issues and found nothing similar.
Describe the bug
Take this XML:
<Response>
<Item name="example">
</Item>
</Response>
If I want to deserialize that Item element into record Item(String name), I'm faced with this error:
Unrecognized property "" (class me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Item), not marked as ignorable (one known property: "name")
at [No location information] (through reference chain: me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Response["Item"]->me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Item[""])
tools.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized property "" (class me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Item), not marked as ignorable (one known property: "name")
at [No location information] (through reference chain: me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Response["Item"]->me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest$Item[""])
at tools.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at tools.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:1378)
at tools.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:2099)
at tools.jackson.databind.deser.bean.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:2032)
at tools.jackson.databind.deser.bean.BeanDeserializerBase.handleUnknownProperties(BeanDeserializerBase.java:1957)
at tools.jackson.databind.deser.bean.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:922)
at tools.jackson.databind.deser.bean.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1695)
at tools.jackson.databind.deser.bean.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:629)
at tools.jackson.databind.deser.bean.BeanDeserializer.deserialize(BeanDeserializer.java:200)
at tools.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:568)
at tools.jackson.databind.deser.bean.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:943)
at tools.jackson.databind.deser.bean.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:775)
at tools.jackson.databind.deser.bean.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1695)
at tools.jackson.databind.deser.bean.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:629)
at tools.jackson.databind.deser.bean.BeanDeserializer.deserialize(BeanDeserializer.java:200)
at tools.jackson.dataformat.xml.deser.XmlDeserializationContext.readRootValue(XmlDeserializationContext.java:69)
at tools.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2666)
at tools.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1556)
at me.retrodaredevil.randomjunkk.JacksonXmlWhitespaceOnlyContentTest.deserializesWhitespaceOnlyElementContent(JacksonXmlWhitespaceOnlyContentTest.java:27)
This is an issue for more complicated XML data, specifically data like this:
<Item name="thing">
<AnotherElement name="thing"/>
</Item>
I run into this problem because Item typically has nested elements inside of it, but when it doesn't, the whitespace data within it is treated as a special blank string property.
Version Information
Jackson 3.2.1
Reproduction
import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.jupiter.api.Test;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.dataformat.xml.XmlMapper;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JacksonXmlWhitespaceOnlyContentTest {
// This test fails because whitespace-only element content is exposed as an empty-named property.
@Test
void deserializesWhitespaceOnlyElementContent() {
XmlMapper mapper = XmlMapper.builder()
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
String xml = """
<Response>
<Item name="example">
</Item>
</Response>
""";
Response response = mapper.readValue(xml, Response.class);
assertEquals(new Item("example"), response.item());
}
record Response(
@JsonProperty("Item") Item item
) {
}
record Item(String name) {
}
}
Expected behavior
FAIL_ON_UNKNOWN_PROPERTIES should only cause failures when there are actual properties that are unknown. I don't think this weird empty string property edge case should cause a failure here.
Additional context
No response
- 主要语言
- Java
- 星标
- 636
- 派生
- 250
- 平均合并
- 7 小时 10 分钟
- 30 天内合并 PR
- 8
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
FasterXML/jackson-dataformat-xml 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
-
难度 4/5 3-5 天 新手友好度 55/100
-
难度 4/5 3-5 天 新手友好度 45/100
FasterXML/jackson-dataformat-xml#931 · 3 条评论 ·
-
3.x
难度 4/5 3-5 天 新手友好度 25/100
FasterXML/jackson-dataformat-xml#787 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
FasterXML/jackson-dataformat-xml#712 · 3 条评论 ·
查看 FasterXML/jackson-dataformat-xml 的全部 Issue
相似的 Issue
-
Cleanup: restore the interrupt flag in AiFailureAnalyzer可能已有人在做 @Rishankgupta08 今天认领。 未关闭good first issue
难度 2/5 1-3 小时 新手友好度 82/100
seleniumboot/selenium-boot#258 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
johanhaleby/occurrent#1505 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
FasterXML/jackson-databind#6443 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 78/100
维护者通常 1 天内回复