Skip to content

Commit

Permalink
Merge pull request #413 from lennycampino/main
Browse files Browse the repository at this point in the history
fix: check if struct implements interface SchemaProvider after deref
  • Loading branch information
danielgtaylor authored Apr 26, 2024
2 parents dcfa3e9 + 4ee4554 commit ed2c3cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 5 additions & 5 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ed2c3cd

Please sign in to comment.