Skip to content

Unmarshal json null relationship data to nil #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
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
1 change: 1 addition & 0 deletions jsonapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ var (
articleRelatedInvalidEmptyRelationshipBody = `{"data":{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{}}}}`
articleRelatedInvalidEmptyDataBody = `{"data":{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":{}}}}}`
articleRelatedNoOmitEmptyBody = `{"data":{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":null},"comments":{"data":[]}}}}`
articleRelatedLiteralNullData = `{"data":{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":null},"comments":{"data":null}}}}`
articleRelatedAuthorBody = `{"data":{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":{"id":"1","type":"author"},"links":{"self":"http://example.com/articles/1/relationships/author","related":"http://example.com/articles/1/author"}}}}}`
articleRelatedAuthorTwiceBody = `{"data":[{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":{"id":"1","type":"author"}}}},{"id":"2","type":"articles","attributes":{"title":"B"},"relationships":{"author":{"data":{"id":"1","type":"author"}}}}]}`
articleRelatedAuthorTwiceWithIncludeBody = `{"data":[{"id":"1","type":"articles","attributes":{"title":"A"},"relationships":{"author":{"data":{"id":"1","type":"author"}}}},{"id":"2","type":"articles","attributes":{"title":"B"},"relationships":{"author":{"data":{"id":"1","type":"author"}}}}],"included":[{"id":"1","type":"author","attributes":{"name":"A"}}]}`
Expand Down
7 changes: 7 additions & 0 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ func setFieldValue(fv reflect.Value, v any) {
}
fv.Set(vv)
}

func canBeNil(fv reflect.Value) bool {
return fv.Kind() == reflect.Interface ||
fv.Kind() == reflect.Map ||
fv.Kind() == reflect.Pointer ||
fv.Kind() == reflect.Slice
}
3 changes: 3 additions & 0 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ func (ro *resourceObject) unmarshalFields(v any, rv reflect.Value, rt reflect.Ty
}
if !relDocument.hasMany && relDocument.isEmpty() {
// ensure struct field is nil for data:null cases only (we want empty slice for data:[])
if canBeNil(fv) {
fv.Set(reflect.Zero(ft.Type))
}
continue
}

Expand Down
13 changes: 13 additions & 0 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,19 @@ func TestUnmarshal(t *testing.T) {
},
expect: &ArticleRelated{},
expectError: ErrEmptyDataObject,
}, {
description: "*ArticleRelated with nil relationships data",
given: articleRelatedLiteralNullData,
do: func(body []byte) (any, error) {
a := ArticleRelated{
Author: &Author{ID: "100"},
Comments: []*Comment{{ID: "200"}},
}
err := Unmarshal(body, &a)
return &a, err
},
expect: &ArticleRelated{ID: "1", Title: "A", Author: nil, Comments: nil},
expectError: nil,
}, {
description: "*ArticleRelated.Author",
given: articleRelatedAuthorBody,
Expand Down
Loading