From a94871d726c767ca439cf647aac7a0046cac916b Mon Sep 17 00:00:00 2001 From: Kiyan Date: Fri, 2 Aug 2024 10:02:42 +0800 Subject: [PATCH 1/2] fix(schema): schema example not work in Parameter --- huma.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/huma.go b/huma.go index f3fd5622..65187c2c 100644 --- a/huma.go +++ b/huma.go @@ -167,6 +167,9 @@ func findParams(registry Registry, op *Operation, t reflect.Type) *findResult[*p if e := f.Tag.Get("example"); e != "" { example = jsonTagValue(registry, f.Type.Name(), pfi.Schema, f.Tag.Get("example")) } + if example == nil && len(pfi.Schema.Examples) > 0 { + example = pfi.Schema.Examples[0] + } // While discouraged, make it possible to make query/header params required. if r := f.Tag.Get("required"); r == "true" { From 40d56036337f6ede26c307c4a1c6614d178dc54d Mon Sep 17 00:00:00 2001 From: Kiyan Date: Mon, 5 Aug 2024 14:23:05 +0800 Subject: [PATCH 2/2] add schema with example test --- huma_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/huma_test.go b/huma_test.go index 79041645..9e1f5704 100644 --- a/huma_test.go +++ b/huma_test.go @@ -2134,6 +2134,32 @@ func TestUnsupportedEmbeddedTypeWithMethods(t *testing.T) { }) } +type SchemaWithExample int + +func (*SchemaWithExample) Schema(r huma.Registry) *huma.Schema { + schema := &huma.Schema{ + Type: huma.TypeInteger, + Examples: []any{1}, + } + return schema +} + +func TestSchemaWithExample(t *testing.T) { + _, app := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0")) + huma.Register(app, huma.Operation{ + OperationID: "test", + Method: http.MethodGet, + Path: "/test", + }, func(ctx context.Context, input *struct { + Test SchemaWithExample `query:"test"` + }) (*struct{}, error) { + return nil, nil + }) + + example := app.OpenAPI().Paths["/test"].Get.Parameters[0].Example + assert.Equal(t, 1, example) +} + // func BenchmarkSecondDecode(b *testing.B) { // //nolint: musttag // type MediumSized struct {