Skip to content

Commit b28beab

Browse files
committed
Add TestUnmarshalNestedStructPtr
1 parent 0400041 commit b28beab

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

request_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,52 @@ func sampleSerializedEmbeddedTestModel() *Blog {
946946
return blog
947947
}
948948

949+
func TestUnmarshalNestedStructPtr(t *testing.T) {
950+
type Director struct {
951+
Firstname string `json:"firstname"`
952+
Surname string `json:"surname"`
953+
}
954+
type Movie struct {
955+
ID string `jsonapi:"primary,movies"`
956+
Name string `jsonapi:"attr,name"`
957+
Director *Director `jsonapi:"attr,director"`
958+
}
959+
sample := map[string]interface{}{
960+
"data": map[string]interface{}{
961+
"type": "movies",
962+
"id": "123",
963+
"attributes": map[string]interface{}{
964+
"name": "The Shawshank Redemption",
965+
"director": map[string]interface{}{
966+
"firstname": "Frank",
967+
"surname": "Darabont",
968+
},
969+
},
970+
},
971+
}
972+
973+
data, err := json.Marshal(sample)
974+
if err != nil {
975+
t.Fatal(err)
976+
}
977+
in := bytes.NewReader(data)
978+
out := new(Movie)
979+
980+
if err := UnmarshalPayload(in, out); err != nil {
981+
t.Fatal(err)
982+
}
983+
984+
if out.Name != "The Shawshank Redemption" {
985+
t.Fatalf("expected out.Name to be `The Shawshank Redemption`, but got `%s`", out.Name)
986+
}
987+
if out.Director.Firstname != "Frank" {
988+
t.Fatalf("expected out.Director.Firstname to be `Frank`, but got `%s`", out.Director.Firstname)
989+
}
990+
if out.Director.Surname != "Darabont" {
991+
t.Fatalf("expected out.Director.Surname to be `Darabont`, but got `%s`", out.Director.Surname)
992+
}
993+
}
994+
949995
func TestUnmarshalNestedStruct(t *testing.T) {
950996

951997
boss := map[string]interface{}{

0 commit comments

Comments
 (0)