Skip to content

Commit bb89ac5

Browse files
authored
Fix UUID query param binding (#623)
* Check case where destination object is an array. If obj implements encoding.TextUnmarshaler, try to unmarshal, otherwise fallthrough to default case. * Revert white space deletion.
1 parent e7a92f5 commit bb89ac5

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

bindstring.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package runtime
1515

1616
import (
17+
"encoding"
1718
"errors"
1819
"fmt"
1920
"reflect"
@@ -85,6 +86,15 @@ func BindStringToObject(src string, dst interface{}) error {
8586
if err == nil {
8687
v.SetBool(val)
8788
}
89+
case reflect.Array:
90+
if tu, ok := dst.(encoding.TextUnmarshaler); ok {
91+
if err := tu.UnmarshalText([]byte(src)); err != nil {
92+
return fmt.Errorf("error unmarshaling '%s' text as %T: %s", src, dst, err)
93+
}
94+
95+
return nil
96+
}
97+
fallthrough
8898
case reflect.Struct:
8999
// if this is not of type Time or of type Date look to see if this is of type Binder.
90100
if dstType, ok := dst.(Binder); ok {

bindstring_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,11 @@ func TestBindStringToObject(t *testing.T) {
169169
var dstEmbeddedMockBinder EmbeddedMockBinder
170170
assert.NoError(t, BindStringToObject(dateString, &dstEmbeddedMockBinder))
171171
assert.EqualValues(t, dateString, dstEmbeddedMockBinder.Time.Format("2006-01-02"))
172+
173+
// Checks UUID binding
174+
uuidString := "bbca1470-5e1f-4c64-ba99-fa7a6d2687b0"
175+
var dstUUID types.UUID
176+
assert.NoError(t, BindStringToObject(uuidString, &dstUUID))
177+
assert.Equal(t, dstUUID.String(), uuidString)
178+
172179
}

0 commit comments

Comments
 (0)