[FIX] Issue 176 (#199)

* fix(request): change the order of the resource data check to prioritise the type when unmarshalling
- fixes #176

* tests(request): update the TestUnmarshalPayload_ptrsAllNil test that broke

* Using %v instead of %s

* Added a test case to demonstrate that this library follows the spec: covered the case where the incoming request payload is missing the `type` field, an error is expected.

Co-authored-by: Aren Patel <git@arenpatel.com>
This commit is contained in:
Quetzy Garcia 2021-03-06 20:33:18 +00:00 committed by GitHub
parent 471426f0d9
commit b10ff4bf78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -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)

View File

@ -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 {