From 69ca9e73531e2c0a78ef413d9548f61a8216564a Mon Sep 17 00:00:00 2001 From: Adam Thomas Date: Fri, 25 Jun 2021 10:49:29 -0700 Subject: [PATCH 1/2] 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. --- .../integration/HttpBindingProtocolGenerator.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java index 7fdc1884640..576f2708af6 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/HttpBindingProtocolGenerator.java @@ -599,6 +599,11 @@ private void generateOperationRequestSerializer( + " input: $T,\n" + " context: $L\n" + "): Promise<$T> => {", "}", methodName, inputType, contextType, requestType, () -> { + + // Get the hostname, path, port, and scheme from client's resolved endpoint. Then construct the request from + // them. The client's resolved endpoint can be default one or supplied by users. + writer.write("const {hostname, protocol = $S, port, path: basePath} = await context.endpoint();", "https"); + writeRequestHeaders(context, operation, bindingIndex); writeResolvedPath(context, operation, bindingIndex, trait); boolean hasQueryComponents = writeRequestQueryString(context, operation, bindingIndex, trait); @@ -616,10 +621,6 @@ private void generateOperationRequestSerializer( if (hasHostPrefix) { HttpProtocolGeneratorUtils.writeHostPrefix(context, operation); } - - // Get the hostname, port, and scheme from client's resolved endpoint. Then construct the request from - // them. The client's resolved endpoint can be default one or supplied by users. - writer.write("const {hostname, protocol = \"https\", port} = await context.endpoint();"); writer.openBlock("return new $T({", "});", requestType, () -> { writer.write("protocol,"); if (hasHostPrefix) { @@ -683,7 +684,9 @@ private void writeResolvedPath( List labelBindings = bindingIndex.getRequestBindings(operation, Location.LABEL); // Always write the bound path, but only the actual segments. - writer.write("let resolvedPath = $S;", "/" + trait.getUri().getSegments().stream() + writer.write("let resolvedPath = `$L` + $S;", + "${basePath?.endsWith('/') ? basePath.slice(0, -1) : (basePath || '')}", + "/" + trait.getUri().getSegments().stream() .map(Segment::toString) .collect(Collectors.joining("/"))); From 02dc06b1ba4674bf5e963725ea5de4a40ebaaf42 Mon Sep 17 00:00:00 2001 From: Adam Thomas Date: Fri, 25 Jun 2021 15:15:28 -0700 Subject: [PATCH 2/2] Add endpoint to protocol test client generation --- .../smithy/typescript/codegen/HttpProtocolTestGenerator.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java index 8c9fc1096eb..063cdc63756 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java @@ -235,6 +235,9 @@ private void generateClientRequestTest(OperationShape operation, HttpRequestTest // Create a client with a custom request handler that intercepts requests. writer.openBlock("const client = new $T({", "});\n", serviceSymbol, () -> { writer.write("...clientParams,"); + testCase.getHost().ifPresent(host -> { + writer.write("endpoint: \"https://$L\",", host); + }); writer.write("requestHandler: new RequestSerializationTestHandler(),"); });