diff --git a/registry.go b/registry.go index 6f008882..5b034d97 100644 --- a/registry.go +++ b/registry.go @@ -71,6 +71,11 @@ func (r *mapRegistry) Schema(t reflect.Type, allowRef bool, hint string) *Schema origType := t t = deref(t) + // Pointer to array should decay to array + if t.Kind() == reflect.Array || t.Kind() == reflect.Slice { + origType = t + } + alias, ok := r.aliases[t] if ok { return r.Schema(alias, allowRef, hint) diff --git a/schema.go b/schema.go index f9471887..f65de67a 100644 --- a/schema.go +++ b/schema.go @@ -559,8 +559,12 @@ func SchemaFromField(registry Registry, f reflect.StructField, hint string) *Sch fs.MaxLength = intTag(f, "maxLength") fs.Pattern = f.Tag.Get("pattern") fs.PatternDescription = f.Tag.Get("patternDescription") - fs.MinItems = intTag(f, "minItems") - fs.MaxItems = intTag(f, "maxItems") + if _, ok := f.Tag.Lookup("minItems"); ok { + fs.MinItems = intTag(f, "minItems") + } + if _, ok := f.Tag.Lookup("maxItems"); ok { + fs.MaxItems = intTag(f, "maxItems") + } fs.UniqueItems = boolTag(f, "uniqueItems") fs.MinProperties = intTag(f, "minProperties") fs.MaxProperties = intTag(f, "maxProperties") diff --git a/schema_test.go b/schema_test.go index 3623a7dc..f92740e3 100644 --- a/schema_test.go +++ b/schema_test.go @@ -66,6 +66,13 @@ func (c BadRefSchema) Schema(r huma.Registry) *huma.Schema { var _ huma.SchemaProvider = BadRefSchema{} +type TypedArrayWithCustomDesc [4]float64 + +func (t *TypedArrayWithCustomDesc) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema { + s.Description = "custom description" + return s +} + func TestSchema(t *testing.T) { bitSize := strconv.Itoa(bits.UintSize) @@ -704,9 +711,12 @@ func TestSchema(t *testing.T) { "properties":{ "array":{ "items":{ - "$ref":"#/components/schemas/RecursiveChildLoop"}, - "type":"array" + "$ref":"#/components/schemas/RecursiveChildLoop" }, + "maxItems":1, + "minItems":1, + "type":"array" + }, "byRef":{ "$ref":"#/components/schemas/RecursiveChildKey" }, @@ -836,6 +846,52 @@ func TestSchema(t *testing.T) { }{}, panics: `nullable is not supported for field 'Value' which is type '#/components/schemas/ValueStruct'`, }, + { + name: "field-custom-array", + input: struct { + Value TypedArrayWithCustomDesc `json:"value"` + }{}, + expected: ` { + "additionalProperties":false, + "properties":{ + "value":{ + "description":"custom description", + "items":{ + "format":"double", + "type":"number" + }, + "maxItems":4, + "minItems":4, + "type":"array" + } + }, + "required":["value"], + "type":"object" + }`, + }, + { + name: "field-ptr-to-custom-array", + input: struct { + Value *TypedArrayWithCustomDesc `json:"value"` + }{}, + expected: ` { + "additionalProperties":false, + "properties":{ + "value":{ + "description":"custom description", + "items":{ + "format":"double", + "type":"number" + }, + "maxItems":4, + "minItems":4, + "type":"array" + } + }, + "required":["value"], + "type":"object" + }`, + }, } for _, c := range cases {