Skip to content

Commit 5443f2c

Browse files
VintaChlenixissinitsyn
and
issinitsyn
authored
Bugfix issue #1169 (#1170)
* 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]>
1 parent bd9c181 commit 5443f2c

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

core/matching/matchers/json_match.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package matchers
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"reflect"
67
)
@@ -17,13 +18,17 @@ func JsonMatch(match interface{}, toMatch string) bool {
1718
return true
1819
}
1920
var matchingObject interface{}
20-
err := json.Unmarshal([]byte(matchString), &matchingObject)
21+
d := json.NewDecoder(bytes.NewBuffer([]byte(matchString)))
22+
d.UseNumber()
23+
err := d.Decode(&matchingObject)
2124
if err != nil {
2225
return false
2326
}
2427

2528
var toMatchObject interface{}
26-
err = json.Unmarshal([]byte(toMatch), &toMatchObject)
29+
d = json.NewDecoder(bytes.NewBuffer([]byte(toMatch)))
30+
d.UseNumber()
31+
err = d.Decode(&toMatchObject)
2732
if err != nil {
2833
return false
2934
}

core/matching/matchers/json_match_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,32 @@ func Test_JsonMatch_MatchesTrueWithUnminifiedJSONRootAsArray(t *testing.T) {
104104
]`)).To(BeTrue())
105105
}
106106

107-
108107
func Test_JsonMatch_MatchesTrueWithJSONRootAsArray_WithDataInDifferentOrder(t *testing.T) {
109108
RegisterTestingT(t)
110109

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

113+
func Test_JsonMatch_MatchesFalseWithJSON_WithInt64(t *testing.T) {
114+
RegisterTestingT(t)
115+
116+
Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990161}}`)).To(BeFalse())
117+
}
118+
119+
func Test_JsonMatch_MatchesTrueWithJSON_WithInt64(t *testing.T) {
120+
RegisterTestingT(t)
121+
122+
Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990160}}`)).To(BeTrue())
123+
}
124+
125+
func Test_JsonMatch_MatchesFalseWithJSON_WithFloat64(t *testing.T) {
126+
RegisterTestingT(t)
127+
128+
Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990161}}`)).To(BeFalse())
129+
}
130+
131+
func Test_JsonMatch_MatchesTrueWithJSON_WithFloat64(t *testing.T) {
132+
RegisterTestingT(t)
133+
134+
Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990160}}`)).To(BeTrue())
135+
}

core/matching/matchers/json_partial_match.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package matchers
22

33
import (
4+
"bytes"
45
"encoding/json"
56
)
67

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

17-
err0 := json.Unmarshal([]byte(toMatch), &toMatchType)
18-
err1 := json.Unmarshal([]byte(matchString), &expected)
18+
d := json.NewDecoder(bytes.NewBuffer([]byte(toMatch)))
19+
d.UseNumber()
20+
err0 := d.Decode(&toMatchType)
21+
d = json.NewDecoder(bytes.NewBuffer([]byte(matchString)))
22+
d.UseNumber()
23+
err1 := d.Decode(&expected)
1924
if err0 != nil || err1 != nil {
2025
return false
2126
}

core/matching/matchers/json_partial_match_test.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ func Test_JsonPartialMatch_MatchesTrueWithJSONRootAsArrayAgainstJSONRootAsArray(
361361
}]`)).To(BeTrue())
362362
}
363363

364-
365364
func Test_JsonPartialMatch_MatchesFalseWithJSONRootAsArrayAgainstJSONRootAsArrayWithDifferentElement(t *testing.T) {
366365
RegisterTestingT(t)
367366

@@ -380,3 +379,27 @@ func Test_JsonPartialMatch_MatchesFalseWithJSONRootAsArrayAgainstJSONRootAsArray
380379
"age": 400
381380
}]`)).To(BeFalse())
382381
}
382+
383+
func Test_JsonPartialMatch_MatchesFalseWithJSON_WithInt64(t *testing.T) {
384+
RegisterTestingT(t)
385+
386+
Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990161}}`)).To(BeFalse())
387+
}
388+
389+
func Test_JsonPartialMatch_MatchesTrueWithJSON_WithInt64(t *testing.T) {
390+
RegisterTestingT(t)
391+
392+
Expect(matchers.JsonMatch(`{"test":{"id":112769992360719990160}}`, `{"test":{"id":112769992360719990160}}`)).To(BeTrue())
393+
}
394+
395+
func Test_JsonPartialMatch_MatchesFalseWithJSON_WithFloat64(t *testing.T) {
396+
RegisterTestingT(t)
397+
398+
Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990161}}`)).To(BeFalse())
399+
}
400+
401+
func Test_JsonPartialMatch_MatchesTrueWithJSON_WithFloat64(t *testing.T) {
402+
RegisterTestingT(t)
403+
404+
Expect(matchers.JsonMatch(`{"test":{"id":11.2769992360719990160}}`, `{"test":{"id":11.2769992360719990160}}`)).To(BeTrue())
405+
}

0 commit comments

Comments
 (0)