Skip to content

Commit 1b74ca8

Browse files
authored
fix: bind uuid.UUID fields in deepObject parameters (#171)
uuid.UUID is a [16]byte array, so it never reached the TextUnmarshaler check that #45 added to the struct branch and still failed with "unhandled type: uuid.UUID". Handle array kinds that implement encoding.TextUnmarshaler, as BindStringToObject already does. Co-authored-by: youdie006 <youdie006@users.noreply.github.com>
1 parent 171731d commit 1b74ca8

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎deepobject.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
289289
}
290290
iv.Set(dstSlice)
291291
return nil
292+
case reflect.Array:
293+
// Array-backed text types such as uuid.UUID.
294+
if tu, ok := v.Interface().(encoding.TextUnmarshaler); ok {
295+
return tu.UnmarshalText([]byte(pathValues.value))
296+
}
297+
return errors.New("unhandled type: " + it.String())
292298
case reflect.Struct:
293299
// Some special types we care about are structs. Handle them
294300
// here. They may be redefined, so we need to do some hoop
@@ -338,7 +344,7 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
338344
dst.Set(reflect.ValueOf(tm))
339345
return nil
340346
}
341-
// For other struct types that implement TextUnmarshaler (e.g. uuid.UUID),
347+
// For other struct types that implement TextUnmarshaler,
342348
// use that for binding. This comes after the legacy Date/time.Time checks
343349
// above which have special fallback format handling.
344350
if tu, ok := v.Interface().(encoding.TextUnmarshaler); ok {

‎deepobject_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99
"unicode"
1010

11+
"github.com/google/uuid"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
)
@@ -561,3 +562,45 @@ func TestDeepObject_InterfaceRoundTrip(t *testing.T) {
561562
var dst map[string]interface{}
562563
assertDeepObjectWireSafe(t, marshaled, "properties", &dst, src)
563564
}
565+
566+
func TestDeepObject_UUIDFields(t *testing.T) {
567+
type UUIDObject struct {
568+
ID uuid.UUID `json:"id"`
569+
Owner *uuid.UUID `json:"owner,omitempty"`
570+
}
571+
id := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
572+
owner := uuid.MustParse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
573+
574+
t.Run("uuid values bind", func(t *testing.T) {
575+
params := url.Values{}
576+
params.Set("p[id]", id.String())
577+
params.Set("p[owner]", owner.String())
578+
579+
var dst UUIDObject
580+
err := UnmarshalDeepObject(&dst, "p", params)
581+
require.NoError(t, err)
582+
assert.Equal(t, UUIDObject{ID: id, Owner: &owner}, dst)
583+
})
584+
585+
t.Run("invalid uuid returns error", func(t *testing.T) {
586+
params := url.Values{}
587+
params.Set("p[id]", "not-a-uuid")
588+
589+
var dst UUIDObject
590+
err := UnmarshalDeepObject(&dst, "p", params)
591+
require.Error(t, err)
592+
assert.Contains(t, err.Error(), "invalid UUID")
593+
})
594+
595+
t.Run("array without TextUnmarshaler is unhandled", func(t *testing.T) {
596+
params := url.Values{}
597+
params.Set("p[pair]", "a")
598+
599+
var dst struct {
600+
Pair [2]string `json:"pair"`
601+
}
602+
err := UnmarshalDeepObject(&dst, "p", params)
603+
require.Error(t, err)
604+
assert.Contains(t, err.Error(), "unhandled type")
605+
})
606+
}

0 commit comments

Comments
 (0)