Skip to content

Commit 9ee6cdc

Browse files
authored
fix(codegen): apply reserved word escaping to union shape in Json serializer (#7419)
1 parent 511167d commit 9ee6cdc

File tree

1 file changed

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

1 file changed

+10
-1
lines changed

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

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

1818
import java.util.Map;
19+
import java.util.Objects;
1920
import java.util.TreeMap;
2021
import java.util.function.BiFunction;
2122
import software.amazon.smithy.aws.typescript.codegen.validation.UnaryFunctionCall;
23+
import software.amazon.smithy.codegen.core.ReservedWords;
24+
import software.amazon.smithy.codegen.core.ReservedWordsBuilder;
2225
import software.amazon.smithy.codegen.core.Symbol;
2326
import software.amazon.smithy.codegen.core.SymbolProvider;
2427
import software.amazon.smithy.model.Model;
@@ -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,8 @@ 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, {", "});",
220+
reservedWords.escape(shape.getId().getName(serviceShape)), () -> {
212221
// Use a TreeMap to sort the members.
213222
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
214223
members.forEach((memberName, memberShape) -> {

0 commit comments

Comments
 (0)