From 30058741d3f66e8d46983d14b41800426df52791 Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Fri, 29 Nov 2024 16:07:38 +0530 Subject: [PATCH 1/3] bugfix - resolve issue #1155 --- core/util/util.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/core/util/util.go b/core/util/util.go index 33bfe2aa3..3f475f1e7 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -399,14 +399,14 @@ func jsonPath(query, toMatch string) interface{} { return "" } - // Jsonpath library converts large int into a string with scientific notion, the following - // reverts that process to avoid mismatching when using the jsonpath result for csv data lookup - floatResult, err := strconv.ParseFloat(result, 64) - // if the string is a float and a whole number - if err == nil && floatResult == float64(int64(floatResult)) { - intResult := int(floatResult) - result = strconv.Itoa(intResult) - } + //// Jsonpath library converts large int into a string with scientific notion, the following + //// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup + //floatResult, err := strconv.ParseFloat(result, 64) + //// if the string is a float and a whole number + //if err == nil && floatResult == float64(int64(floatResult)) { + // intResult := int(floatResult) + // result = strconv.Itoa(intResult) + //} // convert to array data if applicable var data interface{} From 0580133b65876374a8258043961de6613f44dd03 Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Sat, 30 Nov 2024 16:42:24 +0530 Subject: [PATCH 2/3] handling for big int --- core/util/util.go | 15 +++++++-------- core/util/util_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core/util/util.go b/core/util/util.go index 3f475f1e7..82e175f11 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -14,6 +14,7 @@ import ( log "github.com/sirupsen/logrus" "io/ioutil" "k8s.io/client-go/util/jsonpath" + "math/big" "math/rand" "net/http" "net/url" @@ -399,14 +400,12 @@ func jsonPath(query, toMatch string) interface{} { return "" } - //// Jsonpath library converts large int into a string with scientific notion, the following - //// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup - //floatResult, err := strconv.ParseFloat(result, 64) - //// if the string is a float and a whole number - //if err == nil && floatResult == float64(int64(floatResult)) { - // intResult := int(floatResult) - // result = strconv.Itoa(intResult) - //} + // Jsonpath library converts large int into a string with scientific notion, the following + // reverts that process to avoid mismatching when using the jsonpath result for csv data lookup + bigInt := new(big.Int) + if _, ok := bigInt.SetString(result, 10); ok { + result = fmt.Sprint(bigInt) + } // convert to array data if applicable var data interface{} diff --git a/core/util/util_test.go b/core/util/util_test.go index ddc888014..645003643 100644 --- a/core/util/util_test.go +++ b/core/util/util_test.go @@ -234,6 +234,22 @@ func Test_CopyMap(t *testing.T) { Expect(newMap["second"]).To(Equal("2")) } +func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) { + + RegisterTestingT(t) + res := jsonPath(PrepareJsonPathQuery("$.registrant"), + `{"registrant":"13495985898986869898697879879987978978.12345566777"}`) + Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777")) +} + +func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) { + + RegisterTestingT(t) + res := jsonPath(PrepareJsonPathQuery("$.registrant"), + `{"registrant":"13495985898986869898697879879987978978"}`) + Expect(res).To(Equal("13495985898986869898697879879987978978")) +} + func Test_Identical_ReturnsTrue_WithExactlySameArray(t *testing.T) { RegisterTestingT(t) first := [2]string{"q1", "q2"} From d7e13e0d6661239cc641567898d34742b7f18cb9 Mon Sep 17 00:00:00 2001 From: kapishmalik Date: Sat, 30 Nov 2024 17:17:14 +0530 Subject: [PATCH 3/3] better version of fix --- core/util/util.go | 32 +++++++++++++++++++++++++++++--- core/util/util_test.go | 15 ++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/core/util/util.go b/core/util/util.go index 82e175f11..661710a3d 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -402,9 +402,14 @@ func jsonPath(query, toMatch string) interface{} { // Jsonpath library converts large int into a string with scientific notion, the following // reverts that process to avoid mismatching when using the jsonpath result for csv data lookup - bigInt := new(big.Int) - if _, ok := bigInt.SetString(result, 10); ok { - result = fmt.Sprint(bigInt) + // Handle large integers in scientific notation by converting back to big.Int + if isScientific(result) { + // If result is in scientific notation, try converting to a big.Int + bigInt := new(big.Int) + bigInt, success := bigIntFromString(result) + if success { + result = bigInt.String() // Convert back to string representation of the big integer + } } // convert to array data if applicable @@ -419,6 +424,27 @@ func jsonPath(query, toMatch string) interface{} { return arrayData } +// isScientific checks if a string is in scientific notation (e.g., "1.349599e+37") +func isScientific(value string) bool { + return strings.Contains(value, "e") || strings.Contains(value, "E") +} + +// bigIntFromString converts a string representing a number (potentially in scientific notation) to big.Int +func bigIntFromString(value string) (*big.Int, bool) { + // Parse the string as a big.Float to handle scientific notation + flt := new(big.Float) + flt, _, err := big.ParseFloat(value, 10, 0, big.ToNearestEven) + if err != nil { + return nil, false + } + + // Convert the big.Float to big.Int (rounding down) + bigInt := new(big.Int) + flt.Int(bigInt) + + return bigInt, true +} + func xPath(query, toMatch string) string { result, err := XpathExecution(query, toMatch) if err != nil { diff --git a/core/util/util_test.go b/core/util/util_test.go index 645003643..24f399676 100644 --- a/core/util/util_test.go +++ b/core/util/util_test.go @@ -237,17 +237,22 @@ func Test_CopyMap(t *testing.T) { func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) { RegisterTestingT(t) - res := jsonPath(PrepareJsonPathQuery("$.registrant"), - `{"registrant":"13495985898986869898697879879987978978.12345566777"}`) + res := jsonPath("$.registrant", `{"registrant":"13495985898986869898697879879987978978.12345566777"}`) Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777")) } func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) { RegisterTestingT(t) - res := jsonPath(PrepareJsonPathQuery("$.registrant"), - `{"registrant":"13495985898986869898697879879987978978"}`) - Expect(res).To(Equal("13495985898986869898697879879987978978")) + res := jsonPath("$.registrant", `{"registrant":5553686208582}`) + Expect(res).To(Equal(5553686208582)) +} + +func Test_JsonPathMethod_WithWordContainingEe(t *testing.T) { + + RegisterTestingT(t) + res := jsonPath("$.registrant", `{"registrant":"ETest"}`) + Expect(res).To(Equal("ETest")) } func Test_Identical_ReturnsTrue_WithExactlySameArray(t *testing.T) {