Skip to content
Open
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
21 changes: 21 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,27 @@ func TestFeatures(t *testing.T) {
URL: "/body",
Body: `{"items": [{"id": 1}]}`,
},
{
Name: "request-body-custom-unmarshaler-default",
Register: func(t *testing.T, api huma.API) {
huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
}, func(ctx context.Context, input *struct {
Body struct {
Name OmittableNullable[string] `json:"name,omitzero" default:"Kari"`
}
}) (*struct{}, error) {
assert.True(t, input.Body.Name.Sent)
assert.False(t, input.Body.Name.Null)
assert.Equal(t, "Kari", input.Body.Name.Value)
return nil, nil
})
},
Method: http.MethodPut,
URL: "/body",
Body: `{}`,
},
{
Name: "request-body-pointer-defaults",
Register: func(t *testing.T, api huma.API) {
Expand Down
26 changes: 25 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
ipType = reflect.TypeFor[net.IP]()
ipAddrType = reflect.TypeFor[netip.Addr]()
rawMessageType = reflect.TypeFor[json.RawMessage]()
jsonUnmarshalerType = reflect.TypeFor[json.Unmarshaler]()
schemaProviderType = reflect.TypeFor[SchemaProvider]()
schemaTransformerType = reflect.TypeFor[SchemaTransformer]()
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
Expand Down Expand Up @@ -504,6 +505,24 @@ func convertType(fieldName string, t reflect.Type, v any) any {
return tmp.Interface()
}

target := deref(t)
targetPtr := reflect.PointerTo(target)
if target.Implements(jsonUnmarshalerType) || targetPtr.Implements(jsonUnmarshalerType) {
data, err := json.Marshal(v)
if err != nil {
panic(fmt.Errorf("unable to convert %v to %v for field '%s': %v: %w", tv, t, fieldName, err, ErrSchemaInvalid))
}

converted := reflect.New(target)
if err := json.Unmarshal(data, converted.Interface()); err != nil {
panic(fmt.Errorf("unable to convert %v to %v for field '%s': %v: %w", tv, t, fieldName, err, ErrSchemaInvalid))
}
if t.Kind() == reflect.Pointer {
return converted.Interface()
}
return converted.Elem().Interface()
}

if !tv.ConvertibleTo(deref(t)) {
panic(fmt.Errorf("unable to convert %v to %v for field '%s': %w", tv, t, fieldName, ErrSchemaInvalid))
}
Expand Down Expand Up @@ -557,7 +576,12 @@ func jsonTagValue(r Registry, fieldName string, s *Schema, value string) any {
func jsonTag(r Registry, f reflect.StructField, s *Schema, name string) any {
t := f.Type
if value := f.Tag.Get(name); value != "" {
return convertType(f.Name, t, jsonTagValue(r, f.Name, s, value))
parsed := jsonTagValue(r, f.Name, s, value)
base := deref(t)
if base.Implements(schemaProviderType) || reflect.PointerTo(base).Implements(schemaProviderType) {
return parsed
}
return convertType(f.Name, t, parsed)
}
return nil
}
Expand Down
23 changes: 23 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,22 @@ func TestSchema(t *testing.T) {
"required": ["value"]
}`,
},
{
name: "field-default-custom-unmarshaler",
input: struct {
Value OmittableNullable[string] `json:"value,omitzero" default:"foo"`
}{},
expected: `{
"type": "object",
"properties": {
"value": {
"type": ["string", "null"],
"default": "foo"
}
},
"additionalProperties": false
}`,
},
{
name: "field-optional-without-name",
input: struct {
Expand Down Expand Up @@ -1066,6 +1082,13 @@ func TestSchema(t *testing.T) {
}{},
panics: `invalid number tag value 'true' for field 'Value': schema is invalid`,
},
{
name: "panic-json-custom-unmarshaler",
input: struct {
Value OmittableNullable[int] `json:"value,omitzero" default:"true"`
}{},
panics: `invalid number tag value 'true' for field 'Value': schema is invalid`,
},
{
name: "panic-json-int2",
input: struct {
Expand Down