Skip to content

Commit dfbbd7b

Browse files
committed
handle unset unions
1 parent 89fedbc commit dfbbd7b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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 && Object.keys(r.body).length).toBeFalsy();");
551553
return;
552554
}
553555

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,16 +2466,29 @@ private HttpBinding readPayload(
24662466
// If payload is a Union, then we need to parse the string into JavaScript object.
24672467
importUnionDeserializer(writer);
24682468
writer.write("const data: Record<string, any> | undefined "
2469-
+ "= __expectUnion(await parseBody(output.body, context));");
2469+
+ "= await parseBody(output.body, context);");
24702470
} else if (target instanceof StringShape || target instanceof DocumentShape) {
24712471
// If payload is String or Document, we need to collect body and convert binary to string.
24722472
writer.write("const data: any = await collectBodyString(output.body, context);");
24732473
} else {
24742474
throw new CodegenException(String.format("Unexpected shape type bound to payload: `%s`",
24752475
target.getType()));
24762476
}
2477-
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
2477+
2478+
if (target instanceof UnionShape) {
2479+
writer.openBlock(
2480+
"if (Object.keys(data ?? {}).length) {",
2481+
"}",
2482+
() -> {
2483+
writer.write("contents.$L = __expectUnion($L);", binding.getMemberName(), getOutputValue(context,
2484+
Location.PAYLOAD, "data", binding.getMember(), target));
2485+
}
2486+
);
2487+
} else {
2488+
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
24782489
Location.PAYLOAD, "data", binding.getMember(), target));
2490+
}
2491+
24792492
return binding;
24802493
}
24812494

0 commit comments

Comments
 (0)