Skip to content

Add omitempty to Node.ID #89

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 24, 2017
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
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ManyPayload struct {
// Node is used to represent a generic JSON API Resource
type Node struct {
Type string `json:"type"`
ID string `json:"id"`
ID string `json:"id,omitempty"`
ClientID string `json:"client-id,omitempty"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Relationships map[string]interface{} `json:"relationships,omitempty"`
Expand Down
26 changes: 26 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ func TestMarshalIDPtr(t *testing.T) {
}
}

func TestMarshalOnePayload_omitIDString(t *testing.T) {
type Foo struct {
ID string `jsonapi:"primary,foo"`
Title string `jsonapi:"attr,title"`
}

foo := &Foo{Title: "Foo"}
out := bytes.NewBuffer(nil)
if err := MarshalOnePayload(out, foo); err != nil {
t.Fatal(err)
}

var jsonData map[string]interface{}
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
t.Fatal(err)
}
payload := jsonData["data"].(map[string]interface{})

// Verify that empty ID of type string gets omitted. See:
// https://github.com/google/jsonapi/issues/83#issuecomment-285611425
_, ok := payload["id"]
if ok {
t.Fatal("Was expecting the data.id member to be omitted")
}
}

func TestMarshall_invalidIDType(t *testing.T) {
type badIDStruct struct {
ID *bool `jsonapi:"primary,cars"`
Expand Down