Skip to content

Commit f2a2d92

Browse files
committed
expose serializeInputEventDocumentPayload() to generate protocol-specific event payload
1 parent 5f83ba1 commit f2a2d92

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
package software.amazon.smithy.typescript.codegen.integration;
1717

18-
import static software.amazon.smithy.model.knowledge.HttpBinding.Location;
19-
2018
import java.nio.file.Paths;
2119
import java.util.Collection;
2220
import java.util.Collections;
@@ -37,6 +35,7 @@
3735
import software.amazon.smithy.codegen.core.SymbolReference;
3836
import software.amazon.smithy.model.Model;
3937
import software.amazon.smithy.model.knowledge.HttpBinding;
38+
import software.amazon.smithy.model.knowledge.HttpBinding.Location;
4039
import software.amazon.smithy.model.knowledge.HttpBindingIndex;
4140
import software.amazon.smithy.model.knowledge.OperationIndex;
4241
import software.amazon.smithy.model.knowledge.TopDownIndex;
@@ -1350,6 +1349,32 @@ protected abstract void serializeInputDocumentBody(
13501349
List<HttpBinding> documentBindings
13511350
);
13521351

1352+
/**
1353+
* Writes the code needed to serialize an event payload as a protocol-specific document.
1354+
*
1355+
* <p>Implementations of this method are expected to set a value to the ${@code message.body} property.
1356+
* The value set is expected to by a JavaScript ${@code Uint8Array} type and is to be encoded as the
1357+
* event payload.
1358+
*
1359+
* <p>Two parameters will be available in scope:
1360+
* <ul>
1361+
* <li>{@code message: <T>}: The partially constructed event message.</li>
1362+
* <li>{@code context: SerdeContext}: a TypeScript type containing context and tools for type serde.</li>
1363+
* </ul>
1364+
*
1365+
* <p>For example:
1366+
*
1367+
* <pre>{@code
1368+
* message.body = context.utf8Decoder(JSON.stringify(bodyParams));
1369+
* }</pre>
1370+
* @param context The generation context.
1371+
* @param payloadShape The payload shape. Only structure and union shape is serialized as document.
1372+
*/
1373+
protected abstract void serializeInputEventDocumentPayload(
1374+
GenerationContext context,
1375+
Shape payloadShape
1376+
);
1377+
13531378
/**
13541379
* Writes the code needed to serialize a protocol output document.
13551380
*
@@ -1611,25 +1636,24 @@ private void writeEventBody(GenerationContext context, StructureShape event) {
16111636
TypeScriptWriter writer = context.getWriter();
16121637
Model model = context.getModel();
16131638
List<MemberShape> payloadMembers = event.getAllMembers().values().stream()
1614-
.filter(member -> member.hasTrait(EventPayloadTrait.class)).collect(Collectors.toList());
1615-
List<MemberShape> documentMembers = event.getAllMembers().values().stream()
1616-
.filter(member -> !member.hasTrait(EventHeaderTrait.class)
1617-
&& !member.hasTrait(EventPayloadTrait.class))
1639+
.filter(member -> member.hasTrait(EventPayloadTrait.class))
16181640
.collect(Collectors.toList());
1619-
if (!payloadMembers.isEmpty()) {
1620-
// Write event payload if exists. There is at most 1 payload member.
1641+
Shape payloadShape = payloadMembers.isEmpty()
1642+
? event
1643+
: model.expectShape(payloadMembers.get(0).getTarget());
1644+
if (payloadShape instanceof BlobShape || payloadShape instanceof StringShape) {
1645+
// Since event itself must be a structure shape, so string or blob payload member must has eventPayload
1646+
// trait explicitly.
16211647
MemberShape payloadMember = payloadMembers.get(0);
1622-
String memberName = payloadMember.getMemberName();
1648+
String payloadMemberName = payloadMember.getMemberName();
16231649
writer.write("message.body = $L || message.body;",
1624-
getInputValue(context, Location.PAYLOAD, "input." + memberName, payloadMember,
1650+
getInputValue(context, Location.PAYLOAD, "input." + payloadMemberName, payloadMember,
16251651
model.expectShape(payloadMember.getTarget())));
1626-
} else if (!documentMembers.isEmpty()) {
1627-
// Write event document bindings if exist.
1628-
SymbolProvider symbolProvider = context.getSymbolProvider();
1629-
Symbol symbol = symbolProvider.toSymbol(event);
1630-
// Use normal structure serializer instead of event serializer to serialize document body.
1631-
String serFunctionName = ProtocolGenerator.getSerFunctionName(symbol, context.getProtocolName());
1632-
writer.write("message.body = $L(input, context);", serFunctionName);
1652+
} else if (payloadShape instanceof StructureShape || payloadShape instanceof UnionShape) {
1653+
serializeInputEventDocumentPayload(context, payloadShape);
1654+
} else {
1655+
throw new CodegenException(String.format("Unexpected shape type bound to event payload: `%s`",
1656+
payloadShape.getType()));
16331657
}
16341658
}
16351659

0 commit comments

Comments
 (0)