Skip to content

Commit

Permalink
Allow binding types as parameters through String() methods (deepmap…
Browse files Browse the repository at this point in the history
…#638)

If trying to use a type, such as a UUID in a path parameter, we'd
previously receive an error at runtime to say that `types.UUID` isn't
supported.

As this is a common use case, we should support it. Instead of adding
support for it manually, we can instead look at whether the there's a
`String` method, and if so use that to serialise the field.
  • Loading branch information
Jamie Tanna committed Jun 29, 2022
1 parent ac69f99 commit 650a2f4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/runtime/styleparam.go
Expand Up @@ -382,7 +382,12 @@ func primitiveToString(value interface{}) (string, error) {
case reflect.String:
output = v.String()
default:
return "", fmt.Errorf("unsupported type %s", reflect.TypeOf(value).String())
v, ok := value.(fmt.Stringer)
if !ok {
return "", fmt.Errorf("unsupported type %s", reflect.TypeOf(value).String())
}

output = v.String()
}
return output, nil
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/runtime/styleparam_test.go
Expand Up @@ -629,6 +629,11 @@ func TestStyleParam(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, "1.05", result)

uuidValue := uuid.MustParse("c2d07ba4-5106-4eab-bcad-0bd6068dcb1a")
result, err = StyleParamWithLocation("simple", false, "foo", ParamLocationQuery, types.UUID(uuidValue))
assert.NoError(t, err)
assert.EqualValues(t, "c2d07ba4-5106-4eab-bcad-0bd6068dcb1a", result)

// Test that we handle optional fields
type TestObject2 struct {
FirstName *string `json:"firstName"`
Expand Down

0 comments on commit 650a2f4

Please sign in to comment.