Skip to content

[2.x] Preserve exact multipleOf operands - #1291

Merged
stevehu merged 14 commits into
networknt:2.xfrom
PHJ2000:fix-multipleof-precision-2x
Oct 13, 2026
Merged

stevehu merged 14 commits into
networknt:2.xfrom
PHJ2000:fix-multipleof-precision-2x

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1286. Corresponding master PR: #1290.

Preserve exact integral and BigDecimal operands in multipleOf validation. For example, 9007199254740993 is rejected as a multiple of 2 and accepted as a multiple of 3. The conversion hooks are used for both directly constructed validators and subclasses, and validation checks their returned values without substituting a rounded divisor.

Mixed integer/Double or Float inputs retain the exact integer value instead of rounding it through double. For example, multipleOf: 2.0 now rejects integer 9007199254740993. Integer divisor 9007199254740993 also rejects input 9007199254740993.0, which the default reader has already rounded to 9007199254740992. Double/Float conversion itself still uses BigDecimal.valueOf(doubleValue()). Reader precision remains a separate issue in #1293.

The integer fast path checks whether the returned dividend can be represented exactly as a long. Other values use coefficient/scale arithmetic without constructing exponent-sized powers. The default divisor conversion retains trailing-zero normalization, while error messages preserve the representation returned by custom divisor hooks. Javadoc and the CHANGELOG describe the exact conversion and its effect on integral dividend scale (for example, 2 instead of 2.0). Zero/negative divisor handling, type-loose conversion and this branch's non-finite-number behavior are unchanged.

Tests cover exact values, mixed inputs, conversion overrides, messages and extreme scales. Forked tests check whether forced termination succeeds and retain the timeout failure if the child is still alive.

Validation: mvn -B -ntp clean verify passes on Java 8 and 17 with Jackson 2; 8,841 tests reported, 15 skipped, no failures or errors on each JDK. All 136 MultipleOfValidatorTest cases pass.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The focused fix correctly addresses precision loss and includes comprehensive regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Preserves exact multipleOf arithmetic for integral and BigDecimal operands while retaining legacy floating-point behavior.

Changes:

  • Avoids lossy double conversion for exact numeric nodes.
  • Adds regression coverage for large integers and extreme decimals.
File Description
MultipleOfValidator.java Uses exact decimal operands where possible.
MultipleOfValidatorTest.java Covers precision and range regressions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Handle extreme numeric scales and trailing-zero inputs without
normalization overflow or excessive computation.

Preserve exact numeric values in default validation readers while
retaining public mapper defaults and compact doubles where lossless.

Fix related enum, uniqueness, bounds, and count-limit handling.
Preserve custom-mapper behavior and protected validator hooks.

Add regression coverage and update the Numeric Precision documentation.

Forward nextFieldName() to the underlying parser's fast path.

Validation: mvn -B verify on JDK 8; 8,561 tests, 15 skipped, 0 failures or errors.
@PHJ2000

PHJ2000 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Ported the latest review fixes in 59c4691; the detailed response and parsing measurements are on #1290.

This version keeps Jackson 2's decimal scales because its DecimalNode equality is already numeric, retains its YAML non-finite-input behavior, and adapts parser APIs and tests for Java 8. The shared fixes include FloatNode bounds, large-exponent loose values, location-aware reading, conversion-hook compatibility, and count limits. Non-integral/non-finite maxItems now uses the other maximum validators' unrestrictive fallback.

Java 8 mvn -B -ntp verify passed: 8,654 tests reported, 15 skipped, no failures or errors.

@PHJ2000

PHJ2000 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Applied the four follow-ups from #1290 in 85d9ce6: float-safe conversion for both multipleOf operands, cache clearing on parser advancement, early rejection of NaN/FloatNode infinity bounds, and shared helpers in DecimalUtils. Existing DoubleNode infinity behavior and Jackson 2 decimal scales remain unchanged.

Added 19 regression cases. Full mvn -B -ntp clean verify passed on Java 8: 8,673 tests reported, 15 skipped, no failures or errors.

@PHJ2000

PHJ2000 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Updated in 7579163 with the same narrowed scope as #1290: only MultipleOfValidator and MultipleOfValidatorTest differ from the 2.x base. Default readers and other keywords are restored; the exact-operand fix, arithmetic safeguards, and conversion hooks remain.

mvn -B -ntp clean verify passed on Java 8: 8,543 tests reported, 15 skipped, no failures or errors. The description is updated, and the decimal-reader work and deferred findings are tracked in #1293.

@PHJ2000

PHJ2000 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

Applied the review fixes from #1290 to 2.x. This restores the existing behavior for mixed integer/floating-point inputs and updates conversion-hook handling, divisor messages, and test process cleanup. The changes are adapted for Java 8 and Jackson 2.

All 99 MultipleOfValidatorTest cases and the full mvn -B -ntp clean verify build passed.

Keep conversion hooks and the existing primitive fallback for subclasses.
Only the concrete built-in validator with concrete IntNode/LongNode inputs
uses the direct remainder path.

Local JDK 8 measurements show the scaled-dividend subclass case increasing
from about 7.66 to 7.86 ns/op; ordinary integer paths improve slightly.
Keep this optimization separate so it can be reverted independently.
@PHJ2000

PHJ2000 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Applied the latest review fixes from #1290 in a98dc32. Mixed-number validation now preserves the existing behavior when a dividend hook applies abs, negate, or scaling, and error messages show the divisor used for the check. The redundant conversions and forked-test cleanup/coverage issues are also addressed.

The changes preserve Jackson 2's existing NaN/Infinity behavior, including custom divisor hooks, and use Java 8-compatible APIs.

I added a direct integer fast path that skips BigDecimal conversions for the built-in validator with standard IntNode and LongNode inputs. Subclasses continue to use the conversion hooks. In local JDK 8 JMH measurements before and after this change, the subclass case where getDividend() returns 6.0 increased from about 7.66 to 7.86 ns/op.

mvn -B -ntp clean verify passed on Java 8 and Java 17. Each run reported 8,617 tests, with 15 skipped and no failures or errors; all 129 MultipleOfValidatorTest cases passed.

@codecov-commenter

codecov-commenter commented Oct 13, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (2.x@d25a6e5). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@          Coverage Diff           @@
##             2.x    #1291   +/-   ##
======================================
  Coverage       ?   79.14%           
  Complexity     ?     2097           
======================================
  Files          ?      202           
  Lines          ?     7007           
  Branches       ?     1266           
======================================
  Hits           ?     5546           
  Misses         ?     1007           
  Partials       ?      454           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@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 for sticking with this through all the rounds. The final version does exactly what we agreed: validation uses what the hooks return, the override detection is gone, and the forked-test cleanup checks waitFor.

Verification (each PR merged into the current base)

  • MultipleOfValidatorTest: 138 on master (JDK 17, 25), 136 on 2.x (Java 8, 21). The JSON Schema Test Suite (7261) and the extras pass. The full suite passes on master/JDK 25 (9002) and 2.x/Java 8 (8876).
  • Revert check: with the base validator, 45 of 138 tests fail on master and 41 of 136 on 2.x.
  • 60k random divisor/value pairs across Int/Long/BigInteger/BigDecimal/Double/Float nodes, compared with an exact BigDecimal remainder check: no wrong verdicts, messages or exceptions on either branch. The base code gave about 170 wrong verdicts and over 4,000 wrong messages on the same inputs.
  • I checked isMultipleOf by hand, including the modular-exponentiation path.

What I pushed to both branches
Both PRs conflicted with their base in CHANGELOG.md after #1326/#1327 merged. I merged the current base and kept both entries. I also changed this entry's reference to the issue, (#1286), so it matches on both branches. CI passes on the new heads.

For later, outside this PR

  • multipleOf: 100 still reports must be multiple of 1E+2, which you kept on purpose.
  • On 2.x, a divisor the reader turns into Infinity (such as 1e400) still throws when the schema loads. The reader side is #1293.

Merging #1290 and #1291 together.

@stevehu
stevehu merged commit f2387c2 into networknt:2.x Oct 13, 2026
4 checks passed
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.

4 participants