Skip to content

Commit f4fbb62

Browse files
AllanZhengYPsrchase
authored andcommitted
fix: resolve endpoint in command serializer (smithy-lang#162)
* fix: resolve endpoint in command serializer The endpoint used to be a function returning a promise of Endpoint, and it is resolved in serializer parameter. It introduces mismatch between SerDeContext type and the value, where the value contains resolved endpoint but the type is a provider function returning Endpoint promise. This change assumes the SerDeContext.endpoint is actually a provider function and only resolve the promise when constructing a request. This change also fix the issue that the request resolved path got overwritten by '/'. Now only the hostname, port and scheme from client endpoint config is persisted when creating a request. * add inline documents
1 parent 9dbc5a9 commit f4fbb62

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,21 @@ private void generateOperationSerializer(
227227
HttpProtocolGeneratorUtils.writeHostPrefix(context, operation);
228228
}
229229

230+
// Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from
231+
// them. The client's resolved endpoint can be default one or supplied by users.
232+
writer.write("const {hostname, protocol = \"https\", port} = await context.endpoint();");
230233
writer.openBlock("return new $T({", "});", requestType, () -> {
231234
if (hasHostPrefix) {
232235
writer.write("hostname: resolvedHostname,");
233236
}
234-
writer.write("protocol: \"https\",");
237+
writer.write("protocol,");
238+
writer.write("hostname,");
239+
writer.write("port,");
235240
writer.write("method: $S,", trait.getMethod());
236-
writer.write("headers: headers,");
241+
writer.write("headers,");
237242
writer.write("path: resolvedPath,");
238243
if (hasQueryComponents) {
239-
writer.write("query: query,");
244+
writer.write("query,");
240245
}
241246
if (!bodyBindings.isEmpty()) {
242247
// Track all shapes bound to the body so their serializers may be generated.
@@ -246,8 +251,7 @@ private void generateOperationSerializer(
246251
.forEach(serializingDocumentShapes::add);
247252
}
248253
// Always set the body,
249-
writer.write("body: body,");
250-
writer.write("...context.endpoint,");
254+
writer.write("body,");
251255
});
252256
});
253257

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpRpcProtocolGenerator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,23 @@ public void generateSharedComponents(GenerationContext context) {
100100
writer.addUseImports(requestType);
101101
writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
102102
writer.addImport("HeaderBag", "__HeaderBag", "@aws-sdk/types");
103-
writer.openBlock("const buildHttpRpcRequest = (\n"
103+
writer.openBlock("const buildHttpRpcRequest = async (\n"
104104
+ " context: __SerdeContext,\n"
105105
+ " headers: __HeaderBag,\n"
106106
+ " path: string,\n"
107107
+ " resolvedHostname: string | undefined,\n"
108108
+ " body: any,\n"
109-
+ "): $T => {", "};", requestType, () -> {
109+
+ "): Promise<$T> => {", "};", requestType, () -> {
110+
// Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from
111+
// them. The client's resolved endpoint can be default one or supplied by users.
112+
writer.write("const {hostname, protocol = \"https\", port} = await context.endpoint();");
110113
writer.openBlock("const contents: any = {", "};", () -> {
111-
writer.write("protocol: \"https\",");
114+
writer.write("protocol,");
115+
writer.write("hostname,");
116+
writer.write("port,");
112117
writer.write("method: \"POST\",");
113-
writer.write("path: path,");
114-
writer.write("headers: headers,");
115-
writer.write("...context.endpoint,");
118+
writer.write("path,");
119+
writer.write("headers,");
116120
});
117121
writer.openBlock("if (resolvedHostname !== undefined) {", "}", () -> {
118122
writer.write("contents.hostname = resolvedHostname;");

0 commit comments

Comments
 (0)