Skip to content

[2.x] Compare uniqueItems numbers by value (backport #1273) - #1301

Merged
stevehu merged 2 commits into
networknt:2.xfrom
ItsDeadlyProgrammer:fix/1299-unique-items-numeric-2.x
Oct 12, 2026
Merged

stevehu merged 2 commits into
networknt:2.xfrom
ItsDeadlyProgrammer:fix/1299-unique-items-numeric-2.x

Conversation

@ItsDeadlyProgrammer

@ItsDeadlyProgrammer ItsDeadlyProgrammer commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1299 (2.x backport of #1273)

On 2.x, UniqueItemsValidator compared items with HashSet<JsonNode>. JsonNode equality depends on node type (IntNode(1) is not equal to DoubleNode(1.0)), so [1, 1.0] and [[1], [1.0]] passed uniqueItems. This brings over comparisonKey from #1273: numbers are compared as a scale-independent BigDecimal, and arrays and objects are compared element by element. Numbers still stay distinct from booleans and strings.

Changes for 2.x:

Tests:

  • Brought over UniqueItemsNumericEqualityTest with the Jackson 2 imports.
  • Restored [1,1.0] in UniqueItemsApplicabilityTest, so the file now matches master. The {"a":1,"b":1.0} instance mentioned in the issue was already the same on both branches.

Without the validator change, 10 of these test cases fail. With it, mvn verify passes on JDK 17 (8550 tests).

UniqueItemsValidator compared items with HashSet<JsonNode>, and JsonNode
equality depends on node type: IntNode(1) does not equal DoubleNode(1.0), so
[1, 1.0] passed uniqueItems. Items are now compared by the same key as on
master: numbers become a scale-independent BigDecimal, and arrays and objects
are compared element by element.

Jackson 2 changes: node.fields() replaces node.properties(), and
JsonNodeTypes.isNonFiniteNumber is added from master so that NaN and
Infinity keep comparing as nodes instead of throwing.

Brings over UniqueItemsNumericEqualityTest and restores the [1,1.0] instance
in UniqueItemsApplicabilityTest to match master.

Fixes networknt#1299
@stevehu

stevehu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Code review (xhigh · 5 findings)

Thanks for the backport. It matches #1273 apart from the Jackson 2 changes, and I found no bugs it introduces.

Verification on Java 8 (the 2.x target), head 58d10df:

  • Both UniqueItems*Test classes pass, along with the full JSON suites (7,261 suite tests and 148 extras).
  • With the validator change reverted, exactly 10 cases fail, as the description says.
  • Edge cases behave correctly: [0,-0.0] and [0.1,0.10] are duplicates, [1,"1"] is unique, and YAML input [1, 1.0] is a duplicate.
  • The CRLF line endings in UniqueItemsValidator.java are kept.

Findings:

  1. Speed (follow-up, not for this PR). comparisonKey builds a new BigDecimal for every number and deep-copies every nested array or object, so every array pays for it. In my rough timing, 200k ints went from 1.8 to 7.9 ms and 20k objects from 3.3 to 6.7 ms. master has the same design, so I opened uniqueItems: comparisonKey allocations slow down validation of all arrays (master and 2.x) #1302 to fix both branches together.
  2. Please change this one: use properties() instead of fields(). Jackson 2.22.1, which 2.x builds against, has JsonNode.properties() and marks fields() as @Deprecated. 2.x already uses properties() in 4 places (PropertyDependenciesValidator, AbstractDialectRegistry). Using for (Map.Entry<String, JsonNode> property : node.properties()) matches master line for line, avoids the deprecated call and lets the Iterator import go. Please also update the description, which says fields() was needed.
  3. Known limit, tracked in Track exact decimal literals and numeric reader compatibility #1293. With the default mapper, decimal literals are read as doubles before uniqueItems sees them. So [9007199254740993, 9007199254740993.0] still passes, and [1e400, 1e401] is reported as a duplicate because both become Infinity. With USE_BIG_DECIMAL_FOR_FLOATS, both are handled correctly. This isn't for this PR.
  4. One test checks nothing (same on master). decimalsWithDifferentScaleAreDuplicates ([1.0, 1.00]) passes with or without the fix, because the default mapper reads both as the same DoubleNode. Reading with USE_BIG_DECIMAL_FOR_FLOATS would make it meaningful. This is optional.
  5. Javadoc nit (same on master). Lines 75-77 of the comparisonKey Javadoc lost their indentation. This is optional; fixing it on both branches together would keep the files identical.

Once item 2 is in, this is ready to merge.

@ItsDeadlyProgrammer

Copy link
Copy Markdown
Contributor Author

Thanks. Pushed 9f053c1: the object loop now uses node.properties(), and the Iterator import is gone. UniqueItemsValidator now matches master apart from the Jackson package. I also removed the fields() line from the description. mvn verify passes on JDK 17 with 8550 tests.

I'll leave items 4 and 5 for #1302, so master and 2.x can change together.

@stevehu stevehu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. 9f053c1 resolves item 2, and I checked it:

  • UniqueItemsValidator now differs from master only in the Jackson import, and the CRLF line endings are kept.
  • On Java 8, both UniqueItems*Test classes and the full JSON suites pass.

I added items 4 and 5 to #1302 so they're tracked with the speed work. Approving and merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants