Skip to content

Commit

Permalink
fix: stop adding serde middleware repeatedly (smithy-lang#259)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AllanZhengYP authored Jan 8, 2021
1 parent 2b4b2c0 commit c57cca3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public void addsCommandSpecificPlugins() {
" configuration: ExampleClientResolvedConfig,\n" +
" options?: __HttpHandlerOptions\n" +
" ): Handler<GetFooCommandInput, GetFooCommandOutput> {\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);");
}
Expand Down

0 comments on commit c57cca3

Please sign in to comment.