From 3941fb57a2ccaa18149fe97c8007d6e470efdbc5 Mon Sep 17 00:00:00 2001 From: Josh Brandoff Date: Fri, 12 Jun 2015 22:14:12 -0400 Subject: [PATCH 1/5] Added support for JSON-API content-type. --- render/json.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/render/json.go b/render/json.go index f28cd1f8..82a2c5d7 100644 --- a/render/json.go +++ b/render/json.go @@ -13,18 +13,29 @@ type ( JSON struct { Data interface{} } + + JSONAPI struct { + Data interface{} + } IndentedJSON struct { Data interface{} } ) -var jsonContentType = []string{"application/json; charset=utf-8"} +var ( + jsonContentType = []string{"application/json; charset=utf-8"} + jsonAPIContentType = []string{"application/vnd.api+json"} +) func (r JSON) Render(w http.ResponseWriter) error { return WriteJSON(w, r.Data) } +func (r JSONAPI) Render(w http.ResponseWriter) error { + return WriteJSONAPI(w, r.Data) +} + func (r IndentedJSON) Render(w http.ResponseWriter) error { w.Header()["Content-Type"] = jsonContentType jsonBytes, err := json.MarshalIndent(r.Data, "", " ") @@ -39,3 +50,8 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error { w.Header()["Content-Type"] = jsonContentType return json.NewEncoder(w).Encode(obj) } + +func WriteJSONAPI(w http.ResponseWrite, obj interface{}) error { + w.Header()["Content-Type"] = jsonAPIContentType + return json.NewEncode(w).Encode(obj) +} From 5e67d3ec7475cac752dbbdf3a8c29c04d1b48673 Mon Sep 17 00:00:00 2001 From: Josh Brandoff Date: Fri, 12 Jun 2015 22:15:16 -0400 Subject: [PATCH 2/5] Added unit test for JSONAPI modifications. --- render/render_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/render/render_test.go b/render/render_test.go index 7a6ffb7d..fc2623f0 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -29,6 +29,19 @@ func TestRenderJSON(t *testing.T) { assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8") } +func TestRenderJSONAPI(t *testing.T) { + w := httptest.NewRecorder() + data := map[string]interface{}{ + "foo": "bar", + } + + err := (JSONAPI{data}).Render(w) + + assert.NoError(t, err) + assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n") + assert.Equal(t, w.Header().Get("Content-Type"), "application/vnd.api+json") +} + func TestRenderIndentedJSON(t *testing.T) { w := httptest.NewRecorder() data := map[string]interface{}{ From 00f1c2af30ee48fcf123e6632fc77550f27ea785 Mon Sep 17 00:00:00 2001 From: Josh Brandoff Date: Fri, 12 Jun 2015 22:20:11 -0400 Subject: [PATCH 3/5] Fixing missing 's'... --- render/json.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/render/json.go b/render/json.go index 82a2c5d7..47716f48 100644 --- a/render/json.go +++ b/render/json.go @@ -51,7 +51,7 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error { return json.NewEncoder(w).Encode(obj) } -func WriteJSONAPI(w http.ResponseWrite, obj interface{}) error { +func WriteJSONAPI(w http.ResponseWriter, obj interface{}) error { w.Header()["Content-Type"] = jsonAPIContentType - return json.NewEncode(w).Encode(obj) + return json.NewEncoder(w).Encode(obj) } From 19bf52b5bdfbce5fd47bedb0c1f7494d590068ae Mon Sep 17 00:00:00 2001 From: Josh Brandoff Date: Fri, 12 Jun 2015 22:27:37 -0400 Subject: [PATCH 4/5] Add JSONAPI support to context --- context.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/context.go b/context.go index f5f0de4a..a493dbfc 100644 --- a/context.go +++ b/context.go @@ -331,6 +331,15 @@ func (c *Context) JSON(code int, obj interface{}) { } } +// Serializes the given struct as JSON into the response body. +// It also sets the Content-Type as "application/vnd.api+json". +func (c *Context) JSONAPI(code int, obj interface{}) { + c.writermem.WriteHeader(code) + if err := render.WriteJSONAPI(c.Writer, obj); err != nil { + c.renderError(err) + } +} + // Serializes the given struct as XML into the response body. // It also sets the Content-Type as "application/xml". func (c *Context) XML(code int, obj interface{}) { From 7d753188a7c06af5fa9fe8fb120bdb4d180cf659 Mon Sep 17 00:00:00 2001 From: Josh Brandoff Date: Fri, 12 Jun 2015 22:28:58 -0400 Subject: [PATCH 5/5] Added relevant unit test. --- context_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/context_test.go b/context_test.go index f6083070..c61d8807 100644 --- a/context_test.go +++ b/context_test.go @@ -219,6 +219,17 @@ func TestContextRenderJSON(t *testing.T) { assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8") } +// Tests that the response is serialized as JSON +// and Content-Type is set to application/vnd.api+json +func TestContextRenderJSON(t *testing.T) { + c, w, _ := createTestContext() + c.JSONAPI(201, H{"foo": "bar"}) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json") +} + // Tests that the response is serialized as JSON // and Content-Type is set to application/json func TestContextRenderIndentedJSON(t *testing.T) {