Skip to content

Commit 7666f3d

Browse files
committed
fix(codegen): apply reserved word escaping to union shape in Json serializer
1 parent 511167d commit 7666f3d

File tree

1 file changed

+9
-1
lines changed
  • codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen

1 file changed

+9
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
package software.amazon.smithy.aws.typescript.codegen;
1717

1818
import java.util.Map;
19+
import java.util.Objects;
1920
import java.util.TreeMap;
21+
import software.amazon.smithy.codegen.core.ReservedWords;
22+
import software.amazon.smithy.codegen.core.ReservedWordsBuilder;
2023
import java.util.function.BiFunction;
2124
import software.amazon.smithy.aws.typescript.codegen.validation.UnaryFunctionCall;
2225
import software.amazon.smithy.codegen.core.Symbol;
@@ -35,6 +38,7 @@
3538
import software.amazon.smithy.model.traits.SparseTrait;
3639
import software.amazon.smithy.model.traits.TimestampFormatTrait;
3740
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
41+
import software.amazon.smithy.typescript.codegen.TypeScriptClientCodegenPlugin;
3842
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
3943
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
4044
import software.amazon.smithy.typescript.codegen.integration.DocumentMemberSerVisitor;
@@ -57,6 +61,10 @@ final class JsonShapeSerVisitor extends DocumentShapeSerVisitor {
5761

5862
private final BiFunction<MemberShape, String, String> memberNameStrategy;
5963

64+
private final ReservedWords reservedWords = new ReservedWordsBuilder()
65+
.loadWords(Objects.requireNonNull(TypeScriptClientCodegenPlugin.class.getResource("reserved-words.txt")))
66+
.build();
67+
6068
JsonShapeSerVisitor(GenerationContext context, boolean serdeElisionEnabled) {
6169
this(context,
6270
// Use the jsonName trait value if present, otherwise use the member name.
@@ -208,7 +216,7 @@ public void serializeUnion(GenerationContext context, UnionShape shape) {
208216
ServiceShape serviceShape = context.getService();
209217

210218
// Visit over the union type, then get the right serialization for the member.
211-
writer.openBlock("return $L.visit(input, {", "});", shape.getId().getName(serviceShape), () -> {
219+
writer.openBlock("return $L.visit(input, {", "});", reservedWords.escape(shape.getId().getName(serviceShape)), () -> {
212220
// Use a TreeMap to sort the members.
213221
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
214222
members.forEach((memberName, memberShape) -> {

0 commit comments

Comments
 (0)