Skip to content

Commit

Permalink
[issues-391] Fix document typecheck in encoding for nested types (#394)
Browse files Browse the repository at this point in the history
* [issues-391] Fix document typecheck in encoding  for nested types

This resolves #391
where encoding of document types
is not checking to see if nested types at every layer
are explicitly marked for no serialization or deserialization.
I moved the check into the recursive layer that way every
nested type would get checked. This mimics how
decoding is correctly handling this issue.
  • Loading branch information
isaiahvita committed Oct 21, 2022
1 parent 4994e29 commit a308d23
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .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": [
"."
]
}
13 changes: 8 additions & 5 deletions document/json/encoder.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -323,4 +326,4 @@ func isValidJSONNumber(s string) bool {

// Make sure we are at the end.
return s == ""
}
}
11 changes: 11 additions & 0 deletions 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"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a308d23

Please sign in to comment.