Skip to content

Commit

Permalink
Merge pull request #727 from danielgtaylor/custom-request-schema
Browse files Browse the repository at this point in the history
fix: allow providing a custom request schema
  • Loading branch information
danielgtaylor authored Feb 15, 2025
2 parents 151aa1e + 8894fbf commit 169da69
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
15 changes: 8 additions & 7 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,16 +1069,17 @@ func setRequestBodyFromBody(op *Operation, registry Registry, fBody reflect.Stru
if c := fBody.Tag.Get("contentType"); c != "" {
contentType = c
}
hint := getHint(inputType, fBody.Name, op.OperationID+"Request")
if nameHint := fBody.Tag.Get("nameHint"); nameHint != "" {
hint = nameHint
}
s := SchemaFromField(registry, fBody, hint)
if op.RequestBody.Content[contentType] == nil {
op.RequestBody.Content[contentType] = &MediaType{}
}
op.RequestBody.Content[contentType].Schema = s

if op.RequestBody.Content[contentType].Schema == nil {
hint := getHint(inputType, fBody.Name, op.OperationID+"Request")
if nameHint := fBody.Tag.Get("nameHint"); nameHint != "" {
hint = nameHint
}
s := SchemaFromField(registry, fBody, hint)
op.RequestBody.Content[contentType].Schema = s
}
}

type rawBodyType int
Expand Down
40 changes: 34 additions & 6 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,19 +690,13 @@ func TestFeatures(t *testing.T) {
{
Name: "request-body-examples",
Register: func(t *testing.T, api huma.API) {
schema := &huma.Schema{
Type: huma.TypeObject,
Properties: map[string]*huma.Schema{"name": {Type: huma.TypeString}},
}

huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
RequestBody: &huma.RequestBody{
Description: "A description",
Content: map[string]*huma.MediaType{
"application/json": {
Schema: schema,
Examples: map[string]*huma.Example{
"Example 1": {
Summary: "Example summary",
Expand Down Expand Up @@ -924,6 +918,40 @@ func TestFeatures(t *testing.T) {
URL: "/body",
Body: `{"name": "Name"}`,
},
{
Name: "request-body-custom-schema",
Register: func(t *testing.T, api huma.API) {
api.OpenAPI().Components.Schemas.Map()["Dummy"] = &huma.Schema{
Type: huma.TypeObject,
Properties: map[string]*huma.Schema{
"name": {Type: huma.TypeString},
},
}
huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
RequestBody: &huma.RequestBody{
Content: map[string]*huma.MediaType{
"application/json": {
Schema: &huma.Schema{
Ref: "#/components/schemas/Dummy",
},
},
},
},
}, func(ctx context.Context, input *struct {
Body struct {
Name string `json:"name"`
}
}) (*struct{}, error) {
return nil, nil
})
assert.Equal(t, "#/components/schemas/Dummy", api.OpenAPI().Paths["/body"].Put.RequestBody.Content["application/json"].Schema.Ref)
},
Method: http.MethodPut,
URL: "/body",
Body: `{"name": "Name"}`,
},
{
Name: "request-body-embed-struct",
Register: func(t *testing.T, api huma.API) {
Expand Down

0 comments on commit 169da69

Please sign in to comment.