|
24 | 24 | import software.amazon.smithy.model.shapes.Shape;
|
25 | 25 | import software.amazon.smithy.model.shapes.StructureShape;
|
26 | 26 | import software.amazon.smithy.model.shapes.UnionShape;
|
| 27 | +import software.amazon.smithy.model.traits.IdempotencyTokenTrait; |
27 | 28 | import software.amazon.smithy.model.traits.JsonNameTrait;
|
28 | 29 | import software.amazon.smithy.model.traits.TimestampFormatTrait;
|
29 | 30 | import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
|
@@ -102,35 +103,31 @@ public void serializeInputDocument(
|
102 | 103 |
|
103 | 104 | SymbolProvider symbolProvider = context.getSymbolProvider();
|
104 | 105 |
|
105 |
| - writer.write("const bodyParams: any = {};"); |
106 |
| - for (HttpBinding binding : documentBindings) { |
107 |
| - MemberShape memberShape = binding.getMember(); |
108 |
| - // The name of the member to get from the input shape. |
109 |
| - String memberName = symbolProvider.toMemberName(memberShape); |
110 |
| - String inputLocation = "input." + memberName; |
111 |
| - // Use the jsonName trait value if present, otherwise use the member name. |
112 |
| - String locationName = memberShape.getTrait(JsonNameTrait.class) |
113 |
| - .map(JsonNameTrait::getValue) |
114 |
| - .orElseGet(binding::getLocationName); |
115 |
| - Shape target = context.getModel().expectShape(memberShape.getTarget()); |
116 |
| - |
117 |
| - // Handle if the member is an idempotency token that should be auto-filled. |
118 |
| - AwsProtocolUtils.writeIdempotencyAutofill(context, memberShape, inputLocation); |
119 |
| - |
120 |
| - // Generate an if statement to set the bodyParam if the member is set. |
121 |
| - writer.openBlock("if ($L !== undefined) {", "}", inputLocation, () -> { |
| 106 | + writer.openBlock("body = JSON.stringify({", "});", () -> { |
| 107 | + for (HttpBinding binding : documentBindings) { |
| 108 | + MemberShape memberShape = binding.getMember(); |
| 109 | + // The name of the member to get from the input shape. |
| 110 | + String memberName = symbolProvider.toMemberName(memberShape); |
| 111 | + String inputLocation = "input." + memberName; |
| 112 | + // Use the jsonName trait value if present, otherwise use the member name. |
| 113 | + String locationName = memberShape.getTrait(JsonNameTrait.class) |
| 114 | + .map(JsonNameTrait::getValue) |
| 115 | + .orElseGet(binding::getLocationName); |
| 116 | + Shape target = context.getModel().expectShape(memberShape.getTarget()); |
| 117 | + |
122 | 118 | // Handle @timestampFormat on members not just the targeted shape.
|
123 | 119 | String valueProvider = memberShape.hasTrait(TimestampFormatTrait.class)
|
124 | 120 | ? AwsProtocolUtils.getInputTimestampValueProvider(context, memberShape,
|
125 | 121 | getDocumentTimestampFormat(), inputLocation)
|
126 | 122 | : target.accept(getMemberSerVisitor(context, inputLocation));
|
127 | 123 |
|
128 |
| - // Dispatch to the input value provider for any additional handling. |
129 |
| - writer.write("bodyParams['$L'] = $L;", locationName, valueProvider); |
130 |
| - }); |
131 |
| - } |
132 |
| - |
133 |
| - writer.write("body = JSON.stringify(bodyParams);"); |
| 124 | + if (memberShape.hasTrait(IdempotencyTokenTrait.class)) { |
| 125 | + writer.write("'$L': $L ?? generateIdempotencyToken(),", locationName, valueProvider); |
| 126 | + } else { |
| 127 | + writer.write("...($L !== undefined && { '$L': $L }),", inputLocation, locationName, valueProvider); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
134 | 131 | }
|
135 | 132 |
|
136 | 133 | @Override
|
|
0 commit comments