Skip to content

Commit 6297831

Browse files
committed
address feedbacks: fix indentation; readResponseBody; add docs
1 parent c9454c4 commit 6297831

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ private List<HttpBinding> readResponseBody(
720720
.orElse(false);
721721

722722
if (!documentBindings.isEmpty()) {
723+
// If response has document binding, the body can be parsed to JavaScript object.
723724
writer.write("const data: any = await parseBody(output.body, context);");
724725
deserializeOutputDocument(context, operationOrError, documentBindings);
725726
return documentBindings;
@@ -734,12 +735,11 @@ private List<HttpBinding> readResponseBody(
734735
} else if (target instanceof BlobShape) {
735736
// If payload is blob, only need to collect stream to binary data(Uint8Array).
736737
writer.write("const data: any = await collectBody(output.body, context);");
737-
} else if (target instanceof CollectionShape || target instanceof StructureShape) {
738-
// 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.
739740
writer.write("const data: any = await parseBody(output.body, context);");
740741
} else {
741-
// If payload is other scalar types(not Collection or Structure), because payload will be values in
742-
// 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.
743743
writer.write("const data: any = await collectBodyString(output.body, context);");
744744
}
745745
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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static void generateCollectBody(GenerationContext context) {
112112
TypeScriptWriter writer = context.getWriter();
113113

114114
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
115+
writer.write("// Collect low-level response body stream to Uint8Array.");
115116
writer.openBlock("const collectBody = (streamBody: any, context: __SerdeContext): Promise<Uint8Array> => {",
116117
"};", () -> {
117118
writer.write("return context.streamCollector(streamBody) || new Uint8Array();");
@@ -124,6 +125,7 @@ static void generateCollectBodyString(GenerationContext context) {
124125
TypeScriptWriter writer = context.getWriter();
125126

126127
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
128+
writer.write("// Encode Uint8Array data into string with utf-8.");
127129
writer.openBlock("const collectBodyString = (streamBody: any, context: __SerdeContext): Promise<string> => {",
128130
"};", () -> {
129131
writer.write("return collectBody(streamBody, context).then(body => context.utf8Encoder(body));");
@@ -167,11 +169,12 @@ static Set<StructureShape> generateErrorDispatcher(
167169
// Prepare error response for parsing error code. If error code needs to be parsed from response body
168170
// then we collect body and parse it to JS object, otherwise leave the response body as is.
169171
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-
});
172+
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput",
173+
() -> {
174+
writer.write("...output,");
175+
writer.write("body: $L,",
176+
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
177+
});
175178

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

0 commit comments

Comments
 (0)