Skip to content

Commit

Permalink
codegen: Ensure API structs don't collide with API client type name (#…
Browse files Browse the repository at this point in the history
…3651)

Updates the SDK's code generation to renames structures that collide
with the service's API client struct name.

For example, service Foo, has struct shape named Foo. The API client
type would be generated as Foo, and the colliding struct shape would be
renamed to Foo_, with a trailing underscore(_).
  • Loading branch information
jasdel committed Nov 24, 2020
1 parent 540127a commit 8e5531c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions private/model/api/passes.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ func (a *API) renameExportable() {
}

newName := a.ExportableName(k)
if s.Type == "structure" && newName == a.StructName() {
// If struct collides client's struct type name the shape needs to
// be renamed with a trailing `_` to prevent collision.
newName += "_"
}

if newName != s.ShapeName {
s.Rename(newName)
}
Expand Down
36 changes: 36 additions & 0 deletions private/model/api/passes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,42 @@ func TestCreateInputOutputShapes(t *testing.T) {
"FirstOpInput", "FirstOpOutput",
},
},
"collidingShape": {
API: &API{
name: "APIClientName",
Metadata: meta,
Operations: map[string]*Operation{
"FirstOp": {Name: "FirstOp",
InputRef: ShapeRef{ShapeName: "FirstOpRequest"},
},
},
Shapes: map[string]*Shape{
"FirstOpRequest": {ShapeName: "FirstOpRequest", Type: "structure",
MemberRefs: map[string]*ShapeRef{
"Foo": {ShapeName: "APIClientName"},
"ooF": {ShapeName: "APIClientNameList"},
},
},
"APIClientName": {
ShapeName: "APIClientName", Type: "structure",
},
"APIClientNameList": {
ShapeName: "APIClientNameList", Type: "list",
MemberRef: ShapeRef{ShapeName: "APIClientName"},
},
},
},
ExpectOps: map[string]OpExpect{
"FirstOp": {
Input: "FirstOpInput",
Output: "FirstOpOutput",
},
},
ExpectShapes: []string{
"APIClientNameList", "APIClientName_",
"FirstOpInput", "FirstOpOutput",
},
},
}

for name, c := range cases {
Expand Down

0 comments on commit 8e5531c

Please sign in to comment.