Skip to content

Commit 8b1bdce

Browse files
committed
test(json): add test case for #63
1 parent 936b01c commit 8b1bdce

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

json/json_test.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,6 @@ func TestGithubIssue41(t *testing.T) {
15781578
"expected: ", expectedString,
15791579
)
15801580
}
1581-
15821581
}
15831582

15841583
func TestGithubIssue44(t *testing.T) {
@@ -1602,6 +1601,40 @@ func (r *rawJsonString) UnmarshalJSON(b []byte) error {
16021601
return nil
16031602
}
16041603

1604+
// See https://github.com/segmentio/encoding/issues/63
1605+
// In short, embedding a struct pointer resulted in an incorrect memory address
1606+
// as we were still looking to the parent struct to start and only using offsets
1607+
// which resulted in the wrong values being extracted.
1608+
func TestGithubIssue63(t *testing.T) {
1609+
expectedString := `{"my_field":"test","my_other_field":"testing","code":1}`
1610+
1611+
type MyStruct struct {
1612+
MyField string `json:"my_field,omitempty"`
1613+
MyOtherField string `json:"my_other_field"`
1614+
MyEmptyField string `json:"my_empty_field,omitempty"`
1615+
}
1616+
1617+
type MyStruct2 struct {
1618+
*MyStruct
1619+
Code int `json:"code"`
1620+
}
1621+
1622+
input := MyStruct2{
1623+
MyStruct: &MyStruct{
1624+
MyField: "test",
1625+
MyOtherField: "testing",
1626+
},
1627+
Code: 1,
1628+
}
1629+
1630+
if b, err := Marshal(input); err != nil {
1631+
t.Error(err)
1632+
} else if string(b) != expectedString {
1633+
t.Errorf("got: %s", string(b))
1634+
t.Errorf("expected: %s", expectedString)
1635+
}
1636+
}
1637+
16051638
func TestSetTrustRawMessage(t *testing.T) {
16061639
buf := &bytes.Buffer{}
16071640
enc := NewEncoder(buf)

0 commit comments

Comments
 (0)