diff --git a/.changelog/52ea7af068da449f918698755a7d0f01.json b/.changelog/52ea7af068da449f918698755a7d0f01.json new file mode 100644 index 000000000..6c1986046 --- /dev/null +++ b/.changelog/52ea7af068da449f918698755a7d0f01.json @@ -0,0 +1,8 @@ +{ + "id": "52ea7af0-68da-449f-9186-98755a7d0f01", + "type": "bugfix", + "description": "fixed document type checking for encoding nested types", + "modules": [ + "." + ] +} \ No newline at end of file diff --git a/document/json/encoder.go b/document/json/encoder.go index 860f899ab..f3ed69b1e 100644 --- a/document/json/encoder.go +++ b/document/json/encoder.go @@ -20,10 +20,6 @@ type Encoder struct { // Encode returns the JSON encoding of v. func (e *Encoder) Encode(v interface{}) ([]byte, error) { - if document.IsNoSerde(v) { - return nil, fmt.Errorf("unsupported type: %v", v) - } - encoder := smithyjson.NewEncoder() if err := e.encode(jsonValueProvider(encoder.Value), reflect.ValueOf(v), serde.Tag{}); err != nil { @@ -137,6 +133,13 @@ func (e *Encoder) encodeZeroValue(vp valueProvider, rv reflect.Value) error { } func (e *Encoder) encodeStruct(vp valueProvider, rv reflect.Value) error { + if rv.CanInterface() && document.IsNoSerde(rv.Interface()) { + return &document.UnmarshalTypeError{ + Value: fmt.Sprintf("unsupported type"), + Type: rv.Type(), + } + } + switch { case rv.Type().ConvertibleTo(serde.ReflectTypeOf.Time): return &document.InvalidMarshalError{ @@ -323,4 +326,4 @@ func isValidJSONNumber(s string) bool { // Make sure we are at the end. return s == "" -} +} \ No newline at end of file diff --git a/document/json/encoder_test.go b/document/json/encoder_test.go index 1ab2a3c85..a0930f7cb 100644 --- a/document/json/encoder_test.go +++ b/document/json/encoder_test.go @@ -1,6 +1,7 @@ package json_test import ( + "github.com/aws/smithy-go/document" "github.com/aws/smithy-go/document/internal/serde" "github.com/aws/smithy-go/document/json" "github.com/google/go-cmp/cmp" @@ -41,10 +42,20 @@ func TestEncoder_Encode(t *testing.T) { func TestNewEncoderUnsupportedTypes(t *testing.T) { type customTime time.Time + type noSerde = document.NoSerde + type NestedThing struct { + SomeThing string + noSerde + } + type Thing struct { + OtherThing string + NestedThing NestedThing + } cases := []interface{}{ time.Now().UTC(), customTime(time.Now().UTC()), + Thing{OtherThing: "foo", NestedThing: NestedThing{SomeThing: "bar"}}, } encoder := json.NewEncoder()