From 3b95f7825c80efb7b50e7663dcf73f353e42ff7c Mon Sep 17 00:00:00 2001 From: Lenny-Campino Hartmann Date: Fri, 26 Apr 2024 17:18:45 +0700 Subject: [PATCH 1/2] fix: check if struct implements interface SchemaProvider after deref --- schema.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/schema.go b/schema.go index dacd4893..accb2a9c 100644 --- a/schema.go +++ b/schema.go @@ -601,17 +601,17 @@ type SchemaProvider interface { // registry := huma.NewMapRegistry("#/prefix", huma.DefaultSchemaNamer) // schema := huma.SchemaFromType(registry, reflect.TypeOf(MyType{})) func SchemaFromType(r Registry, t reflect.Type) *Schema { + isPointer := t.Kind() == reflect.Pointer + + s := Schema{} + t = deref(t) + v := reflect.New(t).Interface() if sp, ok := v.(SchemaProvider); ok { // Special case: type provides its own schema. Do not try to generate. return sp.Schema(r) } - isPointer := t.Kind() == reflect.Pointer - - s := Schema{} - t = deref(t) - // Handle special cases. switch t { case timeType: From 4ee4554f6f6f715ac2e7fd2d5824c5d51fd37bb4 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Fri, 26 Apr 2024 05:56:39 -0700 Subject: [PATCH 2/2] test: add custom schema pointer test --- schema_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/schema_test.go b/schema_test.go index d88e496a..4ae63e6f 100644 --- a/schema_test.go +++ b/schema_test.go @@ -479,6 +479,23 @@ func TestSchema(t *testing.T) { "required": ["value"] }`, }, + { + name: "field-example-custom-pointer", + input: struct { + Value *CustomSchema `json:"value" example:"foo"` + }{}, + expected: `{ + "type": "object", + "properties": { + "value": { + "type": "string", + "examples": ["foo"] + } + }, + "additionalProperties": false, + "required": ["value"] + }`, + }, { name: "field-enum-custom", input: struct {