diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-06-30 16:11:05 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-07-06 20:03:36 +0200 |
commit | 12a65e76df9470d9563b91a22969ddb41b7c19aa (patch) | |
tree | ab7b7b3cc67fbb9b4a86c1c347081e4e959ed8e8 /common/types | |
parent | 58c0f5e6171cbf8e3ed8d73ac95a7b85168c5b2f (diff) |
Add openapi3.Unmarshal
Fixes #7442
Fixes #7443
Diffstat (limited to 'common/types')
-rw-r--r-- | common/types/convert.go | 17 | ||||
-rw-r--r-- | common/types/convert_test.go | 9 |
2 files changed, 23 insertions, 3 deletions
diff --git a/common/types/convert.go b/common/types/convert.go index 24e01c273..137029a0e 100644 --- a/common/types/convert.go +++ b/common/types/convert.go @@ -14,6 +14,7 @@ package types import ( + "encoding/json" "html/template" "github.com/spf13/cast" @@ -59,10 +60,20 @@ func TypeToString(v interface{}) (string, bool) { // ToString converts v to a string. func ToString(v interface{}) string { + s, _ := ToStringE(v) + return s +} + +// ToStringE converts v to a string. +func ToStringE(v interface{}) (string, error) { if s, ok := TypeToString(v); ok { - return s + return s, nil } - return cast.ToString(v) - + switch s := v.(type) { + case json.RawMessage: + return string(s), nil + default: + return cast.ToStringE(v) + } } diff --git a/common/types/convert_test.go b/common/types/convert_test.go index 7f86f4c8a..8a4f04db2 100644 --- a/common/types/convert_test.go +++ b/common/types/convert_test.go @@ -14,6 +14,7 @@ package types import ( + "encoding/json" "testing" qt "github.com/frankban/quicktest" @@ -27,3 +28,11 @@ func TestToStringSlicePreserveString(t *testing.T) { c.Assert(ToStringSlicePreserveString(nil), qt.IsNil) } + +func TestToString(t *testing.T) { + c := qt.New(t) + + c.Assert(ToString([]byte("Hugo")), qt.Equals, "Hugo") + c.Assert(ToString(json.RawMessage("Hugo")), qt.Equals, "Hugo") + +} |