From ffd640e1fa88161d0e5cb8e91b7817fb4d46ef2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serta=C3=A7=20=C3=96zercan?= <852750+sozercan@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:39:46 -0700 Subject: [PATCH] fix(constraint): preserve numeric precision in AddData round-trip Signed-off-by: Sertac Ozercan --- constraint/pkg/client/client.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/constraint/pkg/client/client.go b/constraint/pkg/client/client.go index 522333ca6..cc06a8a57 100644 --- a/constraint/pkg/client/client.go +++ b/constraint/pkg/client/client.go @@ -3,7 +3,6 @@ package client import ( "context" - "encoding/json" "fmt" "sort" "strings" @@ -19,6 +18,7 @@ import ( "github.com/open-policy-agent/frameworks/constraint/pkg/handler" "github.com/open-policy-agent/frameworks/constraint/pkg/instrumentation" "github.com/open-policy-agent/frameworks/constraint/pkg/types" + "github.com/open-policy-agent/opa/v1/util" admissionv1 "k8s.io/api/admission/v1" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -539,15 +539,11 @@ func (c *Client) AddData(ctx context.Context, data interface{}) (*types.Response } } - // Round trip data to force untyped JSON, as drivers are not type-aware - bytes, err := json.Marshal(processedData) - if err != nil { - errMap[name] = err - - continue - } - var processedDataCpy interface{} - err = json.Unmarshal(bytes, &processedDataCpy) + // 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) if err != nil { errMap[name] = err