Skip to content

Commit

Permalink
Bugfix issue #1169 (#1170)
Browse files Browse the repository at this point in the history
* Fixed integer precision loss via matching JSONs. It is caused by unmarshaling JSON that contains bigints to type interface{}. Now decoder.UseNumber() is used.

* Added tests with float type

---------

Co-authored-by: issinitsyn <[email protected]>
  • Loading branch information
VintaChlenix and issinitsyn authored Jan 20, 2025
1 parent bd9c181 commit 5443f2c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
9 changes: 7 additions & 2 deletions core/matching/matchers/json_match.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package matchers

import (
"bytes"
"encoding/json"
"reflect"
)
Expand All @@ -17,13 +18,17 @@ func JsonMatch(match interface{}, toMatch string) bool {
return true
}
var matchingObject interface{}
err := json.Unmarshal([]byte(matchString), &matchingObject)
d := json.NewDecoder(bytes.NewBuffer([]byte(matchString)))
d.UseNumber()
err := d.Decode(&matchingObject)
if err != nil {
return false
}

var toMatchObject interface{}
err = json.Unmarshal([]byte(toMatch), &toMatchObject)
d = json.NewDecoder(bytes.NewBuffer([]byte(toMatch)))
d.UseNumber()
err = d.Decode(&toMatchObject)
if err != nil {
return false
}
Expand Down
24 changes: 23 additions & 1 deletion core/matching/matchers/json_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,32 @@ func Test_JsonMatch_MatchesTrueWithUnminifiedJSONRootAsArray(t *testing.T) {
]`)).To(BeTrue())
}


func Test_JsonMatch_MatchesTrueWithJSONRootAsArray_WithDataInDifferentOrder(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`[{"minified":true, "json":true}]`, `[{"json":true,"minified":true}]`)).To(BeTrue())
}

func Test_JsonMatch_MatchesFalseWithJSON_WithInt64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990161}}`)).To(BeFalse())
}

func Test_JsonMatch_MatchesTrueWithJSON_WithInt64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990160}}`)).To(BeTrue())
}

func Test_JsonMatch_MatchesFalseWithJSON_WithFloat64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990161}}`)).To(BeFalse())
}

func Test_JsonMatch_MatchesTrueWithJSON_WithFloat64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990160}}`)).To(BeTrue())
}
9 changes: 7 additions & 2 deletions core/matching/matchers/json_partial_match.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package matchers

import (
"bytes"
"encoding/json"
)

Expand All @@ -14,8 +15,12 @@ func JsonPartialMatch(match interface{}, toMatch string) bool {
return false
}

err0 := json.Unmarshal([]byte(toMatch), &toMatchType)
err1 := json.Unmarshal([]byte(matchString), &expected)
d := json.NewDecoder(bytes.NewBuffer([]byte(toMatch)))
d.UseNumber()
err0 := d.Decode(&toMatchType)
d = json.NewDecoder(bytes.NewBuffer([]byte(matchString)))
d.UseNumber()
err1 := d.Decode(&expected)
if err0 != nil || err1 != nil {
return false
}
Expand Down
25 changes: 24 additions & 1 deletion core/matching/matchers/json_partial_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ func Test_JsonPartialMatch_MatchesTrueWithJSONRootAsArrayAgainstJSONRootAsArray(
}]`)).To(BeTrue())
}


func Test_JsonPartialMatch_MatchesFalseWithJSONRootAsArrayAgainstJSONRootAsArrayWithDifferentElement(t *testing.T) {
RegisterTestingT(t)

Expand All @@ -380,3 +379,27 @@ func Test_JsonPartialMatch_MatchesFalseWithJSONRootAsArrayAgainstJSONRootAsArray
"age": 400
}]`)).To(BeFalse())
}

func Test_JsonPartialMatch_MatchesFalseWithJSON_WithInt64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990161}}`)).To(BeFalse())
}

func Test_JsonPartialMatch_MatchesTrueWithJSON_WithInt64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990160}}`)).To(BeTrue())
}

func Test_JsonPartialMatch_MatchesFalseWithJSON_WithFloat64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990161}}`)).To(BeFalse())
}

func Test_JsonPartialMatch_MatchesTrueWithJSON_WithFloat64(t *testing.T) {
RegisterTestingT(t)

Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990160}}`)).To(BeTrue())
}

0 comments on commit 5443f2c

Please sign in to comment.