Skip to content

Commit 3e9599c

Browse files
AllanZhengYPkstich
authored andcommitted
fix: parse body only once for error response (#694)
1 parent 2ea9328 commit 3e9599c

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ static void generateJsonParseBody(GenerationContext context) {
8888
// Include a JSON body parser used to deserialize documents from HTTP responses.
8989
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
9090
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): any => {", "};", () -> {
91-
writer.openBlock("return context.streamCollector(streamBody).then((body: any) => {", "});", () -> {
92-
writer.write("const encoded = context.utf8Encoder(body);");
91+
writer.openBlock("return collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
9392
writer.openBlock("if (encoded.length) {", "}", () -> {
9493
writer.write("return JSON.parse(encoded);");
9594
});

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
*/
4242
abstract class JsonRpcProtocolGenerator extends HttpRpcProtocolGenerator {
4343

44+
/**
45+
* Creates a AWS JSON RPC protocol generator.
46+
*/
47+
JsonRpcProtocolGenerator() {
48+
super(true);
49+
}
50+
4451
protected Format getDocumentTimestampFormat() {
4552
return Format.EPOCH_SECONDS;
4653
}
@@ -96,8 +103,8 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
96103
@Override
97104
protected void writeErrorCodeParser(GenerationContext context) {
98105
TypeScriptWriter writer = context.getWriter();
99-
100-
writer.write("const errorTypeParts: String = data[\"__type\"].split('#');");
106+
// parsedOutput is in scope because HttpRpcProtocolGenerator.isErrorCodeInBody is true
107+
writer.write("const errorTypeParts: String = parsedOutput.body[\"__type\"].split('#');");
101108
writer.write("errorCode = (errorTypeParts[1] === undefined) ? errorTypeParts[0] : errorTypeParts[1];");
102109
}
103110

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
* @see <a href="https://awslabs.github.io/smithy/spec/http.html">Smithy HTTP protocol bindings.</a>
4646
*/
4747
abstract class RestJsonProtocolGenerator extends HttpBindingProtocolGenerator {
48+
/**
49+
* Creates a AWS JSON RPC protocol generator.
50+
*/
51+
RestJsonProtocolGenerator() {
52+
super(false);
53+
}
4854

4955
@Override
5056
protected TimestampFormatTrait.Format getDocumentTimestampFormat() {

0 commit comments

Comments
 (0)