Skip to content

[2.x] Preserve count and length limits beyond int range - #1342

Merged
stevehu merged 3 commits into
2.xfrom
fix/1334-count-limits-2.x
Oct 23, 2026
Merged

stevehu merged 3 commits into
2.xfrom
fix/1334-count-limits-2.x

Conversation

@stevehu

@stevehu stevehu commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1334. Corresponding master PR: #1341.

Valid count limits such as {"minItems":2147483648} currently wrap to different values. Preserve exact limits across all eight count and length keywords so this schema instead reports a minItems validation error with the original limit. Large maxima accept small instances, and min/maxContains remain ineffective without contains.

Use a package-private count-limit representation with int comparisons for ordinary limits and a range marker for larger values. Keep large values as decimals without expanding exponents to integers; format large message arguments as strings to avoid exponent-sized MessageFormat expansion. Preserve ordinary Integer annotations, matching-index annotations, Unicode code-point length, and the protected MinPropertiesValidator.min field as a saturated int view. No reader changes are included.

Add 99 regression cases covering Draft 2019-09 and 2020-12, ordinary boundaries, limits above Integer.MAX_VALUE and Long.MAX_VALUE, integral decimal/exponent forms, applicability, annotations, and a 1e1000000000 limit. Update CHANGELOG.

Validation:

  • Before the fix, the initial 98 regression cases reported 41 failures and 2 errors on JDK 8.
  • mvn -B -ntp -Dtest=Issue1334Test,Issue1292Test test: 117 passed on JDK 8.
  • mvn -B -ntp clean verify: 9,083 tests, 15 skipped, no failures or errors, independently on JDK 8 and JDK 17.
  • GitNexus pre-change impact and pre-commit detect_changes report CRITICAL reach through shared validation paths; owner approved the focused Count and length limits beyond int range throw on master or wrap on 2.x #1334 scope. Source review and the complete suites supplement this graph evidence.

Remote CI is pending.

@stevehu

stevehu commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Review result: No actionable defects found in PR #1342 (2.x) at the reviewed head. The branch-specific implementation is consistent with PR #1341 (master).

Validation: Issue1334Test passed on this branch: 99 tests, with no failures, errors, or skips. Full suites were not run.

@PHJ2000

PHJ2000 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Verified at 89c1327: my regression tests and Issue1334Test passed on JDK 8 and 21, and the full clean verify passed on JDK 21. LGTM within the cases reported in #1334 and the related compatibility checks.

Detailed verification results.

@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Review notes

The main logic looks correct: the compare-operator rewrites are right, and MinMaxContainsValidator now rejects 2147483648, which used to wrap negative. I ran probes on this branch (this 2.x patch is identical to the master PR #1341 apart from the Jackson 2 imports and textValue). I did not run the PR's own tests.

Correctness

  1. MaxItemsValidator line 34 falls back to CountLimit.of(0) for a non-integral limit, while maxProperties and maxLength fall back to unlimited. Jackson reads 1e400 from JSON text as Infinity, so canConvertToExactIntegral() is false. Probe: {"maxItems":1e400} against [1] gives "must have at most 0 items but found 1", while {"maxProperties":1e400} and {"maxLength":1e400} accept their instances. maxItems: 1.5 also behaves as 0. This contradicts "large maxima accept small instances". Use Integer.MAX_VALUE like the sibling validators.
  2. Huge exponents are only preserved for programmatic BigDecimal nodes. The 1e1000000000 test uses ObjectNode.put(String, BigDecimal). As JSON text, {"minItems":1e1000000000} against [], {"minLength":1e1000000000} against "" and {"minItems":1e400} against [] all produce 0 errors, so the limit is silently ignored. Please add text-parsed cases, or document the limitation.
  3. Message formatting changes at the int boundary. minItems:2147483647 prints 2,147,483,647 (grouped Integer), but 2147483648 prints 2147483648 (a String). Error.getArguments()[0] also changes type from Integer to String. ContainsValidator always uses String.valueOf(bounds.value()), so minContains:1000 prints 1000 while minItems:1000 prints 1,000. Consider one consistent formatting path, or at least a CHANGELOG note on the argument type.

Cleanup

  • CountLimit.compareTo(int actual) compares limit to actual, so every call site hand-inverts the operator (max.compareTo(size) < 0 means size > max). Intention-revealing methods such as isExceededBy(actual) and isNotReachedBy(actual) would be clearer, and outsideIntRange could be a boolean.
  • MinPropertiesValidator now holds both min (saturated, protected) and minimum. Validation ignores min, so a subclass that reads it sees different semantics from the actual check. Consider deprecating it.
  • ContainsValidator line 116: CountLimit.of(1) is allocated on every validate(). Hoist it into a constant.

Tests

  • The negative out-of-range branch (outsideIntRange == -1), the type-loose MinItems/MaxItems branches and Drafts 4/6/7 have no coverage. Probes show maxItems:-3000000000 errors and minItems:-3000000000 passes, so a regression test would be easy to add.

CHANGELOG: the entry's spacing differs from its neighbours, and it doesn't mention the argument-type change.

@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the review notes

The review fixes are now on this branch (0d4801d, with head 42becb5 being an empty commit that refreshed the PR). I read the fix against my earlier notes and the findings are addressed. I did not re-run the tests myself. Fresh Java 8, 11, 17 and 21 CI jobs are queued, so CI is not yet verified on this head.

  • maxItems falls back to unlimited for a non-integral limit.
  • JSON-text exponents are preserved through NodeReader.readSchema, which reads the eight count keywords as BigDecimal. This is a reader change, so the PR description's "No reader changes are included" is no longer accurate.
  • Error arguments are consistently ungrouped Strings, and ContainsValidator uses the same path.
  • compareTo is replaced by isMinimumSatisfiedBy / isMaximumSatisfiedBy, MinPropertiesValidator has one field again, and the contains default is a constant.
  • Tests and CHANGELOG are expanded.

Remaining notes, non-blocking: CountLimitParser matches on the field name only, so a property named minItems inside properties, enum, const or default also gets BigDecimal floats, and MinPropertiesValidator reparses the limit per validate() when it is saturated.

@stevehu
stevehu merged commit ab45838 into 2.x Oct 23, 2026
5 checks passed
@stevehu
stevehu deleted the fix/1334-count-limits-2.x branch October 23, 2026 06:53
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