Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
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