Skip to content

Commit f43b0a2

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 c72c566 commit f43b0a2

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
@@ -87,8 +87,11 @@ public void generateSharedComponents(GenerationContext context) {
8787
@Override
8888
protected void writeDefaultHeaders(GenerationContext context, Shape operationOrError, boolean isInput) {
8989
super.writeDefaultHeaders(context, operationOrError, isInput);
90-
if (isInput && operationOrError.isOperationShape()) {
91-
AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operationOrError.asOperationShape().get());
90+
if (operationOrError.isOperationShape()) {
91+
OperationShape operation = operationOrError.asOperationShape().get();
92+
if (isInput) {
93+
AwsProtocolUtils.generateUnsignedPayloadSigV4Header(context, operation);
94+
}
9295
} else if (operationOrError.isStructureShape()) {
9396
context.getWriter().write("'x-amzn-errortype': $S,", operationOrError.getId().getName());
9497
}
@@ -104,7 +107,11 @@ public void serializeDocumentBody(
104107
// Short circuit when we have no bindings.
105108
TypeScriptWriter writer = context.getWriter();
106109
if (documentBindings.isEmpty()) {
107-
writer.write("body = \"\";");
110+
if (isInput) {
111+
writer.write("body = \"\";");
112+
} else {
113+
writer.write("body = \"{}\";");
114+
}
108115
return;
109116
}
110117

@@ -170,6 +177,19 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
170177
return new JsonMemberSerVisitor(context, dataSource, getDocumentTimestampFormat());
171178
}
172179

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

0 commit comments

Comments
 (0)