fix(constraint): preserve AddData integer precision during JSON round-trip#707
Open
fix(constraint): preserve AddData integer precision during JSON round-trip#707
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates Client.AddData to avoid integer precision loss when round-tripping referential/inventory data through JSON before writing to driver storage.
Changes:
- Replaced
encoding/jsonmarshal/unmarshal round-trip withopa/util.RoundTrip(UseNumber) to preserve numeric precision. - Removed the unused
encoding/jsonimport and addedgithub.com/open-policy-agent/opa/v1/util.
Show a summary per file
| File | Description |
|---|---|
constraint/pkg/client/client.go |
Uses OPA’s JSON round-trip helper to prevent large integer coercion to float64 during AddData processing. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Comment on lines
+542
to
+546
| // Round trip data to force untyped JSON, as drivers are not type-aware. | ||
| // Use OPA's JSON decoder to preserve numeric precision by decoding numbers | ||
| // as json.Number instead of float64. | ||
| var processedDataCpy interface{} = processedData | ||
| err = util.RoundTrip(&processedDataCpy) |
There was a problem hiding this comment.
The AddData round-trip behavior change is intended to preserve integer precision (>2^53) but there is no unit test here exercising a payload with a large integer and asserting it is not coerced to float64. Adding a focused test case (e.g., data containing an int64 just above 2^53) would prevent regressions and validate the motivation for using util.RoundTrip/UseNumber.
JaydipGabani
approved these changes
Apr 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Client.AddDatapreviously round-tripped referential/inventory data usingencoding/jsonMarshal/Unmarshal, which decodes numbers tofloat64and can lose precision for integers > 2^53.RoundTripOnWrite, so callers must preserve numeric precision when preparing data for driver storage to avoid policy-evaluation corruption.Description
encoding/jsonMarshal/Unmarshalround-trip inClient.AddDatawith OPA'sutil.RoundTripto preserve numeric precision by decoding numbers asjson.Number(UseNumber).encoding/jsonimport and addedgithub.com/open-policy-agent/opa/v1/utilto the imports inconstraint/pkg/client/client.go.inmem.OptRoundTripOnWrite(false)performance behavior is unchanged.Testing
go test ./pkg/client -run TestClient_AddData -count=1and the test passed (ok github.com/open-policy-agent/frameworks/constraint/pkg/client).go test ./pkg/clientand observed successful completion for the modified unit test(s).Codex Task