diff --git a/internal/apijson/registry.go b/internal/apijson/registry.go index 2ea00ae..119cc5f 100644 --- a/internal/apijson/registry.go +++ b/internal/apijson/registry.go @@ -29,3 +29,13 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari unionVariants[variant.Type] = typ } } + +// Useful to wrap a union type to force it to use [apijson.UnmarshalJSON] since you cannot define an +// UnmarshalJSON function on the interface itself. +type UnionUnmarshaler[T any] struct { + Value T +} + +func (c *UnionUnmarshaler[T]) UnmarshalJSON(buf []byte) error { + return UnmarshalRoot(buf, &c.Value) +}