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 9154108edcd..64285b35c4b 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 @@ -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 && Object.keys(r.body).length).toBeFalsy();"); return; } 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 2df46ef0f47..b511122fced 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 @@ -2466,7 +2466,7 @@ 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 | 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);"); @@ -2474,8 +2474,21 @@ private HttpBinding readPayload( 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; }