Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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{
Expand Down
28 changes: 17 additions & 11 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Copy link
Copy Markdown
Contributor

@aren55555 aren55555 Feb 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this work for serialization/marshaling? ie did it appear in the json payload as expected?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this serializes correctly. I just pushed an update including a test for this.

}

func (b *Blog) JSONAPILinks() *Links {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down