Skip to content

fix: keep large numbers exact when loading JSON and YAML data files - #1434

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

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

Conversation

@mbevc1

@mbevc1 mbevc1 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Send numbers from JSON and YAML 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).

Numbers now keep their written form too: 1.0 stays 1.0 and 1e2 stays 1e2, where they were sent as 1 and 100. In a YAML file, an integer beyond 64 bits is now sent where it was rejected, and spellings JSON lacks are respelled with the same digits (+.50 becomes 0.50).

Closes #1430

Checklist

  • Docs are autogenerated from CLI help — make sure the help text reflects your changes (the attest custom help now says YAML decimal numbers keep every digit)
  • 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 or YAML 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 1m 25s —— View job


Follow-up review complete

  • Fetch previous review comments
  • Diff BEFORE..AFTER to see what changed
  • Assess status of each prior finding
  • Check new commits for introduced bugs/regressions
  • Post summary comment

Scope of new commit (42050068): docs-only — updates the YAMLDocuments godoc in internal/utils/yaml.go and adds two new cases to yaml_test.go. No production logic changed.

Status of prior findings:

  1. Leading-zero inconsistency between integers and decimals (yaml.go:142) — ✅ Resolved. The godoc now explicitly states the rule: "A leading zero is dropped only where a decimal point or an exponent rules out octal: 089 is an error." This matches the code path exactly (the integer-literal check at yaml.go:139 only fires for pure integer literals). Thread already marked resolved.
  2. "Whatever their size" overstated the float range (yaml.go:37) — ✅ Resolved. Wording now reads "Decimal numbers within float64's range keep every digit... decimal numbers beyond float64's range, are strings exactly as written," matching the suggested fix. A new test case (1 followed by 400 zeros → string as written) now pins this exact edge case. Thread already marked resolved.

New inline comments: 0 — this commit only touches comments and test data; no bug, security issue, or regression surfaced in the changed lines.

Note: I was unable to execute go test/go build in this sandbox (commands required approval that wasn't available), so verification of the new test case was done by static inspection of jsonNumber/prepareScalar rather than by running the suite.

@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
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
@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
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
@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.

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