From cebae8a41a158a3f1cec241b68a0ec84f3cefef7 Mon Sep 17 00:00:00 2001 From: Allan Zheng Date: Wed, 8 Jan 2020 15:36:38 -0800 Subject: [PATCH] fix: parse body only once for error response --- .../aws/typescript/codegen/AwsProtocolUtils.java | 3 +-- .../typescript/codegen/JsonRpcProtocolGenerator.java | 11 +++++++++-- .../typescript/codegen/RestJsonProtocolGenerator.java | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java index 71312db1b064..3db4e51f42f7 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsProtocolUtils.java @@ -88,8 +88,7 @@ static void generateJsonParseBody(GenerationContext context) { // Include a JSON body parser used to deserialize documents from HTTP responses. writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types"); writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): any => {", "};", () -> { - writer.openBlock("return context.streamCollector(streamBody).then((body: any) => {", "});", () -> { - writer.write("const encoded = context.utf8Encoder(body);"); + writer.openBlock("return collectBodyString(streamBody, context).then(encoded => {", "});", () -> { writer.openBlock("if (encoded.length) {", "}", () -> { writer.write("return JSON.parse(encoded);"); }); diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java index cd3bc6db8ef3..322fb26f65a2 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/JsonRpcProtocolGenerator.java @@ -41,6 +41,13 @@ */ abstract class JsonRpcProtocolGenerator extends HttpRpcProtocolGenerator { + /** + * Creates a AWS JSON RPC protocol generator. + */ + JsonRpcProtocolGenerator() { + super(true); + } + protected Format getDocumentTimestampFormat() { return Format.EPOCH_SECONDS; } @@ -96,8 +103,8 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context, @Override protected void writeErrorCodeParser(GenerationContext context) { TypeScriptWriter writer = context.getWriter(); - - writer.write("const errorTypeParts: String = data[\"__type\"].split('#');"); + // parsedOutput is in scope because HttpRpcProtocolGenerator.isErrorCodeInBody is true + writer.write("const errorTypeParts: String = parsedOutput.body[\"__type\"].split('#');"); writer.write("errorCode = (errorTypeParts[1] === undefined) ? errorTypeParts[0] : errorTypeParts[1];"); } diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java index 414c395b0bba..28ecf4a64aa0 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java @@ -45,6 +45,12 @@ * @see Smithy HTTP protocol bindings. */ abstract class RestJsonProtocolGenerator extends HttpBindingProtocolGenerator { + /** + * Creates a AWS JSON RPC protocol generator. + */ + RestJsonProtocolGenerator() { + super(false); + } @Override protected TimestampFormatTrait.Format getDocumentTimestampFormat() {