Skip to content

Commit

Permalink
Properly handle unions in from_dict
Browse files Browse the repository at this point in the history
When calling from_dict, unions need to use their special global
method. This was already being generated, just never called.
  • Loading branch information
JordonPhillips committed Oct 10, 2023
1 parent 06eb6b1 commit 94fdd7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
21 changes: 20 additions & 1 deletion codegen/smithy-python-codegen-test/model/main.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use aws.protocols#restJson1
service Weather {
version: "2006-03-01"
resources: [City]
operations: [GetCurrentTime]
operations: [GetCurrentTime, TestUnionListOperation]
}

resource City {
Expand All @@ -26,6 +26,25 @@ resource City {
operations: [GetCityAnnouncements]
}

@http(code: 200, method: "POST", uri: "/test-union-list")
operation TestUnionListOperation {
input := {
inputList: UnionList
}
output := {
response: String
}
}

list UnionList {
member: UnionListMember
}

union UnionListMember {
field1: String
field2: Integer
}

resource Forecast {
identifiers: { cityId: CityId }
read: GetForecast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ private void writeFromDict() {
writer.addStdlibImport("typing", "List");
writer.addStdlibImport("typing", "Any");
writer.openBlock("def $L(given: List[Any]) -> $T:", "", fromDictSymbol.getName(), symbol, () -> {
if (target.isUnionShape() || target.isStructureShape()) {
if (target.isStructureShape()) {
writer.write("return [$T.from_dict(v)$L for v in given]", targetSymbol, sparseGuard);
} else if (target.isMapShape() || target instanceof CollectionShape) {
} else if (target.isUnionShape() || target.isMapShape() || target instanceof CollectionShape) {
var targetFromDictSymbol = targetSymbol.expectProperty("fromDict", Symbol.class);
writer.write("return [$T(v)$L for v in given]", targetFromDictSymbol, sparseGuard);
} else {
Expand Down

0 comments on commit 94fdd7d

Please sign in to comment.