Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 6c4fbec

Browse files
committed
Merge branch 'master' into aren55555/feature/simplifiy-marshal-api
# Conflicts: # response_test.go
2 parents 40560af + ebb7923 commit 6c4fbec

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func ListBlogs(w http.ResponseWriter, r *http.Request) {
291291
w.Header().Set("Content-Type", jsonapi.MediaType)
292292
w.WriteHeader(http.StatusOK)
293293

294-
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
294+
if err := jsonapi.MarshalPayload(w, blogs); err != nil {
295295
http.Error(w, err.Error(), http.StatusInternalServerError)
296296
}
297297
}
@@ -329,7 +329,7 @@ func CreateBlogs(w http.ResponseWriter, r *http.Request) {
329329
w.Header().Set("Content-Type", jsonapi.MediaType)
330330
w.WriteHeader(http.StatusCreated)
331331

332-
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
332+
if err := jsonapi.MarshalPayload(w, blogs); err != nil {
333333
http.Error(w, err.Error(), http.StatusInternalServerError)
334334
}
335335
}

examples/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func exerciseHandler() {
103103
fixtureBlogCreate(3),
104104
}
105105
in = bytes.NewBuffer(nil)
106-
jsonapi.MarshalManyPayload(in, blogs)
106+
jsonapi.MarshalPayload(in, blogs)
107107

108108
req, _ = http.NewRequest(http.MethodPut, "/blogs", in)
109109

examples/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (h *ExampleHandler) echoBlogs(w http.ResponseWriter, r *http.Request) {
7070

7171
w.WriteHeader(http.StatusOK)
7272
w.Header().Set(headerContentType, jsonapi.MediaType)
73-
if err := jsonapiRuntime.MarshalManyPayload(w, blogs); err != nil {
73+
if err := jsonapiRuntime.MarshalPayload(w, blogs); err != nil {
7474
http.Error(w, err.Error(), http.StatusInternalServerError)
7575
}
7676
}

examples/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestExampleHandler_put(t *testing.T) {
3636
fixtureBlogCreate(3),
3737
}
3838
requestBody := bytes.NewBuffer(nil)
39-
jsonapi.MarshalManyPayload(requestBody, blogs)
39+
jsonapi.MarshalPayload(requestBody, blogs)
4040

4141
r, err := http.NewRequest(http.MethodPut, "/blogs", requestBody)
4242
if err != nil {

node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (p *ManyPayload) clearIncluded() {
3636
// Node is used to represent a generic JSON API Resource
3737
type Node struct {
3838
Type string `json:"type"`
39-
ID string `json:"id"`
39+
ID string `json:"id,omitempty"`
4040
ClientID string `json:"client-id,omitempty"`
4141
Attributes map[string]interface{} `json:"attributes,omitempty"`
4242
Relationships map[string]interface{} `json:"relationships,omitempty"`

response_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,33 @@ func TestMarshalIDPtr(t *testing.T) {
224224
}
225225
}
226226

227-
func TestMarshal_invalidIDType(t *testing.T) {
227+
func TestMarshalOnePayload_omitIDString(t *testing.T) {
228+
type Foo struct {
229+
ID string `jsonapi:"primary,foo"`
230+
Title string `jsonapi:"attr,title"`
231+
}
232+
233+
foo := &Foo{Title: "Foo"}
234+
out := bytes.NewBuffer(nil)
235+
if err := MarshalPayload(out, foo); err != nil {
236+
t.Fatal(err)
237+
}
238+
239+
var jsonData map[string]interface{}
240+
if err := json.Unmarshal(out.Bytes(), &jsonData); err != nil {
241+
t.Fatal(err)
242+
}
243+
payload := jsonData["data"].(map[string]interface{})
244+
245+
// Verify that empty ID of type string gets omitted. See:
246+
// https://github.com/google/jsonapi/issues/83#issuecomment-285611425
247+
_, ok := payload["id"]
248+
if ok {
249+
t.Fatal("Was expecting the data.id member to be omitted")
250+
}
251+
}
252+
253+
func TestMarshall_invalidIDType(t *testing.T) {
228254
type badIDStruct struct {
229255
ID *bool `jsonapi:"primary,cars"`
230256
}

0 commit comments

Comments
 (0)