diff --git a/request.go b/request.go index a7bb0b1..b2fa477 100644 --- a/request.go +++ b/request.go @@ -175,10 +175,6 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) } if annotation == annotationPrimary { - if data.ID == "" { - continue - } - // Check the JSON API Type if data.Type != args[1] { er = fmt.Errorf( @@ -189,6 +185,10 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) break } + if data.ID == "" { + continue + } + // ID will have to be transmitted as astring per the JSON API spec v := reflect.ValueOf(data.ID) diff --git a/request_test.go b/request_test.go index 3326598..daa2159 100644 --- a/request_test.go +++ b/request_test.go @@ -70,11 +70,20 @@ func TestUnmarshalToStructWithPointerAttr(t *testing.T) { } } +func TestUnmarshalPayload_missingTypeFieldShouldError(t *testing.T) { + if err := UnmarshalPayload( + strings.NewReader(`{"data":{"body":"hello world"}}`), + &Post{}, + ); err == nil { + t.Fatalf("Expected an error but did not get one") + } +} + func TestUnmarshalPayload_ptrsAllNil(t *testing.T) { out := new(WithPointer) if err := UnmarshalPayload( - strings.NewReader(`{"data": {}}`), out); err != nil { - t.Fatalf("Error unmarshalling to Foo") + strings.NewReader(`{"data":{"type":"with-pointers"}}`), out); err != nil { + t.Fatalf("Error unmarshalling to Foo: %v", err) } if out.ID != nil {