From 38ffa88685cda697f840f95d3bd1e19d01ba9a5b Mon Sep 17 00:00:00 2001 From: AllanFly120 Date: Fri, 10 Jan 2020 09:40:01 -0800 Subject: [PATCH] fix: parse body only once for error response (#694) --- .../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() {