Skip to content

Commit 851b17f

Browse files
Calculate content-length for SSDKs
This updates SSDK generation so that content-length is properly calculated. The existing functionality for clients couldn't be re-used because it is designed to function as a middleware that runs just beforee the request is sent. That makes sense for the client, because a middleware could be modifying the body. But since the SSDK doesn't have middleware, we just embed it directly.
1 parent 652cc99 commit 851b17f

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ private void generateOperationResponseSerializer(
506506
.forEach(serializingDocumentShapes::add);
507507
}
508508

509+
calculateContentLength(context);
510+
509511
writer.openBlock("return new $T({", "});", responseType, () -> {
510512
writer.write("headers,");
511513
writer.write("body,");
@@ -519,6 +521,20 @@ private void generateOperationResponseSerializer(
519521
}
520522
}
521523

524+
private void calculateContentLength(GenerationContext context) {
525+
TypeScriptWriter writer = context.getWriter();
526+
writer.addDependency(TypeScriptDependency.AWS_SDK_UTIL_BODY_LENGTH_NODE);
527+
writer.addImport("calculateBodyLength", null, "@aws-sdk/util-body-length-node");
528+
writer.openBlock("if (body && Object.keys(headers).map((str) => str.toLowerCase())"
529+
+ ".indexOf('content-length') === -1) {", "}", () -> {
530+
writer.write("const length = calculateBodyLength(body);");
531+
writer.openBlock("if (length !== undefined) {", "}", () -> {
532+
writer.write("headers = { ...headers, 'content-length': String(length) };");
533+
});
534+
});
535+
536+
}
537+
522538
private void generateErrorSerializer(GenerationContext context, StructureShape error) {
523539
SymbolProvider symbolProvider = context.getSymbolProvider();
524540
Symbol symbol = symbolProvider.toSymbol(error);
@@ -846,7 +862,7 @@ private void writeResponseHeaders(
846862
TypeScriptWriter writer = context.getWriter();
847863

848864
// Headers are always present either from the default document or the payload.
849-
writer.openBlock("const headers: any = {", "};", () -> {
865+
writer.openBlock("let headers: any = {", "};", () -> {
850866
writeContentTypeHeader(context, operationOrError, false);
851867
injectExtraHeaders.run();
852868

0 commit comments

Comments
 (0)