diff --git a/request_test.go b/request_test.go index a74e337..0179150 100644 --- a/request_test.go +++ b/request_test.go @@ -26,18 +26,18 @@ func TestMalformedTag(t *testing.T) { } } -//func TestUnmarshalSetsId(t *testing.T) { -//in := samplePayload() -//out := new(Blog) +func TestUnmarshalSetsId(t *testing.T) { + in := samplePayloadWithId() + out := new(Blog) -//if err := UnmarshalJsonApiPayload(in, out); err != nil { -//t.Fatal(err) -//} + if err := UnmarshalJsonApiPayload(in, out); err != nil { + t.Fatal(err) + } -//if out.Id != 0 { -//t.Fatalf("Did not set Id on dst interface") -//} -//} + if out.Id != 2 { + t.Fatalf("Did not set Id on dst interface") + } +} func TestUnmarshalSetsAttrs(t *testing.T) { out, err := unmarshalSamplePayload() @@ -173,3 +173,23 @@ func samplePayload() io.Reader { return out } + +func samplePayloadWithId() io.Reader { + payload := &JsonApiOnePayload{ + Data: &JsonApiNode{ + Id: "2", + Type: "blogs", + Attributes: map[string]interface{}{ + "title": "New blog", + "created_at": 1436216820, + "view_count": 1000, + }, + }, + } + + out := bytes.NewBuffer(nil) + + json.NewEncoder(out).Encode(payload) + + return out +} diff --git a/response.go b/response.go index 4f9ad60..8d861df 100644 --- a/response.go +++ b/response.go @@ -82,6 +82,10 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) { tag := structField.Tag.Get("jsonapi") + if tag == "" { + return false + } + args := strings.Split(tag, ",") if len(args) != 2 {