danielgtaylor / danielgtaylor/huma

`default` tag causes panic with generic wrapper types (e.g., `OmittableNullable[string]`)

Open
#814 0 comments 2 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

Hi 👋

While experimenting with the `OmittableNullable[T]` type as shown in the official [examples/omit](https://github.com/danielgtaylor/huma/blob/main/examples/omit/main.go), I encountered a panic when trying to set a `default` value on a field of this type.

### ❗️ Problem

The following struct works fine:

```go
type MyInput struct {
Name OmittableNullable[string] `json:"name,omitempty" maxLength:"10"`
}
```

However, adding a `default` tag causes a runtime panic:

```go
type MyInput struct {
Name OmittableNullable[string] `json:"name,omitempty" maxLength:"10" default:"Kari"`
}
```

### 💥 Panic Trace

```
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!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.