Skip to content

Commit

Permalink
Pass clientMode when creating a relationship marshaler (#50)
Browse files Browse the repository at this point in the history
If `clientMode` is enabled on a marshaler then any `relationshipMarshaler` types that are created from it should inherit the `clientMode` setting

This allows client mode to work with struct types that use nested relationships
  • Loading branch information
kevinconaway authored Feb 6, 2024
1 parent e241740 commit 3a88ca9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (m *Marshaler) relationshipMarshaler(link *Link) *Marshaler {

rm.memberNameValidationMode = m.memberNameValidationMode
rm.link = link
rm.clientMode = m.clientMode
return rm
}

Expand Down
15 changes: 15 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ func TestMarshalRelationships(t *testing.T) {
func TestMarshalClientMode(t *testing.T) {
t.Parallel()

articleNoIDRelatedComplexBody := `{"data":{"type":"articles","attributes":{"title":"Bazel 101"},"relationships":{"author":{"data":{"id":"1","type":"author"},"links":{"self":"http://example.com/articles//relationships/author","related":"http://example.com/articles//author"}},"comments":{"data":[{"type":"comments"},{"type":"comments"},{"type":"comments"}],"links":{"self":"http://example.com/articles//relationships/comments","related":"http://example.com/articles//comments"}}}}}`

tests := []struct {
description string
given any
Expand All @@ -660,6 +662,19 @@ func TestMarshalClientMode(t *testing.T) {
given: &Article{ID: "", Title: "A"},
expect: articleANoIDBody,
},
{
description: "empty primary field with relationships",
given: &ArticleRelated{
Title: "Bazel 101",
Author: &authorA,
Comments: []*Comment{
{Body: "Why is Bazel so slow on my computerr?", Archived: true, Author: &authorBWithMeta},
{Body: "Why is Bazel so slow on my computer?", Author: &authorBWithMeta},
{Body: "Just use an Apple M1", Author: &authorA},
},
},
expect: articleNoIDRelatedComplexBody,
},
}

for i, tc := range tests {
Expand Down

0 comments on commit 3a88ca9

Please sign in to comment.