|
25 | 25 | import software.amazon.smithy.model.shapes.Shape;
|
26 | 26 | import software.amazon.smithy.model.shapes.StructureShape;
|
27 | 27 | import software.amazon.smithy.model.shapes.UnionShape;
|
| 28 | +import software.amazon.smithy.model.traits.IdempotencyTokenTrait; |
28 | 29 | import software.amazon.smithy.model.traits.JsonNameTrait;
|
29 | 30 | import software.amazon.smithy.model.traits.TimestampFormatTrait;
|
30 | 31 | import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
|
@@ -88,35 +89,31 @@ public void serializeMap(GenerationContext context, MapShape shape) {
|
88 | 89 | public void serializeStructure(GenerationContext context, StructureShape shape) {
|
89 | 90 | TypeScriptWriter writer = context.getWriter();
|
90 | 91 |
|
91 |
| - writer.write("const bodyParams: any = {};"); |
92 |
| - // Use a TreeMap to sort the members. |
93 |
| - Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers()); |
94 |
| - members.forEach((memberName, memberShape) -> { |
95 |
| - // Use the jsonName trait value if present, otherwise use the member name. |
96 |
| - String locationName = memberShape.getTrait(JsonNameTrait.class) |
97 |
| - .map(JsonNameTrait::getValue) |
98 |
| - .orElse(memberName); |
99 |
| - Shape target = context.getModel().expectShape(memberShape.getTarget()); |
100 |
| - String inputLocation = "input." + memberName; |
101 |
| - |
102 |
| - // Handle if the member is an idempotency token that should be auto-filled. |
103 |
| - AwsProtocolUtils.writeIdempotencyAutofill(context, memberShape, inputLocation); |
104 |
| - |
105 |
| - // Generate an if statement to set the bodyParam if the member is set. |
106 |
| - writer.openBlock("if ($L !== undefined) {", "}", inputLocation, () -> { |
107 |
| - String dataSource = "input." + memberName; |
| 92 | + writer.openBlock("return {", "};", () -> { |
| 93 | + // Use a TreeMap to sort the members. |
| 94 | + Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers()); |
| 95 | + members.forEach((memberName, memberShape) -> { |
| 96 | + // Use the jsonName trait value if present, otherwise use the member name. |
| 97 | + String locationName = memberShape.getTrait(JsonNameTrait.class) |
| 98 | + .map(JsonNameTrait::getValue) |
| 99 | + .orElse(memberName); |
| 100 | + Shape target = context.getModel().expectShape(memberShape.getTarget()); |
| 101 | + String inputLocation = "input." + memberName; |
108 | 102 |
|
109 | 103 | // Handle @timestampFormat on members not just the targeted shape.
|
110 | 104 | String valueProvider = memberShape.hasTrait(TimestampFormatTrait.class)
|
111 | 105 | ? AwsProtocolUtils.getInputTimestampValueProvider(context, memberShape,
|
112 |
| - TIMESTAMP_FORMAT, dataSource) |
113 |
| - : target.accept(getMemberVisitor(dataSource)); |
114 |
| - |
115 |
| - // Dispatch to the input value provider for any additional handling. |
116 |
| - writer.write("bodyParams['$L'] = $L;", locationName, valueProvider); |
| 106 | + TIMESTAMP_FORMAT, inputLocation) |
| 107 | + : target.accept(getMemberVisitor(inputLocation)); |
| 108 | + |
| 109 | + if (memberShape.hasTrait(IdempotencyTokenTrait.class)) { |
| 110 | + writer.write("'$L': $L ?? generateIdempotencyToken(),", locationName, valueProvider); |
| 111 | + } else { |
| 112 | + writer.write("...($L !== undefined && { '$L': $L }),", inputLocation, locationName, valueProvider); |
| 113 | + } |
117 | 114 | });
|
| 115 | + |
118 | 116 | });
|
119 |
| - writer.write("return bodyParams;"); |
120 | 117 | }
|
121 | 118 |
|
122 | 119 | @Override
|
|
0 commit comments