Using render.JSON with no http Request object throws an error
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 50/100
Direzione di ricerca
Inizia in responder.go alla riga 103, dove JSON accede al contesto della richiesta, e riproduci il problema con il test wrapper mostrato nell’issue. Controlla il percorso nil-request e verifica che il corpo della risposta venga scritto senza panic, preservando il comportamento di stato richiesto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
In the responder, render.JSON() function takes in an "http.Request" object which will be used to get the http response status code
I'm customizing the behavior of the function, by wrapping it with a wrapper function in my project. So I can call one function to pass in the response body as well as the status code, like so
func (r *responder) JSON(w http.ResponseWriter, v interface{}, s int) {
// Write http response status
w.WriteHeader(s)
render.JSON(w, nil, v)
}
When "nil" is passed is shown above instead of an http.Request object, the function throws an error.
--- FAIL: TestJSON (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xf0 pc=0x12a9ca0]
Here is the test I wrote to test the wrapper:
func TestJSON(t *testing.T) {
type mockResponse struct {
MockField string `json:"field"`
}
responder := NewResponder()
recorder := httptest.NewRecorder()
responder.JSON(recorder, mockResponse{MockField: "test"}, http.StatusOK)
assert.Equal(t, recorder.Code, 200)
assert.JSONEq(t, `{"field": "test"}`, recorder.Body.String())
}
I did some debugging, it seems like the error is caused by line 103 in JSON function in responder.go, which is the following:
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
How about adding a check for when the request object is nil, it skips this whole check and only writes the body to the buffer?
- Lingua principale
- Go
- Stelle
- 340
- Fork
- 69
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di go-chi/render
-
Render and Bind panic on unexported nested interface fieldsForse già presa @duongynhi000005-oss l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Channel response appends JSON null after context timeoutForse già presa @awais-ur-rehman l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
-
Any concern render.JSON() can leak internal errors to outside?Forse già presa @team-humaki l’ha presa 50 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 30/100
Tutte le issue di go-chi/render
Issue simili
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
valkey-io/valkey-operator#610 ·
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
-
WriterSetTimes: the UTIME_NOW check fails when the kernel uses its coarse clockForse già presa @JakeChampion l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
JakeChampion/lang#14224 ·
I maintainer di solito rispondono entro 1 giorno
-
agent-butler-finding bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
jordansmall/spindrift#6391 ·
I maintainer di solito rispondono entro 1 giorno