diff --git a/huma.go b/huma.go index 20aaa9c0..f6b14561 100644 --- a/huma.go +++ b/huma.go @@ -582,8 +582,11 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I) if c := f.Tag.Get("contentType"); c != "" { contentType = c } - - s := SchemaFromField(registry, f, getHint(inputType, f.Name, op.OperationID+"Request")) + hint := getHint(inputType, f.Name, op.OperationID+"Request") + if nameHint := f.Tag.Get("nameHint"); nameHint != "" { + hint = nameHint + } + s := SchemaFromField(registry, f, hint) op.RequestBody = &RequestBody{ Required: required, @@ -708,7 +711,11 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I) op.Responses[statusStr].Headers = map[string]*Param{} } if !outBodyFunc { - outSchema := SchemaFromField(registry, f, getHint(outputType, f.Name, op.OperationID+"Response")) + hint := getHint(outputType, f.Name, op.OperationID+"Response") + if nameHint := f.Tag.Get("nameHint"); nameHint != "" { + hint = nameHint + } + outSchema := SchemaFromField(registry, f, hint) if op.Responses[statusStr].Content == nil { op.Responses[statusStr].Content = map[string]*MediaType{} } diff --git a/huma_test.go b/huma_test.go index 9af8c405..77f1887a 100644 --- a/huma_test.go +++ b/huma_test.go @@ -605,6 +605,25 @@ func TestFeatures(t *testing.T) { assert.Equal(t, http.StatusBadRequest, resp.Code) }, }, + { + Name: "request-body-nameHint", + Register: func(t *testing.T, api huma.API) { + huma.Register(api, huma.Operation{ + Method: http.MethodPut, + Path: "/body", + }, func(ctx context.Context, input *struct { + Body struct { + Name string `json:"name"` + } `nameHint:"ANameHint"` + }) (*struct{}, error) { + return nil, nil + }) + assert.Equal(t, "#/components/schemas/ANameHint", api.OpenAPI().Paths["/body"].Put.RequestBody.Content["application/json"].Schema.Ref) + }, + Method: http.MethodPut, + URL: "/body", + Body: `{"name": "Name"}`, + }, { Name: "request-ptr-body-required", Register: func(t *testing.T, api huma.API) { @@ -1009,6 +1028,27 @@ Content of example2.txt. assert.Equal(t, "application/custom-type", resp.Header().Get("Content-Type")) }, }, + { + Name: "response-body-nameHint", + Register: func(t *testing.T, api huma.API) { + type Resp struct { + Body struct { + Greeting string `json:"greeting" ` + } `nameHint:"GreetingResp"` + } + huma.Register(api, huma.Operation{ + Method: http.MethodGet, + Path: "/response", + }, func(ctx context.Context, input *struct{}) (*Resp, error) { + resp := &Resp{} + resp.Body.Greeting = "Hello, world!" + return resp, nil + }) + assert.Equal(t, "#/components/schemas/GreetingResp", api.OpenAPI().Paths["/response"].Get.Responses["200"].Content["application/json"].Schema.Ref) + }, + Method: http.MethodGet, + URL: "/response", + }, { Name: "response", Register: func(t *testing.T, api huma.API) {