Repository: danielgtaylor/huma
Pull request #696 (2025-01-02 18:14:16)

Fix examples
In v2.22.1, I used the display of examples in the swagger.
In v2.23.0 this was broken by adding the line 

```
op.RequestBody.Content[contentType] = &MediaType{Schema: s}
```
https://github.com/danielgtaylor/huma/compare/v2.22.1...v2.23.0#diff-34398007f1f21b0bc55b2fe31480add48b936399f37e1ae49662d9e4dbf33a98R598

Now with the upgrade to v2.23.0+ all my examples are being overwritten and not displayed in swagger.

Made a small fix to make it work for me as before.

Or is there another more correct way to display examples?


Patch:
diff --git a/huma.go b/huma.go
index 37e30b8..eb0e545 100644
--- a/huma.go
+++ b/huma.go
@@ -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


Test patch:
diff --git a/huma_test.go b/huma_test.go
index 400fb04..84f6524 100644
--- a/huma_test.go
+++ b/huma_test.go
@@ -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) {