Skip to content

Commit b6dbeed

Browse files
committed
handle unset unions
1 parent 38d7253 commit b6dbeed

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

.changeset/witty-rings-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
add quoteHeader function
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { quoteHeader } from "./quote-header";
2+
3+
describe(quoteHeader.name, () => {
4+
it("should wrap header elements that include the delimiter", () => {
5+
expect(quoteHeader("b,c")).toBe('"b,c"');
6+
});
7+
8+
it("should not wrap header elements that don't include the delimiter", () => {
9+
expect(quoteHeader("bc")).toBe("bc");
10+
});
11+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @public
3+
* @param part - header list element
4+
* @returns quoted string if part contains delimiter.
5+
*/
6+
export function quoteHeader(part: string) {
7+
return part.includes(",") ? `"${part}"` : part;
8+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,11 @@ private void writeHttpHostAssertion(HttpRequestTestCase testCase) {
545545
}
546546

547547
private void writeHttpBodyAssertions(String body, String mediaType, boolean isClientTest) {
548-
// If we expect an empty body, expect it to be falsy.
549548
if (body.isEmpty()) {
550-
writer.write("expect(r.body).toBeFalsy();");
549+
// If we expect an empty body, expect it to be falsy.
550+
// Or, for JSON an empty object represents an empty body.
551+
// mediaType is often UNKNOWN here.
552+
writer.write("expect(!r.body || r.body === `{}`).toBeTruthy();");
551553
return;
552554
}
553555

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.typescript.codegen.integration;
1717

1818
import java.nio.file.Paths;
19+
import java.sql.Types;
1920
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.Comparator;
@@ -1385,7 +1386,8 @@ private String getCollectionInputParam(
13851386

13861387
switch (bindingType) {
13871388
case HEADER:
1388-
return iteratedParam + ".join(', ')";
1389+
context.getWriter().addImport("quoteHeader", "__quoteHeader", TypeScriptDependency.AWS_SMITHY_CLIENT);
1390+
return iteratedParam + ".map(__quoteHeader).join(', ')";
13891391
case QUERY:
13901392
case QUERY_PARAMS:
13911393
return iteratedParam;
@@ -2466,16 +2468,29 @@ private HttpBinding readPayload(
24662468
// If payload is a Union, then we need to parse the string into JavaScript object.
24672469
importUnionDeserializer(writer);
24682470
writer.write("const data: Record<string, any> | undefined "
2469-
+ "= __expectUnion(await parseBody(output.body, context));");
2471+
+ "= await parseBody(output.body, context);");
24702472
} else if (target instanceof StringShape || target instanceof DocumentShape) {
24712473
// If payload is String or Document, we need to collect body and convert binary to string.
24722474
writer.write("const data: any = await collectBodyString(output.body, context);");
24732475
} else {
24742476
throw new CodegenException(String.format("Unexpected shape type bound to payload: `%s`",
24752477
target.getType()));
24762478
}
2477-
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
2479+
2480+
if (target instanceof UnionShape) {
2481+
writer.openBlock(
2482+
"if (Object.keys(data ?? {}).length) {",
2483+
"}",
2484+
() -> {
2485+
writer.write("contents.$L = __expectUnion($L);", binding.getMemberName(), getOutputValue(context,
2486+
Location.PAYLOAD, "data", binding.getMember(), target));
2487+
}
2488+
);
2489+
} else {
2490+
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
24782491
Location.PAYLOAD, "data", binding.getMember(), target));
2492+
}
2493+
24792494
return binding;
24802495
}
24812496

0 commit comments

Comments
 (0)