Skip to content

Commit 55af99c

Browse files
committed
remove the event headers when serializing implicit event payload
1 parent acc7ef8 commit 55af99c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,24 +1357,21 @@ protected abstract void serializeInputDocumentBody(
13571357
* The value set is expected to by a JavaScript ${@code Uint8Array} type and is to be encoded as the
13581358
* event payload.
13591359
*
1360-
* <p>Two parameters will be available in scope:
1360+
* <p>Three parameters will be available in scope:
13611361
* <ul>
1362+
* <li>{@code body}: The serialized event payload object to needs to be serialized</li>
13621363
* <li>{@code message: <T>}: The partially constructed event message.</li>
13631364
* <li>{@code context: SerdeContext}: a TypeScript type containing context and tools for type serde.</li>
13641365
* </ul>
13651366
*
13661367
* <p>For example:
13671368
*
13681369
* <pre>{@code
1369-
* message.body = context.utf8Decoder(JSON.stringify(bodyParams));
1370+
* message.body = context.utf8Decoder(JSON.stringify(body));
13701371
* }</pre>
13711372
* @param context The generation context.
1372-
* @param payloadShape The payload shape. Only structure and union shape is serialized as document.
13731373
*/
1374-
protected abstract void serializeInputEventDocumentPayload(
1375-
GenerationContext context,
1376-
Shape payloadShape
1377-
);
1374+
protected abstract void serializeInputEventDocumentPayload(GenerationContext context);
13781375

13791376
/**
13801377
* Writes the code needed to serialize a protocol output document.
@@ -1640,7 +1637,7 @@ private void writeEventBody(GenerationContext context, StructureShape event) {
16401637
.filter(member -> member.hasTrait(EventPayloadTrait.class))
16411638
.collect(Collectors.toList());
16421639
Shape payloadShape = payloadMembers.isEmpty()
1643-
? event
1640+
? event // implicit payload
16441641
: model.expectShape(payloadMembers.get(0).getTarget());
16451642
if (payloadShape instanceof BlobShape || payloadShape instanceof StringShape) {
16461643
// Since event itself must be a structure shape, so string or blob payload member must has eventPayload
@@ -1651,7 +1648,18 @@ private void writeEventBody(GenerationContext context, StructureShape event) {
16511648
getInputValue(context, Location.PAYLOAD, "input." + payloadMemberName, payloadMember,
16521649
model.expectShape(payloadMember.getTarget())));
16531650
} else if (payloadShape instanceof StructureShape || payloadShape instanceof UnionShape) {
1654-
serializeInputEventDocumentPayload(context, payloadShape);
1651+
// handle implicit event payload by removing members with eventHeader trait.
1652+
List<MemberShape> headerMembers = event.getAllMembers().values().stream()
1653+
.filter(member -> member.hasTrait(EventHeaderTrait.class)).collect(Collectors.toList());
1654+
for (MemberShape headerMember : headerMembers) {
1655+
String memberName = headerMember.getMemberName();
1656+
writer.write("delete input[$S]", memberName);
1657+
}
1658+
SymbolProvider symbolProvider = context.getSymbolProvider();
1659+
Symbol symbol = symbolProvider.toSymbol(payloadShape);
1660+
String serFunctionName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
1661+
writer.write("const body = $L(input, context);", serFunctionName);
1662+
serializeInputEventDocumentPayload(context);
16551663
} else {
16561664
throw new CodegenException(String.format("Unexpected shape type bound to event payload: `%s`",
16571665
payloadShape.getType()));

0 commit comments

Comments
 (0)