Skip to content

Commit 642b7ba

Browse files
committed
Fix JSON Union serialization issue
1 parent 5bca64d commit 642b7ba

File tree

1 file changed

+8
-3
lines changed
  • codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen

1 file changed

+8
-3
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonShapeSerVisitor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void serializeStructure(GenerationContext context, StructureShape shape)
116116
if (memberShape.hasTrait(IdempotencyTokenTrait.class)) {
117117
writer.write("'$L': $L ?? generateIdempotencyToken(),", locationName, valueProvider);
118118
} else {
119-
writer.write("...($L !== undefined && { '$L': $L }),", inputLocation, locationName, valueProvider);
119+
writer.write("...($L !== undefined && { $S: $L }),", inputLocation, locationName, valueProvider);
120120
}
121121
});
122122

@@ -133,11 +133,16 @@ public void serializeUnion(GenerationContext context, UnionShape shape) {
133133
// Use a TreeMap to sort the members.
134134
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
135135
members.forEach((memberName, memberShape) -> {
136+
// Use the jsonName trait value if present, otherwise use the member name.
137+
String locationName = memberShape.getTrait(JsonNameTrait.class)
138+
.map(JsonNameTrait::getValue)
139+
.orElse(memberName);
136140
Shape target = model.expectShape(memberShape.getTarget());
137141
// Dispatch to the input value provider for any additional handling.
138-
writer.write("$L: value => $L,", memberName, target.accept(getMemberVisitor("value")));
142+
writer.write("$L: value => ({ $S: $L }),", memberName, locationName,
143+
target.accept(getMemberVisitor("value")));
139144
});
140-
writer.write("_: value => value");
145+
writer.write("_: (name, value) => ({ name: value } as any)");
141146
});
142147
}
143148
}

0 commit comments

Comments
 (0)