Skip to content

Commit df27bef

Browse files
Merge pull request #854 from danielgtaylor/partial-revert-850
fix: content type bug introduced in #850
2 parents eafeecd + 0b3d165 commit df27bef

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

huma.go

Lines changed: 7 additions & 2 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
}
@@ -499,6 +504,8 @@ func writeResponse(api API, ctx Context, status int, ct string, body any) error
499504
if ctf, ok := body.(ContentTypeFilter); ok {
500505
ct = ctf.ContentType(ct)
501506
}
507+
508+
ctx.SetHeader("Content-Type", ct)
502509
}
503510

504511
if err := transformAndWrite(api, ctx, status, ct, body); err != nil {
@@ -543,8 +550,6 @@ func transformAndWrite(api API, ctx Context, status int, ct string, body any) er
543550
}
544551
}
545552

546-
ctx.SetHeader("Content-Type", ct)
547-
548553
return nil
549554
}
550555

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)