Skip to content

Commit b823d08

Browse files
adamthom-amznsrchase
authored andcommitted
Fix serialization of REST-JSON requests when the endpoint has a path
Endpoints can have paths (notable example: API Gateway REST APIs without a custom domain name), and serialization of REST-JSON requests was ignoring these paths even though it's available from the endpoint provider.
1 parent 70181fa commit b823d08

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,11 @@ private void generateOperationRequestSerializer(
599599
+ " input: $T,\n"
600600
+ " context: $L\n"
601601
+ "): Promise<$T> => {", "}", methodName, inputType, contextType, requestType, () -> {
602+
603+
// Get the hostname, path, port, and scheme from client's resolved endpoint. Then construct the request from
604+
// them. The client's resolved endpoint can be default one or supplied by users.
605+
writer.write("const {hostname, protocol = $S, port, path: basePath} = await context.endpoint();", "https");
606+
602607
writeRequestHeaders(context, operation, bindingIndex);
603608
writeResolvedPath(context, operation, bindingIndex, trait);
604609
boolean hasQueryComponents = writeRequestQueryString(context, operation, bindingIndex, trait);
@@ -616,10 +621,6 @@ private void generateOperationRequestSerializer(
616621
if (hasHostPrefix) {
617622
HttpProtocolGeneratorUtils.writeHostPrefix(context, operation);
618623
}
619-
620-
// Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from
621-
// them. The client's resolved endpoint can be default one or supplied by users.
622-
writer.write("const {hostname, protocol = \"https\", port} = await context.endpoint();");
623624
writer.openBlock("return new $T({", "});", requestType, () -> {
624625
writer.write("protocol,");
625626
if (hasHostPrefix) {
@@ -683,7 +684,9 @@ private void writeResolvedPath(
683684
List<HttpBinding> labelBindings = bindingIndex.getRequestBindings(operation, Location.LABEL);
684685

685686
// Always write the bound path, but only the actual segments.
686-
writer.write("let resolvedPath = $S;", "/" + trait.getUri().getSegments().stream()
687+
writer.write("let resolvedPath = `$L` + $S;",
688+
"${basePath?.endsWith('/') ? basePath.slice(0, -1) : (basePath || '')}",
689+
"/" + trait.getUri().getSegments().stream()
687690
.map(Segment::toString)
688691
.collect(Collectors.joining("/")));
689692

0 commit comments

Comments
 (0)