Skip to content

Commit f009b41

Browse files
AllanZhengYPsrchase
authored andcommitted
fix: stop adding serde middleware repeatedly (smithy-lang#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: aws/aws-sdk-js-v3#1864
1 parent ed66709 commit f009b41

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public void run() {
104104
writer.writeShapeDocs(operation);
105105
writer.openBlock("export class $L extends $$Command<$T, $T, $L> {", "}", name, inputType, outputType,
106106
configType, () -> {
107+
writer.write("private resolved = false;");
107108

108109
// Section for adding custom command properties.
109110
writer.write("// Start section: $L", COMMAND_PROPERTIES_SECTION);
@@ -145,11 +146,15 @@ private void generateCommandMiddlewareResolver(String configType) {
145146
.write("options?: $T", applicationProtocol.getOptionsType())
146147
.dedent();
147148
writer.openBlock("): Handler<$T, $T> {", "}", inputType, outputType, () -> {
148-
// Add serialization and deserialization plugin.
149-
writer.write("this.middlewareStack.use($T(configuration, this.serialize, this.deserialize));", serde);
149+
writer.openBlock("if (!this.resolved) {", "}", () -> {
150+
// Add serialization and deserialization plugin.
151+
writer.write("this.middlewareStack.use($T(configuration, this.serialize, this.deserialize));", serde);
150152

151-
// Add customizations.
152-
addCommandSpecificPlugins();
153+
// Add customizations.
154+
addCommandSpecificPlugins();
155+
156+
writer.write("this.resolved = true;");
157+
});
153158

154159
// Resolve the middleware stack.
155160
writer.write("\nconst stack = clientStack.concat(this.middlewareStack);\n");

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public void addsCommandSpecificPlugins() {
1818
" configuration: ExampleClientResolvedConfig,\n" +
1919
" options?: __HttpHandlerOptions\n" +
2020
" ): Handler<GetFooCommandInput, GetFooCommandOutput> {\n" +
21-
" this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" +
21+
" if (!this.resolved) {\n" +
22+
" this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" +
23+
" this.resolved = true;\n" +
24+
" }\n" +
2225
"\n" +
2326
" const stack = clientStack.concat(this.middlewareStack);");
2427
}

0 commit comments

Comments
 (0)