Skip to content

Commit

Permalink
handle unset unions
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Sep 26, 2024
1 parent 38d7253 commit e2e28cf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-rings-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/smithy-client": minor
---

add quoteHeader function
1 change: 1 addition & 0 deletions packages/smithy-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from "./lazy-json";
export * from "./NoOpLogger";
export * from "./object-mapping";
export * from "./parse-utils";
export * from "./quote-header";
export * from "./resolve-path";
export * from "./ser-utils";
export * from "./serde-json";
Expand Down
11 changes: 11 additions & 0 deletions packages/smithy-client/src/quote-header.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { quoteHeader } from "./quote-header";

describe(quoteHeader.name, () => {
it("should wrap header elements that include the delimiter", () => {
expect(quoteHeader("b,c")).toBe('"b,c"');
});

it("should not wrap header elements that don't include the delimiter", () => {
expect(quoteHeader("bc")).toBe("bc");
});
});
8 changes: 8 additions & 0 deletions packages/smithy-client/src/quote-header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @public
* @param part - header list element
* @returns quoted string if part contains delimiter.
*/
export function quoteHeader(part: string) {
return part.includes(",") ? `"${part}"` : part;
}
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private void writeHttpResponseAssertions(HttpMalformedResponseDefinition respons
responseDefinition.getHeaders().forEach((header, value) -> {
header = header.toLowerCase();
writer.write("expect(r.headers[$S]).toBeDefined();", header);
writer.write("expect(r.headers[$S]).toBe($S);", header, value);
writer.write("expect(r.headers[$S]).toBe($S);", header, value.replaceAll("\\\\", "\\"));
});
writer.write("");
responseDefinition.getBody().ifPresent(body -> {
Expand Down Expand Up @@ -545,9 +545,11 @@ private void writeHttpHostAssertion(HttpRequestTestCase testCase) {
}

private void writeHttpBodyAssertions(String body, String mediaType, boolean isClientTest) {
// If we expect an empty body, expect it to be falsy.
if (body.isEmpty()) {
writer.write("expect(r.body).toBeFalsy();");
// If we expect an empty body, expect it to be falsy.
// Or, for JSON an empty object represents an empty body.
// mediaType is often UNKNOWN here.
writer.write("expect(!r.body || r.body === `{}`).toBeTruthy();");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,11 @@ private String getCollectionInputParam(

switch (bindingType) {
case HEADER:
if (collectionTarget.isStringShape()) {
context.getWriter().addImport(
"quoteHeader", "__quoteHeader", TypeScriptDependency.AWS_SMITHY_CLIENT);
return iteratedParam + ".map(__quoteHeader).join(', ')";
}
return iteratedParam + ".join(', ')";
case QUERY:
case QUERY_PARAMS:
Expand Down Expand Up @@ -2466,16 +2471,29 @@ private HttpBinding readPayload(
// If payload is a Union, then we need to parse the string into JavaScript object.
importUnionDeserializer(writer);
writer.write("const data: Record<string, any> | undefined "
+ "= __expectUnion(await parseBody(output.body, context));");
+ "= await parseBody(output.body, context);");
} else if (target instanceof StringShape || target instanceof DocumentShape) {
// If payload is String or Document, we need to collect body and convert binary to string.
writer.write("const data: any = await collectBodyString(output.body, context);");
} else {
throw new CodegenException(String.format("Unexpected shape type bound to payload: `%s`",
target.getType()));
}
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,

if (target instanceof UnionShape) {
writer.openBlock(
"if (Object.keys(data ?? {}).length) {",
"}",
() -> {
writer.write("contents.$L = __expectUnion($L);", binding.getMemberName(), getOutputValue(context,
Location.PAYLOAD, "data", binding.getMember(), target));
}
);
} else {
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
Location.PAYLOAD, "data", binding.getMember(), target));
}

return binding;
}

Expand Down

0 comments on commit e2e28cf

Please sign in to comment.