Skip to content

jsonschema: preserve numbers when decoding schema values - #84

Draft
Tangerg wants to merge 1 commit into
google:mainfrom
Tangerg:codex/preserve-schema-json-numbers
Draft

Tangerg wants to merge 1 commit into
google:mainfrom
Tangerg:codex/preserve-schema-json-numbers

Conversation

@Tangerg

@Tangerg Tangerg commented Sep 28, 2026

Copy link
Copy Markdown

Fixes #83.

Decoding {"const":9007199254740993} currently changes the constant to 9007199254740992. Consequently, validation rejects the original exact value and accepts the adjacent incorrect value. Enum constraints and nested constants have the same problem; values outside the float64 range cannot even be decoded.

Use json.Decoder.UseNumber for generic schema values, including the separate explicit-null-aware Const path. The existing exact numeric comparison code then receives the original constraint. Keep rejection of trailing JSON values and malformed input. No new dependencies, compatibility switches, duplicate validation representations, or public field type changes are introduced.

Scope and compatibility

This complements #81 rather than duplicating it: #81 fixes instance type classification, while this change preserves the schema's own values before validation. The regression deliberately uses no type keyword and passes independently of #81.

Numbers decoded into Const, Enum, Examples, and Extra now have dynamic type json.Number instead of float64. Callers asserting float64 must adapt. Already-rounded floating-point instance values cannot recover their original JSON precision. This draft requests review of the behavioral compatibility change and release treatment.

Concrete numeric fields such as Minimum, Maximum, and MultipleOf retain their current types and precision limitations. Instance classification and default application are not changed. This is not a claim of complete arbitrary-precision support throughout the validator.

Validation

  • New regression tests fail on main at 794ce5e and pass with this change. They verify exact schema round-trips, acceptance of the original value, and rejection of adjacent incorrect values for const/enum, nested objects/arrays, signed and unsigned large integers, precise decimals, and 1e400.
  • Additional coverage checks Examples/Extra, explicit null, non-numeric values, unchanged concrete numeric fields, and invalid/trailing JSON rejection.
  • go test ./..., go test -race ./..., go build ./..., go mod tidy -diff, formatting, and git diff --check passed with Go 1.27.0 on darwin/arm64.
  • go vet ./... and staticcheck ./... report existing invalid/unexported JSON-tag fixtures and unused declarations. Both were also run against an unmodified archive of 794ce5e and produced the same diagnostics. No checks or assertions were disabled.

Related downstream protocol-boundary fix: modelcontextprotocol/go-sdk#1311.

@google-cla

google-cla Bot commented Sep 28, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

jsonschema: schema decoding rounds const and enum numbers before validation

1 participant