STRICT_DUPLICATE_DETECTION rejects repeated XML elements read by a custom deserializer

Open
#931 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
api, backend

Research direction

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.

Written by the indexing model from the issue text.

Description

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

Dominant language
Java
Stars
636
Forks
250
Avg merge
7h 10m
Merged PRs (30d)
8

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from FasterXML/jackson-dataformat-xml

All issues in FasterXML/jackson-dataformat-xml

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.