Skip to content

Commit 0b3d165

Browse files
committed
fix: ensure content type is set for 406 not acceptable
1 parent 8b01e42 commit 0b3d165

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

huma.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ func writeResponse(api API, ctx Context, status int, ct string, body any) error
490490
ct, err = api.Negotiate(ctx.Header("Accept"))
491491
if err != nil {
492492
notAccept := NewErrorWithContext(ctx, http.StatusNotAcceptable, "unable to marshal response", err)
493+
ct := "application/json"
494+
if ctf, ok := notAccept.(ContentTypeFilter); ok {
495+
ct = ctf.ContentType(ct)
496+
}
497+
ctx.SetHeader("Content-Type", ct)
493498
if e := transformAndWrite(api, ctx, http.StatusNotAcceptable, "application/json", notAccept); e != nil {
494499
return e
495500
}

huma_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,7 +2067,7 @@ Content-Type: text/plain
20672067
URL: "/response-accept",
20682068
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
20692069
assert.Equal(t, http.StatusNotAcceptable, resp.Code)
2070-
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
2070+
assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type"))
20712071
assert.JSONEq(t, `{"title":"Not Acceptable","status":406,"detail":"unable to marshal response","errors":[{"message":"unknown accept content type"}]}`, resp.Body.String())
20722072
},
20732073
},

0 commit comments

Comments
 (0)