Skip to content

Match loose enum numeric strings by exact value - #1353

Merged
stevehu merged 2 commits into
masterfrom
fix/1349-loose-numeric-enum
Oct 23, 2026
Merged

stevehu merged 2 commits into
masterfrom
fix/1349-loose-numeric-enum

Conversation

@stevehu

@stevehu stevehu commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Loose numeric enum matching currently depends on Jackson's text formatting: enum: [100] accepts "1E+2" on master and "100" on 2.x. Match finite numeric members by exact decimal value on both branches, so "100", "100.0" and "1E+2" all match.

Prepare compact sign/coefficient/power keys once per converted enum member and parse each candidate string once. The parser validates complete JSON number syntax and scans huge exponents using saturating long arithmetic, without expanding zeros or allocating an arbitrary-size exponent integer.

Fixes #1349.

Compatibility

  • This intentionally broadens accepted numeric spellings when typeLoose is enabled. Strict mode and literal string enum members keep their existing semantics.
  • Leading plus, whitespace, leading integer zeros, underscores and malformed fractions/exponents do not match numeric members. Signed zero and equivalent fraction/exponent forms match by value.
  • Containers are not coerced from strings; existing scalar/non-finite text matching and protected numeric/array conversion hooks remain intact.
  • Numeric comparison uses the enum node's existing decimal value. Digits already lost by schema parsing are not recovered. Shared JsonNodeValue equality/hashing is unchanged.
  • Compatibility and migration notes.

Validation

  • mvn -B clean verify: JDK 17/21/25; 10,451 unit cases and one artifact check per run, zero failures/errors, 30 existing skips.
  • One million decimal-key comparisons against BigDecimal, rescaled/engineering spellings and 20,000 generated strings checked against an independent JSON-number grammar.
  • Regression tests cover all five supported dialects, every finite numeric node type, exact values above 2^53, mixed/container enums, strict mode, literal strings, non-finite values, extreme BigDecimal scales and 100,000-digit exponents.
  • Branch-specific validation evidence.

These are local results; remote CI must qualify this PR separately.

Review follow-up

  • Fixed the 2.x null-text crash: non-text instances classified as strings now produce a normal non-match when no numeric text is available. Regression tests cover BinaryNode, POJONode and the parser's null contract on both branches.
  • Added a regression and compatibility example for schema-reader precision: default parsing of 9007199254740993.0 matches "9007199254740992"; an explicit USE_BIG_DECIMAL_FOR_FLOATS reader preserves and matches "9007199254740993". This boundary remains intentional; reader precision policy is unchanged.
  • Retained the parser's two linear passes to keep complete grammar validation separate from coefficient normalization; no performance claim or exponent expansion is introduced.
  • Documentation links above are pinned to this commit and remain usable after deleting the topic branch.

@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Independent review of 6aeb391 (scratch build of the PR head against its merge base). No bugs found on master; remote CI is green (Java 17, 21, 25, submit-maven). I did not run the PR's own test classes locally.

Verified

  • Differential against an exact-decimal oracle (grammar check plus BigDecimal comparison): 24 enum members x about 3,000 candidate spellings each (plain/engineering/scientific forms, sign, whitespace, leading +, leading zeros, underscores, non-ASCII digits, Unicode minus, 1., .5, 1e, huge exponents). No mismatches for members the schema reader parses losslessly.
  • No regressions: everything the base accepted is still accepted.
  • Zero in all spellings (0, -0, 0.00, -0e5, 0E-10, 0e999999999999) matches; 100 matches 100, 100.0, 1E+2, 1e2, 1000000e-4.
  • A 20-million-digit string costs about the same as the base.

Findings (low severity)

  1. Exact matching still depends on schema parse rounding. The comparison is exact against the enum node's decimal value, but the default reader has already rounded members that exceed double precision or range: enum: [9007199254740993.0] accepts "9007199254740992" and rejects "9007199254740993"; enum: [1e-400] accepts "0"; enum: [1.0E+400] matches only "Infinity". This is documented in the PR and identical to the base, so it is not a regression, but it is inconsistent with Preserve exact count values in copied schema trees #1351, which keeps such values exact for count keywords by promoting lossy floats to DecimalNodes. Worth a follow-up issue if you want enum to match.
  2. NumericStringValue.parse scans the significand twice and builds the key from two substrings plus a concatenation. First/last nonzero positions could be tracked during the grammar pass. Small cleanup, not a performance problem.
  3. Links in the PR description use blob/<branch-name>, which will 404 once the branch is deleted after merge; a commit-SHA or default-branch link would not.

🤖 Generated with Claude Code

@stevehu

stevehu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on da667d8, verified on a scratch build of the pushed head.

  • Null guard added. NumericStringValue.parse now returns null for null text. A BinaryNode instance against enum: [100], [1.5] and [1E+2] under typeLoose returns a normal validation error. Master never threw here, so this is the shared guard plus the new test.
  • Behaviour unchanged otherwise. My exact-decimal differential (24 members x about 3,000 candidate spellings) accepts exactly the same set as on 6aeb391.
  • Tests. Issue1349Test, NumericStringValueTest and KeywordNumericEqualityTest (926 tests) pass on JDK 21. The new BinaryNode/POJONode coverage is included.
  • CI. All checks pass on this head (Java 17, 21, 25, submit-maven).

Outcomes

  • Fixed: the null-text crash, the missing test for non-text STRING nodes, and the branch-name doc links (now commit-pinned).
  • Skipped, acceptable as is: the two-pass parse (cosmetic) and the parse-rounding boundary (enum: [9007199254740993.0] accepts "9007199254740992"). The latter is now tested and documented, identical to the base, and still inconsistent with the exact count values in Preserve exact count values in copied schema trees #1351. No blockers remain from my review.

I did not run the full JDK matrix locally; CI covers it.

🤖 Generated with Claude Code

@stevehu
stevehu merged commit ed06b2a into master Oct 23, 2026
4 checks passed
@stevehu
stevehu deleted the fix/1349-loose-numeric-enum branch October 24, 2026 00:41
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.

Decide typeLoose numeric enum string coercion policy across master and 2.x

1 participant