Skip to content

fix: keep large integers exact when loading JSON data files - #1434

Open
mbevc1 wants to merge 5 commits into
mainfrom
20261202_json-number-precision
Open

mbevc1 wants to merge 5 commits into
mainfrom
20261202_json-number-precision

Conversation

@mbevc1

@mbevc1 mbevc1 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Send integers from JSON data files exactly as written. --user-data and kosli attest custom --attestation-data decoded every number as a float64, so an integer above 2^53 reached Kosli as a different number (12345678901234567890 became 12345678901234567000).

Every other number is sent as before: 1.0 as 1, 1e2 as 100, and decimals with float64 precision. YAML files are unchanged.

Closes #1430

Checklist

  • Docs are autogenerated from CLI help — make sure the help text reflects your changes (n/a: no flag or help text changes)
  • Helm chart (charts/k8s-reporter/) updated, if needed. Note: these changes live in a separate PR (n/a: the k8s reporter is untouched)
  • Terraform provider and related changes (terraform-provider-kosli, terraform-aws-kosli-reporter) updated, if needed (n/a: neither passes JSON data files)

@github-actions github-actions Bot added go Pull requests that update go code fix labels Dec 2, 2026
@claude

claude Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @mbevc1's task in 2m 24s —— View job


Follow-up review complete

  • Gather context (new commits, prior review comments)
  • Check status of each previously raised finding
  • Review new commits' changed lines for new issues
  • Post summary

Note on scope: BEFORE=29572c22 isn't reachable in the repo (the branch history was rewritten between review and this push — all 5 commits now on the branch were created at 18:40:58–18:40:59 UTC). I compared the full PR diff (git diff origin/main...HEAD) against the state the prior review commented on.

Status of previous findings

Both of my prior inline comments were on internal/utils/yaml.go (leading-zero handling nit, and the "whatever their size" docstring nit). The latest commits (revert: keep decimal numbers exact when loading YAML data files and the final fix: keep only integers exact when loading JSON data files) fully revert internal/utils/yaml.go to match main — it has zero diff now (git diff origin/main...HEAD -- internal/utils/yaml.go is empty). Both review threads are already marked resolved on GitHub, and no action is needed since the commented-on code no longer exists in this PR.

The PR is now narrowed to JSON-only: large integers (any size) are sent exactly as written; every other JSON number still round-trips through float64 as before (1.0 → 1, 1e2 → 100); 1e400 is a CLI error again, naming the file. This matches the scope agreed in the PR thread after server-side testing showed the YAML/decimal changes risked a breaking change for draft-04 custom types and silently stored null for out-of-range values.

New inline comments: 0

I reviewed the changed lines in cmd/kosli/cli_utils.go (exactIntegers + LoadJsonData), cmd/kosli/cli_utils_test.go, and cmd/kosli/attestationData_test.go:

  • exactIntegers correctly recurses through []any/map[string]any, leaves integer json.Number literals untouched (exact, any size), converts everything else via Float64(), and surfaces strconv.ParseFloat's range error for 1e400 as "<file>: number 1e400 does not fit in a float64".
  • The new table tests directly cover the three scenarios raised in review (whole-number float against an implied integer type, a 30+ digit integer, and 1e400), and the expected outputs match what encoding/json actually produces (verified by inspection: 1.0→1, 1e2→100, negative/nested integers preserved, decimal precision via float64).
  • No correctness issues or regressions found in the changed lines.

Limitation: I could not run go build/go vet/make test_integration in this sandbox (command execution required approval that wasn't available). The analysis above is based on static reading of the diff and Go's documented encoding/json behavior.
· branch 20261202_json-number-precision

@mbevc1 mbevc1 changed the title fix: keep large numbers exact when loading JSON data files fix: keep large numbers exact when loading JSON and YAML data files Dec 2, 2026
@mbevc1
mbevc1 force-pushed the 20261202_json-number-precision branch from 722c844 to e4f05d1 Compare December 2, 2026 11:14
@mbevc1
mbevc1 marked this pull request as ready for review December 2, 2026 11:14
@mbevc1
mbevc1 requested a review from dangrondahl December 2, 2026 11:14
Comment thread internal/utils/yaml.go Resolved
Comment thread internal/utils/yaml.go Resolved
@dangrondahl

Copy link
Copy Markdown
Contributor

The CLI side looks right to me. My concern is what the server does with the new bytes, because the same file now produces a different payload.

Whole-number floats change type on the wire. 1.0 used to arrive as 1 and 1e2 as 100. Now they arrive as written. Does a custom attestation type with type: integer in its schema still accept {"n": 1.0}? If the server's validator separates ints from floats, existing types start rejecting data they accepted before this change. Same question for anything that stringifies, displays or diffs the stored value: 1.10 was 1.1.

Two client errors became server inputs.

  • A YAML integer beyond 64 bits was rejected with "quote it to send it as a string". It is now sent. Can the server store it?
  • A JSON 1e400 used to fail in json.Unmarshal. UseNumber passes it through as a number. In YAML the same value is still sent as a string, so the two formats now disagree.

If the server can't hold these, the user gets a server error where they used to get a clear CLI message.

The tests stop at json.Marshal, so none of this is covered. Could we add a test against the local server before merging?

  1. {"n": 1.0} against a type with an integer schema
  2. a 30-digit integer
  3. 1e400 from a JSON file

If 1 fails, this is a breaking change for existing custom types and should be labelled as one in the release notes. If 2 or 3 fail, I'd keep the client-side errors for those cases.

@mbevc1 mbevc1 changed the title fix: keep large numbers exact when loading JSON and YAML data files fix: keep large integers exact when loading JSON data files Dec 2, 2026
@mbevc1

mbevc1 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks Dan, you were right on 1 and 3, and the PR is now narrowed.

I ran your three cases through the server's API code at 1d6dd1f, with its unit test runner (etuk). That uses the in-memory Mongo stand-in, not a real database, and it is not committed as a test.

Case Result
{"n": 1.0}, schema type: integer, default draft 2020-12 201, stored and returned as 1.0 (was 1)
{"n": 1.0} or 1e2, same schema declaring draft-04 400 1.0 is not of type 'integer'
20-digit and 40-digit integers 201, returned exactly. attestation_data and user_data are written to Mongo as JSON text, so BSON's int64 limit does not apply
1e400 from a JSON file 201 with a permissive schema, stored as null; 400 None is not of type 'number' with type: number
0.12345678901234567890 201, returned as 0.12345678901234568

So 1 was a breaking change for draft-04 types, 3 silently stored null, and exact decimals gained nothing because the server reads them into a float64.

What the PR does now:

  • JSON integers are sent exactly as written, at any size.
  • Every other JSON number goes through float64 as before: 1.0 is sent as 1, 1e2 as 100, and 1e400 is a CLI error again, naming the file.
  • YAML is reverted to what is on main, including the "does not fit in 64 bits" error.

The only wire change left is JSON integers above 2^53, so I don't think this needs a breaking-change note.

Two things still open:

  • JSON now sends a 30-digit integer that YAML rejects. The server stores it, so YAML could send it too, but that needs the marker workaround I just reverted.
  • The server's jq evaluator still rounds integers above 2^53 (.n on 9007199254740993 gives 9007199254740992), so a rule comparing such a value sees the rounded number even though the stored one is exact.

LoadJsonData unmarshalled into any, so every number became a float64 and
an integer above 2^53 was sent as a different number, e.g.
12345678901234567890 became 12345678901234567000. Decoding with
UseNumber keeps each number as json.Number, which marshals back exactly
as written. This covers --user-data on the attest and trail commands and
JSON files given to attest custom --attestation-data.

Fixes #1430
A YAML file given to attest custom --attestation-data went through
yaml.v3, which decodes a number to an int64, a uint64 or a float64. A
decimal lost digits beyond float64 precision and its written form (1.10
was sent as 1.1), and an integer beyond 64 bits was rejected, while the
same numbers in a JSON file are now sent exactly.

yaml.v3 has no hook to decode a scalar into another type, and walking
the tree by hand would mean reimplementing its alias, merge key and
duplicate key handling. So a number node is turned into a string behind
a random marker before decoding, and turned into a json.Number after.
Spellings JSON does not have (+1.5, .5, 1., 1_000) are respelled without
changing a digit.

Also covers 1.0 and 1e2 in the JSON loader tests, as the review of the
JSON fix suggested.

Refs #1430
The YAMLDocuments godoc said decimals keep their digits whatever their
size, but yaml.v3 resolves a decimal beyond float64's range to a string,
so it is sent as one. It also left out that a leading zero is dropped
from a decimal such as 01.50 while 089 is an error. Tests pin a signed
exponent and the beyond-float64 integer.

Refs #1430
The server reads every decimal into a float64, so the extra digits were
dropped on arrival and exact YAML decimals gained nothing. They did
change the wire form of whole-number floats: 1.0 arrived as a float
where it used to arrive as the integer 1, which a custom attestation
type with a draft-04 schema and type integer rejects.

YAML integers within 64 bits were already exact, so the YAML loader goes
back to what it was, along with its documentation.

This reverts commits 4205006 and e4f05d1.

Refs #1430
Keeping every number as written changed the wire form of numbers that
were never wrong. 1.0 arrived at the server as a float where it used to
arrive as the integer 1, and a custom attestation type whose schema
declares draft-04 and type integer answers that with a 400. 1e400, which
the CLI used to reject, was sent and stored by the server as null.

The server reads every decimal into a float64 anyway, so exact decimals
gained nothing. It stores user data and attestation data as JSON text,
so an integer of any size survives.

So an integer stays as written and every other number goes through
float64 as before: 1.0 is sent as 1, 1e2 as 100, and a number beyond
float64 is an error that names the file.

Refs #1430
@mbevc1
mbevc1 force-pushed the 20261202_json-number-precision branch from 29572c2 to 2460afb Compare December 2, 2026 18:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LoadJsonData loses precision on large integers in JSON files

2 participants