From c57cca31d2216849ef123d19b86ea0fab6eb12d4 Mon Sep 17 00:00:00 2001 From: AllanZhengYP Date: Thu, 7 Jan 2021 16:09:08 -0800 Subject: [PATCH] fix: stop adding serde middleware repeatedly (#259) When sending the same command multiple times, resolveMiddleware() will be called many times. So adding serde middleware will throw error because they are already added into stack. This change prevents adding the serde middleware repeatedly. Alternative is moving command middleware to the command constructor, just like in client(that's why client doesn't have the problem). But the command middleware also have depdency over client configs supplied from resolveMiddleware(). So serde middleware and customizations must live here. ref: https://github.com/aws/aws-sdk-js-v3/issues/1864 --- .../smithy/typescript/codegen/CommandGenerator.java | 13 +++++++++---- .../typescript/codegen/CommandGeneratorTest.java | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index c5f5947ddbc..39c930fa6d3 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -104,6 +104,7 @@ public void run() { writer.writeShapeDocs(operation); writer.openBlock("export class $L extends $$Command<$T, $T, $L> {", "}", name, inputType, outputType, configType, () -> { + writer.write("private resolved = false;"); // Section for adding custom command properties. writer.write("// Start section: $L", COMMAND_PROPERTIES_SECTION); @@ -145,11 +146,15 @@ private void generateCommandMiddlewareResolver(String configType) { .write("options?: $T", applicationProtocol.getOptionsType()) .dedent(); writer.openBlock("): Handler<$T, $T> {", "}", inputType, outputType, () -> { - // Add serialization and deserialization plugin. - writer.write("this.middlewareStack.use($T(configuration, this.serialize, this.deserialize));", serde); + writer.openBlock("if (!this.resolved) {", "}", () -> { + // Add serialization and deserialization plugin. + writer.write("this.middlewareStack.use($T(configuration, this.serialize, this.deserialize));", serde); - // Add customizations. - addCommandSpecificPlugins(); + // Add customizations. + addCommandSpecificPlugins(); + + writer.write("this.resolved = true;"); + }); // Resolve the middleware stack. writer.write("\nconst stack = clientStack.concat(this.middlewareStack);\n"); diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java index a2468ba1a04..ca22a49e6c3 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java @@ -18,7 +18,10 @@ public void addsCommandSpecificPlugins() { " configuration: ExampleClientResolvedConfig,\n" + " options?: __HttpHandlerOptions\n" + " ): Handler {\n" + - " this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" + + " if (!this.resolved) {\n" + + " this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" + + " this.resolved = true;\n" + + " }\n" + "\n" + " const stack = clientStack.concat(this.middlewareStack);"); }