Skip to content
Open
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
9 changes: 3 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,13 +1135,10 @@ func (c *Context) SecureJSON(code int, obj any) {
// JSONP serializes the given struct as JSON into the response body.
// It adds padding to response body to request data from a server residing in a different domain than the client.
// It also sets the Content-Type as "application/javascript".
//
// When the callback parameter is empty, it behaves equivalently to Context.JSON.
func (c *Context) JSONP(code int, obj any) {
callback := c.DefaultQuery("callback", "")
if callback == "" {
c.Render(code, render.JSON{Data: obj})
return
}
c.Render(code, render.JsonpJSON{Callback: callback, Data: obj})
c.Render(code, render.JsonpJSON{Callback: c.Query("callback"), Data: obj})
}

// JSON serializes the given struct as JSON into the response body.
Expand Down
10 changes: 5 additions & 5 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ func (r SecureJSON) WriteContentType(w http.ResponseWriter) {

// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
if r.Callback == "" {
return WriteJSON(w, r.Data)
}

r.WriteContentType(w)

ret, err := json.API.Marshal(r.Data)
if err != nil {
return err
}

if r.Callback == "" {
_, err = w.Write(ret)
return err
}

callback := template.JSEscapeString(r.Callback)
if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {
return err
Expand Down
23 changes: 16 additions & 7 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,28 @@ func TestRenderJsonpJSONError(t *testing.T) {
assert.Equal(t, `write "`+`);`+`" error`, err.Error())
}

func TestRenderJsonpJSONError2(t *testing.T) {
func TestRenderJsonpJSONWithEmptyCallback(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]any{
"foo": "bar",
"num": 42,
"nested": map[string]any{
"key": "value",
},
}
(JsonpJSON{"", data}).WriteContentType(w)
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))

e := (JsonpJSON{"", data}).Render(w)
require.NoError(t, e)
err := (JsonpJSON{Callback: "", Data: data}).Render(w)

require.NoError(t, err)

// Verify Content-Type is set to jsonContentType when callback is empty
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))

renderData, err := json.API.Marshal(data)
require.NoError(t, err)

assert.JSONEq(t, "{\"foo\":\"bar\"}", w.Body.String())
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))
// Verify body contains correct JSON data
assert.JSONEq(t, string(renderData), w.Body.String())
}

func TestRenderJsonpJSONFail(t *testing.T) {
Expand Down