Skip to content

Commit ac9959c

Browse files
mollyrobtmeCopilot
authored
fix: handle nil interface response bodies (#1072)
* fix: handle nil interface response bodies * chore: additional regression guard Co-authored-by: Copilot Autofix powered by AI <[email protected]> --------- Co-authored-by: Robert Thomas <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent b659c74 commit ac9959c

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎huma_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,27 @@ Content-Type: text/plain
28502850
assert.Equal(t, http.StatusOK, resp.Code)
28512851
},
28522852
},
2853+
{
2854+
Name: "response-transform-nil-interface-body",
2855+
Transformers: []huma.Transformer{
2856+
huma.NewSchemaLinkTransformer("/", "/").Transform,
2857+
},
2858+
Register: func(t *testing.T, api huma.API) {
2859+
huma.Get(api, "/transform", func(ctx context.Context, i *struct{}) (*struct {
2860+
Body any
2861+
}, error) {
2862+
return &struct {
2863+
Body any
2864+
}{Body: nil}, nil
2865+
})
2866+
},
2867+
Method: http.MethodGet,
2868+
URL: "/transform",
2869+
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
2870+
assert.Equal(t, http.StatusOK, resp.Code)
2871+
assert.JSONEq(t, `null`, resp.Body.String())
2872+
},
2873+
},
28532874
{
28542875
Name: "schema-url-from-x-forwarded-host",
28552876
Transformers: []huma.Transformer{

‎transforms.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ func (t *SchemaLinkTransformer) OnAddOperation(oapi *OpenAPI, op *Operation) {
155155
// Transform is called for every response to add the `$schema` field and/or
156156
// the Link header pointing to the JSON Schema.
157157
func (t *SchemaLinkTransformer) Transform(ctx Context, _ string, v any) (any, error) {
158+
if v == nil {
159+
return v, nil
160+
}
161+
158162
vv := reflect.ValueOf(v)
159163
if vv.Kind() == reflect.Pointer && vv.IsNil() {
160164
return v, nil

0 commit comments

Comments
 (0)