Skip to content

Commit 60f42b9

Browse files
chore: differentiate restJson1 empty body behavior
This adds differentation for empty body behavior in restJson1. For responses this protocol requires always sending an empty json object if there is some shape defined as output but nothing bound to the body or payload.
1 parent f79b076 commit 60f42b9

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/RestJsonProtocolGenerator.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ public void generateSharedComponents(GenerationContext context) {
8989
@Override
9090
protected void writeDefaultHeaders(GenerationContext context, Shape operationOrError, boolean isInput) {
9191
super.writeDefaultHeaders(context, operationOrError, isInput);
92-
if (isInput && operationOrError.isOperationShape()) {
93-
AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operationOrError.asOperationShape().get());
92+
if (operationOrError.isOperationShape()) {
93+
OperationShape operation = operationOrError.asOperationShape().get();
94+
if (isInput) {
95+
AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation);
96+
}
9497
} else if (operationOrError.isStructureShape()) {
9598
context.getWriter().write("'x-amzn-errortype': $S,", operationOrError.getId().getName());
9699
}
@@ -106,7 +109,11 @@ public void serializeDocumentBody(
106109
// Short circuit when we have no bindings.
107110
TypeScriptWriter writer = context.getWriter();
108111
if (documentBindings.isEmpty()) {
109-
writer.write("body = \"\";");
112+
if (isInput) {
113+
writer.write("body = \"\";");
114+
} else {
115+
writer.write("body = \"{}\";");
116+
}
110117
return;
111118
}
112119

@@ -172,6 +179,19 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
172179
return new JsonMemberSerVisitor(context, dataSource, getDocumentTimestampFormat());
173180
}
174181

182+
@Override
183+
protected boolean shouldWriteDefaultBody(GenerationContext context, Shape operationOrError, boolean isInput) {
184+
if (!isInput) {
185+
// Operations that have any defined output shape should always send a default body.
186+
if (operationOrError.isOperationShape()) {
187+
OperationShape operation = operationOrError.asOperationShape().get();
188+
return operation.getOutput().isPresent();
189+
}
190+
return true;
191+
}
192+
return super.shouldWriteDefaultBody(context, operationOrError, isInput);
193+
}
194+
175195
@Override
176196
protected void writeErrorCodeParser(GenerationContext context) {
177197
TypeScriptWriter writer = context.getWriter();

0 commit comments

Comments
 (0)