Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples #696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,10 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
if op.RequestBody.Content == nil {
op.RequestBody.Content = map[string]*MediaType{}
}
op.RequestBody.Content[contentType] = &MediaType{Schema: s}
if op.RequestBody.Content[contentType] == nil {
op.RequestBody.Content[contentType] = &MediaType{}
}
op.RequestBody.Content[contentType].Schema = s

if op.BodyReadTimeout == 0 {
// 5 second default
Expand Down
76 changes: 76 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,82 @@ func TestFeatures(t *testing.T) {
URL: "/body",
Body: `{"name":"foo"}`,
},
{
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",
Value: struct {
Name string `json:"name"`
}{
Name: "foo",
},
},
"Example 2": {
Summary: "Example summary",
Value: struct {
Name string `json:"name"`
}{
Name: "bar",
},
},
},
},
},
},
}, func(ctx context.Context, input *struct {
Body struct {
Name string `json:"name"`
}
}) (*struct{}, error) {
assert.Equal(t, "foo", input.Body.Name)
return nil, nil
})
b, _ := api.OpenAPI().Paths["/body"].Put.RequestBody.MarshalJSON()
assert.JSONEq(t, `{
"description": "A description",
"required": true,
"content": {
"application/json": {
"examples": {
"Example 1": {
"summary": "Example summary",
"value": {
"name": "foo"
}
},
"Example 2": {
"summary": "Example summary",
"value": {
"name": "bar"
}
}
},
"schema": {
"$ref": "#/components/schemas/Request"
}
}
}
}`, string(b))
},
Method: http.MethodPut,
URL: "/body",
Body: `{"name":"foo"}`,
},
{
Name: "request-body-nested-struct-readOnly",
Register: func(t *testing.T, api huma.API) {
Expand Down