Skip to content

Commit faba30a

Browse files
committed
address feedbacks: fix indentation; readResponseBody; add docs
1 parent def3531 commit faba30a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import software.amazon.smithy.model.shapes.NumberShape;
4949
import software.amazon.smithy.model.shapes.OperationShape;
5050
import software.amazon.smithy.model.shapes.Shape;
51-
import software.amazon.smithy.model.shapes.ShapeType;
5251
import software.amazon.smithy.model.shapes.StringShape;
5352
import software.amazon.smithy.model.shapes.StructureShape;
5453
import software.amazon.smithy.model.shapes.TimestampShape;
@@ -721,6 +720,7 @@ private List<HttpBinding> readResponseBody(
721720
.orElse(false);
722721

723722
if (!documentBindings.isEmpty()) {
723+
// If response has document binding, the body can be parsed to JavaScript object.
724724
writer.write("const data: any = await parseBody(output.body, context);");
725725
deserializeOutputDocument(context, operationOrError, documentBindings);
726726
return documentBindings;
@@ -735,12 +735,11 @@ private List<HttpBinding> readResponseBody(
735735
} else if (target instanceof BlobShape) {
736736
// If payload is blob, only need to collect stream to binary data(Uint8Array).
737737
writer.write("const data: any = await collectBody(output.body, context);");
738-
} else if (target instanceof CollectionShape || target instanceof StructureShape) {
739-
// If body is Collection or Structure, they we need to parse the string into JavaScript object.
738+
} else if (target instanceof StructureShape || target instanceof UnionShape) {
739+
// If body is Structure or Union, they we need to parse the string into JavaScript object.
740740
writer.write("const data: any = await parseBody(output.body, context);");
741741
} else {
742-
// If payload is other scalar types(not Collection or Structure), because payload will be values in
743-
// string instead of valid JSON or XML. So we need to collect body and encode binary to string.
742+
// If payload is string, we need to collect body and encode binary to string.
744743
writer.write("const data: any = await collectBodyString(output.body, context);");
745744
}
746745
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,17 @@ static void generateMetadataDeserializer(GenerationContext context, SymbolRefere
108108
writer.write("");
109109
}
110110

111+
/**
112+
* Writes a response body stream collector. This function converts the low-level response body stream to
113+
* Uint8Array binary data.
114+
*
115+
* @param context The generation context.
116+
*/
111117
static void generateCollectBody(GenerationContext context) {
112118
TypeScriptWriter writer = context.getWriter();
113119

114120
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
121+
writer.write("// Collect low-level response body stream to Uint8Array.");
115122
writer.openBlock("const collectBody = (streamBody: any, context: __SerdeContext): Promise<Uint8Array> => {",
116123
"};", () -> {
117124
writer.write("return context.streamCollector(streamBody) || new Uint8Array();");
@@ -120,10 +127,17 @@ static void generateCollectBody(GenerationContext context) {
120127
writer.write("");
121128
}
122129

130+
/**
131+
* Writes a function converting the low-level response body stream to utf-8 encoded string. It depends on
132+
* response body stream collector{@link #generateCollectBody(GenerationContext)}.
133+
*
134+
* @param context The generation context
135+
*/
123136
static void generateCollectBodyString(GenerationContext context) {
124137
TypeScriptWriter writer = context.getWriter();
125138

126139
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
140+
writer.write("// Encode Uint8Array data into string with utf-8.");
127141
writer.openBlock("const collectBodyString = (streamBody: any, context: __SerdeContext): Promise<string> => {",
128142
"};", () -> {
129143
writer.write("return collectBody(streamBody, context).then(body => context.utf8Encoder(body));");
@@ -167,11 +181,12 @@ static Set<StructureShape> generateErrorDispatcher(
167181
// Prepare error response for parsing error code. If error code needs to be parsed from response body
168182
// then we collect body and parse it to JS object, otherwise leave the response body as is.
169183
writer.openBlock(
170-
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput", () -> {
171-
writer.write("...output,");
172-
writer.write("body: $L,",
173-
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
174-
});
184+
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput",
185+
() -> {
186+
writer.write("...output,");
187+
writer.write("body: $L,",
188+
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
189+
});
175190

176191
// Error responses must be at least SmithyException and MetadataBearer implementations.
177192
writer.addImport("SmithyException", "__SmithyException",

0 commit comments

Comments
 (0)