这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(schema): proper array minItems, maxItems and doc reporting in generated schemas #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
60 changes: 58 additions & 2 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
},
Expand Down Expand Up @@ -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 {
Expand Down
Loading