Skip to content

Commit

Permalink
codegen: Dont serialize nil for visited nested event stream shapes (#…
Browse files Browse the repository at this point in the history
…3934)

Updates the SDK's code gen to not encode entries as `nil` for shapes
that have already been visited. This skips `nil`'s in nested slice and
map event stream shapes.

Related to bigger issue of SDK's JSON serializer does not correctly
skip, or serailize NULL for nil values in slices.
  • Loading branch information
jasdel authored May 28, 2021
1 parent e0475a0 commit 0bba8f5
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions private/model/api/eventstream_tmpl_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ func templateMap(args ...interface{}) map[string]interface{} {
}

func valueForType(s *Shape, visited []string) string {
for _, v := range visited {
if v == s.ShapeName {
return "nil"
}
if isShapeVisited(visited, s.ShapeName) {
return "nil"
}

visited = append(visited, s.ShapeName)
Expand Down Expand Up @@ -116,17 +114,21 @@ func valueForType(s *Shape, visited []string) string {
case "list":
w := bytes.NewBuffer(nil)
fmt.Fprintf(w, "%s{\n", s.GoType())
for i := 0; i < 3; i++ {
fmt.Fprintf(w, "%s,\n", valueForType(s.MemberRef.Shape, visited))
if !isShapeVisited(visited, s.MemberRef.Shape.ShapeName) {
for i := 0; i < 3; i++ {
fmt.Fprintf(w, "%s,\n", valueForType(s.MemberRef.Shape, visited))
}
}
fmt.Fprintf(w, "}")
return w.String()

case "map":
w := bytes.NewBuffer(nil)
fmt.Fprintf(w, "%s{\n", s.GoType())
for _, k := range []string{"a", "b", "c"} {
fmt.Fprintf(w, "%q: %s,\n", k, valueForType(s.ValueRef.Shape, visited))
if !isShapeVisited(visited, s.ValueRef.Shape.ShapeName) {
for _, k := range []string{"a", "b", "c"} {
fmt.Fprintf(w, "%q: %s,\n", k, valueForType(s.ValueRef.Shape, visited))
}
}
fmt.Fprintf(w, "}")
return w.String()
Expand All @@ -135,3 +137,12 @@ func valueForType(s *Shape, visited []string) string {
panic(fmt.Sprintf("valueForType does not support %s, %s", s.ShapeName, s.Type))
}
}

func isShapeVisited(visited []string, name string) bool {
for _, v := range visited {
if v == name {
return true
}
}
return false
}

0 comments on commit 0bba8f5

Please sign in to comment.