Repository: danielgtaylor/huma (go)

Issue:
Huma no long returning default application/json header (re: most recent update v2.34)
G'day Huma team/Daniel,

We've noticed some unexpected behaviours regarding the Content-Type header in the most recent update (v2.34) where Content-Type: application/json is no longer set by default, even when:

- cfg.DefaultFormat = "application/json" is explicitly set
- The client does not send an Accept header
- cfg.NoFormatFallBack is left to the default `false`

It looks like this is due to `ctx.SetHeader("Content-Type", ct)` from `writeResponse()` to `transformAndWrite()`
I'm not sure if was the intended behaviour. 

Reference: https://github.com/danielgtaylor/huma/compare/v2.33.0...v2.34.0#diff-34398007f1f21b0bc55b2fe31480add48b936399f37e1ae49662d9e4dbf33a98L503 



Pull request description:
fix: content type bug introduced in #850
This PR reverts part of #850 and adjusts the code so the tests for it still pass while maintaining the correct overall behavior for setting the content type.

Fixes #851 

Patch:
diff --git a/huma.go b/huma.go
index 1887d13..6f98afa 100644
--- a/huma.go
+++ b/huma.go
@@ -490,6 +490,11 @@ func writeResponse(api API, ctx Context, status int, ct string, body any) error
 		ct, err = api.Negotiate(ctx.Header("Accept"))
 		if err != nil {
 			notAccept := NewErrorWithContext(ctx, http.StatusNotAcceptable, "unable to marshal response", err)
+			ct := "application/json"
+			if ctf, ok := notAccept.(ContentTypeFilter); ok {
+				ct = ctf.ContentType(ct)
+			}
+			ctx.SetHeader("Content-Type", ct)
 			if e := transformAndWrite(api, ctx, http.StatusNotAcceptable, "application/json", notAccept); e != nil {
 				return e
 			}
@@ -499,6 +504,8 @@ func writeResponse(api API, ctx Context, status int, ct string, body any) error
 		if ctf, ok := body.(ContentTypeFilter); ok {
 			ct = ctf.ContentType(ct)
 		}
+
+		ctx.SetHeader("Content-Type", ct)
 	}
 
 	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
 		}
 	}
 
-	ctx.SetHeader("Content-Type", ct)
-
 	return nil
 }
 


Test patch:
diff --git a/huma_test.go b/huma_test.go
index a0ea922..4f10903 100644
--- a/huma_test.go
+++ b/huma_test.go
@@ -2067,7 +2067,7 @@ Content-Type: text/plain
 			URL: "/response-accept",
 			Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
 				assert.Equal(t, http.StatusNotAcceptable, resp.Code)
-				assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
+				assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type"))
 				assert.JSONEq(t, `{"title":"Not Acceptable","status":406,"detail":"unable to marshal response","errors":[{"message":"unknown accept content type"}]}`, resp.Body.String())
 			},
 		},