Skip to content

Share numeric value equality across validation keywords (#1307) - #1343

Merged
stevehu merged 3 commits into
masterfrom
fix/1307-numeric-equality
Oct 23, 2026
Merged

stevehu merged 3 commits into
masterfrom
fix/1307-numeric-equality

Conversation

@stevehu

@stevehu stevehu commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1307. Companion PR: #1344.

Share numeric value equality and matching hashes across const, enum and uniqueItems, including nested arrays and objects, extreme decimal scales, and matching non-finite float/double values. Preserve the shortest-decimal interpretation of floating-point nodes; exact decimal reading remains separate in #1293.

Keys retain only the node and cached hash. Remove the per-number kind/value cache and duplicate numeric comparison dispatch. Compare large integer pairs directly. Default enum conversion avoids normalized numeric copies and deep-copied arrays while retaining protected conversion hooks. Prepare loose scalar matching once per schema and skip containers. This fixes the existing Jackson 3 exception for array/object enum members.

Preserve existing branch-specific typeLoose compatibility: enum [100] matches string "1E+2" on master/Jackson 3. Tests and changelog explain the existing divergence. Coverage includes the 900-pair numeric matrix, loose container matching, decimal scales, extreme exponents, hash collisions, numeric enum deduplication and protected conversion overrides.

Validation: mvn -B clean verify passed independently on JDK 17/21/25: 10,023 tests per run, zero failures/errors, 30 skips. No exclusions or POM changes. git diff --check passed.

JDK 17 JMH allocation check (1,000 unique items, two 1s warmups, three 1s measurements, one fork, GC profiler, 256MB heap): about 8 KB/op less allocation for top-level decimal, BigInteger and double shapes versus the previously recorded cached-key results; nested allocation unchanged. No general throughput improvement is claimed. Reproduce with mvn -B -Pbenchmark clean package, then java -jar target/benchmarks.jar NumericUniqueItemsBenchmark -p duplicate=false -wi 2 -i 3 -w 1s -r 1s -f 1 -prof gc -jvmArgs "-Xms256m -Xmx256m".

Follow-up policy decision: #1349 tracks the existing master/2.x difference in numeric enum string matching with typeLoose. This PR preserves the branch's current behavior; alignment and compatibility policy will be decided separately.

@stevehu

stevehu commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Review result: No actionable regressions found in PR #1343 (master) at the reviewed head.

Validation: Full offline verification passed on JDK 25: 10,015 tests, 30 skipped.

GitNexus results were inconclusive.

@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Review notes for #1343. Nothing here blocks on correctness of the new equality code.

What I checked

  • Fuzzed JsonNodeValue equality, same(), nested-in-array equality and hash agreement against plain decimalValue().compareTo (NaN/Infinity handled separately): about 770M pairs from 27,780 numbers across Int/Long/Float/Double/BigInteger/BigDecimal, including long/2^53/2^63 boundaries and extreme scales. No mismatches.
  • Ran enum/const/uniqueItems over about 16,500 schema/instance combinations, with typeLoose on and off, against d0005e6 and this branch. The only differences are the intended ones (numbers nested in arrays/objects now compare by value).

Findings

  1. Existing crash in a loop this PR edits (EnumValidator.java:120). With typeLoose(true) and {"enum":[[1]]} or {"enum":[{"a":1}]}, validating the string "1" throws JsonNodeException (ArrayNode method asString() cannot coerce). It throws identically on d0005e6 (294 cases in my run), so it is not a regression, but key.node.asString() is now in the rewritten loop. Skipping non-scalar members would fix it. 2.x is unaffected.
  2. The NumberValue cache is paid on every item and rarely read (JsonNodeValue.java:75). HashSet only calls equals() on a full hash match, so unique items never use the cached kind/value. The PR's own table is slower on all four shapes (e.g. fractional 14,009 to 13,629 ops/s, BigInteger 2,212 to 2,142, double 51,452 to 50,091) with more allocation on three. A duplicate-heavy benchmark showing a win would justify it; otherwise one JsonNodeValue(node, hash) with the lazy same() is simpler.
  3. Enum normalizes twice (EnumValidator.java:100). processNumberNode (decimalValue + stripTrailingZeros + DecimalNode) and processArrayNode (deepCopy for any array containing a number) run before JsonNodeValue.of() classifies again. The key compares by value, so this no longer affects the lookup. It is only there for the protected hooks. Consider running it only when a subclass overrides them.
  4. Comparison rules live in two places (JsonNodeValue.java:87): NumberValue.equals and sameNumber. They agree today per the fuzz run, but a change to one can silently diverge from the other.
  5. Test pins a surprising behaviour (KeywordNumericEqualityTest.java:108). enumTypeLooseStillUsesTheConvertedNumberText asserts that enum: [100] accepts "1E+2" and rejects "100". The 2.x PR ([2.x] Share numeric value equality across validation keywords (#1307) #1344) asserts the opposite for the same schema, so the two branches now differ on user-visible typeLoose results. Fine if preservation is intentional, but worth a comment or tracking issue.
  6. Minor (JsonNodeValue.java:112): decimalValue(JsonNode) just forwards to node.decimalValue(), and hash() has identical NON_FINITE and DOUBLE branches.
  7. Test gap (KeywordNumericEqualityTest.java:104): no typeLoose case with array/object enum members, which would have caught item 1.

🤖 Generated with Claude Code

@stevehu stevehu changed the title Share numeric equality and cache uniqueItems number values (#1307) Share numeric value equality across validation keywords (#1307) Oct 23, 2026
@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up: my earlier review notes are addressed in b218bdc.

  • Loose enum matching skips array/object members, so the Jackson 3 asString() crash is gone (container-only and mixed schemas are tested).
  • Enum conversion hooks keep the original nodes by default and only copy when a subclass hook changes a number.
  • The NumberValue subclass and its cached fields are removed. There is one comparison path (sameNumber) for top-level and nested values.
  • The decimalValue pass-through is removed and the duplicate hash cases merged.
  • Added tests for negative/zero/positive scales, repeated loose validations, extreme exponents, hash collisions and first-member retention.
  • The master vs 2.x typeLoose text difference (enum: [100] matches "1E+2" here, "100" on 2.x) is kept deliberately and documented in tests and the changelog.

I reran my fuzz on b218bdc: 771,728,400 number pairs across Int/Long/Float/Double/BigInteger/BigDecimal, with no mismatches in equality, nested equality or hash agreement. That covers only the numeric equality and hashing code, not the schema-level enum/const/uniqueItems behavior. I did not rerun the old-vs-new schema comparison. Remote CI is still pending.

🤖 Generated with Claude Code

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.

1 participant