STRICT_DUPLICATE_DETECTION rejects repeated XML elements read by a custom deserializer

未关闭
#931 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java
领域
api, backend

调研方向

Start with StrictDuplicateDetectionXmlTest and trace FromXmlParser token handling into the duplicate-name check in JsonReadContext, focusing on how repeated XML elements are represented before readValueAsTree() builds the tree. Run the three listed JUnit cases to confirm the failure and controls. Done means the custom-deserializer case with strict detection enabled preserves both OU values while the declared-List and strict-disabled controls still pass.

由索引模型根据 Issue 内容生成。

描述

Summary

In XML, sibling elements with the same name are legitimate and normally map to a list. When such repeats are bound to a declared List property, STRICT_DUPLICATE_DETECTION correctly tolerates them (they are coalesced into the list). But when the same repeats are consumed by a custom deserializer that materializes the element with JsonParser.readValueAsTree(), enabling STRICT_DUPLICATE_DETECTION makes the read fail with:

tools.jackson.core.exc.StreamReadException: Duplicate Object property "OU"

A repeated XML element is thus reported as a duplicate object property. This makes STRICT_DUPLICATE_DETECTION effectively incompatible with any element whose child tag names are dynamic/heterogeneous and must be read as a tree — a Distinguished Name is the canonical example:

<Dn>
    <C>FR</C>
    <O>ACME</O>
    <OU>Sales</OU>
    <OU>EMEA</OU>   <!-- legitimate repeat; rejected under STRICT -->
    <CN>leaf</CN>
</Dn>

Because the RDN tag names differ (C, O, OU, CN), the element cannot be bound to a single declared List, so a custom deserializer is required — and that path is broken by the feature.

How to reproduce

./gradlew test     # the three JUnit cases below
./gradlew run      # prints the three cases

run output:

1) declared List + STRICT on  -> OK     [Sales, EMEA]
2) custom deser  + STRICT off  -> OK     [Rdn[type=C, value=FR], Rdn[type=O, value=ACME], Rdn[type=OU, value=Sales], Rdn[type=OU, value=EMEA], Rdn[type=CN, value=leaf]]
3) custom deser  + STRICT on   -> FAILS  StreamReadException: Duplicate Object property "OU"

StrictDuplicateDetectionXmlTest:

  1. repeatedElementsBoundToDeclaredListAreToleratedUnderStrict — control: declared List + STRICT → OK.
  2. repeatedElementsViaCustomDeserializerWorkWithoutStrict — control: custom deserializer, STRICT off → OK (both <OU> kept).
  3. repeatedElementsViaCustomDeserializerFailUnderStrict — the issue: same input, STRICT on → StreamReadException: Duplicate Object property "OU".

Actual behaviour

STRICT_DUPLICATE_DETECTION fires at the streaming/token level (JsonReadContext duplicate name check) as soon as the second <OU> name is seen, before the XML module coalesces the repeats into an array — so the tree is never built.

Expected behaviour

Reading the element via readValueAsTree() should coalesce the repeated <OU> siblings into an ArrayNode (as it already does when STRICT_DUPLICATE_DETECTION is disabled), rather than reporting them as a duplicate object property. Repeated XML elements are not JSON-style duplicate keys.

Additional information

Within the custom deserializer, every way of consuming the element goes through the strict parser and triggers the same detection: readValueAsTree(), manual nextToken() streaming, @JsonAnySetter, TokenBuffer.copyCurrentStructure(...), and skipChildren(). Reading the underlying Stax XMLStreamReader directly avoids the detection but desynchronises FromXmlParser (one-token lookahead), breaking the outer parse. The feature therefore cannot be kept while deserializing such an element.

A Gradle projet that illustrates this issue is attached to the ticket:
jackson-xml-strict-duplicate-dn-sources.zip

主要语言
Java
星标
636
派生
250
平均合并
7 小时 10 分钟
30 天内合并 PR
8

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

FasterXML/jackson-dataformat-xml 的其他 Issue

查看 FasterXML/jackson-dataformat-xml 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。