`default` tag causes panic with generic wrapper types (e.g., `OmittableNullable[string]`) · Issue #814 · danielgtaylor/huma · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While experimenting with the OmittableNullable[T] type as shown in the official examples/omit, I encountered a panic when trying to set a default value on a field of this type.
panic: unable to convert string to main.OmittableNullable[string] for field 'Name': schema is invalid
github.com/danielgtaylor/huma/v2.convertType(...)
.../huma/v2@v2.32.0/schema.go:469 +0x3bc
...
⚠️ Root Cause
From what I can see in convertType() in schema.go, the default value "Kari" is treated as a string, but the target field type is OmittableNullable[string], a custom generic struct. Go's reflection system cannot convert between these types directly, which triggers the panic.
✅ Expected Behavior
Ideally, Huma should:
Detect when a field is a wrapper struct around a basic type, like OmittableNullable[string].
Allow setting a default tag for the inner value type (T), constructing a proper wrapper instance (e.g., OmittableNullable[string]{Value: "Kari", Present: true}).
💡 Possible Solutions
Introduce support for unwrapping known nullable/wrapper types when parsing defaults.
Optionally allow custom hooks or interfaces (e.g., FromDefault(value string) (any, error)) on field types to control how default values are constructed.
Let me know if a workaround is possible in the meantime — thank you for the great library!
The text was updated successfully, but these errors were encountered:
Hi 👋
While experimenting with the
OmittableNullable[T]
type as shown in the official examples/omit, I encountered a panic when trying to set adefault
value on a field of this type.❗️ Problem
The following struct works fine:
However, adding a
default
tag causes a runtime panic:💥 Panic Trace
From what I can see in
convertType()
inschema.go
, the default value"Kari"
is treated as astring
, but the target field type isOmittableNullable[string]
, a custom generic struct. Go's reflection system cannot convert between these types directly, which triggers thepanic
.✅ Expected Behavior
Ideally, Huma should:
OmittableNullable[string]
.default
tag for the inner value type (T
), constructing a proper wrapper instance (e.g.,OmittableNullable[string]{Value: "Kari", Present: true}
).💡 Possible Solutions
FromDefault(value string) (any, error)
) on field types to control how default values are constructed.Let me know if a workaround is possible in the meantime — thank you for the great library!
The text was updated successfully, but these errors were encountered: