Skip to content

fix(constraint): preserve AddData integer precision during JSON round-trip#707

Open
sozercan wants to merge 1 commit intomasterfrom
codex/fix-precision-loss-in-inventory-data
Open

fix(constraint): preserve AddData integer precision during JSON round-trip#707
sozercan wants to merge 1 commit intomasterfrom
codex/fix-precision-loss-in-inventory-data

Conversation

@sozercan
Copy link
Copy Markdown
Member

@sozercan sozercan commented Apr 9, 2026

Motivation

  • Client.AddData previously round-tripped referential/inventory data using encoding/json Marshal/Unmarshal, which decodes numbers to float64 and can lose precision for integers > 2^53.
  • The local Rego driver disables in‑memory RoundTripOnWrite, so callers must preserve numeric precision when preparing data for driver storage to avoid policy-evaluation corruption.

Description

  • Replaced the encoding/json Marshal/Unmarshal round-trip in Client.AddData with OPA's util.RoundTrip to preserve numeric precision by decoding numbers as json.Number (UseNumber).
  • Removed the unused encoding/json import and added github.com/open-policy-agent/opa/v1/util to the imports in constraint/pkg/client/client.go.
  • The change is minimal and localized to the data round-trip step so untyped JSON coercion for drivers is preserved while avoiding float64 precision loss; the inmem.OptRoundTripOnWrite(false) performance behavior is unchanged.

Testing

  • Ran go test ./pkg/client -run TestClient_AddData -count=1 and the test passed (ok github.com/open-policy-agent/frameworks/constraint/pkg/client).
  • Built and ran the client package tests locally with go test ./pkg/client and observed successful completion for the modified unit test(s).

Codex Task

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/json marshal/unmarshal round-trip with opa/util.RoundTrip (UseNumber) to preserve numeric precision.
  • Removed the unused encoding/json import and added github.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)
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants