diff --git a/request.go b/request.go index af300a2c..f70e9628 100644 --- a/request.go +++ b/request.go @@ -317,6 +317,17 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) continue } + if fieldValue.Type() == reflect.TypeOf([]int(nil)) { + values := make([]int, v.Len()) + for i := 0; i < v.Len(); i++ { + values[i] = int(v.Index(i).Interface().(float64)) + } + + fieldValue.Set(reflect.ValueOf(values)) + + continue + } + if fieldValue.Type() == reflect.TypeOf(new(time.Time)) { if iso8601 { var tm string diff --git a/request_test.go b/request_test.go index 4fcb3b6c..fdb182c5 100644 --- a/request_test.go +++ b/request_test.go @@ -248,6 +248,21 @@ func TestUnmarshalSetsAttrs(t *testing.T) { } } +func TestUnmarshalParsesIntArrayAttr(t *testing.T) { + out, err := unmarshalSamplePayloadJSON() + if err != nil { + t.Fatal(err) + } + + if out.BookmarkedPages == nil { + t.Fatalf("Did not parse bookmarked pages") + } + + if len(out.BookmarkedPages) != 3 { + t.Fatalf("Bookmarked pages not properly serialized") + } +} + func TestUnmarshalParsesISO8601(t *testing.T) { payload := &OnePayload{ Data: &Node{ @@ -559,6 +574,17 @@ func unmarshalSamplePayload() (*Blog, error) { return out, nil } +func unmarshalSamplePayloadJSON() (*Blog, error) { + in := sampleJSONPayload() + out := new(Blog) + + if err := UnmarshalPayload(in, out); err != nil { + return nil, err + } + + return out, nil +} + func TestUnmarshalManyPayload(t *testing.T) { sample := map[string]interface{}{ "data": []interface{}{ @@ -725,6 +751,22 @@ func payload(data map[string]interface{}) (result []byte, err error) { return } +func sampleJSONPayload() io.Reader { + response := `{ + "data": { + "type": "blogs", + "attributes": { + "title": "New blog", + "created_at": 1436216820, + "view_count": 1000, + "bookmarked_pages": [ 100, 200, 30 ] + } + } + }` + + return bytes.NewReader([]byte(response)) +} + func samplePayload() io.Reader { payload := &OnePayload{ Data: &Node{ diff --git a/response_test.go b/response_test.go index 76821cdd..5b6d1d96 100644 --- a/response_test.go +++ b/response_test.go @@ -10,14 +10,15 @@ import ( ) type Blog struct { - ID int `jsonapi:"primary,blogs"` - ClientID string `jsonapi:"client-id"` - Title string `jsonapi:"attr,title"` - Posts []*Post `jsonapi:"relation,posts"` - CurrentPost *Post `jsonapi:"relation,current_post"` - CurrentPostID int `jsonapi:"attr,current_post_id"` - CreatedAt time.Time `jsonapi:"attr,created_at"` - ViewCount int `jsonapi:"attr,view_count"` + ID int `jsonapi:"primary,blogs"` + ClientID string `jsonapi:"client-id"` + Title string `jsonapi:"attr,title"` + Posts []*Post `jsonapi:"relation,posts"` + CurrentPost *Post `jsonapi:"relation,current_post"` + CurrentPostID int `jsonapi:"attr,current_post_id"` + CreatedAt time.Time `jsonapi:"attr,created_at"` + ViewCount int `jsonapi:"attr,view_count"` + BookmarkedPages []int `jsonapi:"attr,bookmarked_pages"` } func (b *Blog) JSONAPILinks() *Links { @@ -342,9 +343,10 @@ func TestHasPrimaryAnnotation(t *testing.T) { func TestSupportsAttributes(t *testing.T) { testModel := &Blog{ - ID: 5, - Title: "Title 1", - CreatedAt: time.Now(), + ID: 5, + Title: "Title 1", + CreatedAt: time.Now(), + BookmarkedPages: []int{20, 30}, } out := bytes.NewBuffer(nil) @@ -366,6 +368,10 @@ func TestSupportsAttributes(t *testing.T) { if data.Attributes["title"] != "Title 1" { t.Fatalf("Attributes hash not populated using tags correctly") } + + if data.Attributes["bookmarked_pages"] == nil { + t.Fatalf("[]int attribute not serialized correctly") + } } func TestOmitsZeroTimes(t *testing.T) {