Skip to content

Commit 67eac71

Browse files
committed
Use document ser for empty documents
1 parent 89ea82a commit 67eac71

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

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

+24-16
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private void generateOperationSerializer(
218218
writeHeaders(context, operation, bindingIndex);
219219
writeResolvedPath(context, operation, bindingIndex, trait);
220220
boolean hasQueryComponents = writeRequestQueryString(context, operation, bindingIndex, trait);
221-
List<HttpBinding> documentBindings = writeRequestBody(context, operation, bindingIndex);
221+
List<HttpBinding> bodyBindings = writeRequestBody(context, operation, bindingIndex);
222222
boolean hasHostPrefix = operation.hasTrait(EndpointTrait.class);
223223

224224
if (hasHostPrefix) {
@@ -237,14 +237,15 @@ private void generateOperationSerializer(
237237
if (hasQueryComponents) {
238238
writer.write("query: query,");
239239
}
240-
if (!documentBindings.isEmpty()) {
241-
// Track all shapes bound to the document so their serializers may be generated.
242-
documentBindings.stream()
240+
if (!bodyBindings.isEmpty()) {
241+
// Track all shapes bound to the body so their serializers may be generated.
242+
bodyBindings.stream()
243243
.map(HttpBinding::getMember)
244244
.map(member -> context.getModel().expectShape(member.getTarget()))
245245
.forEach(serializingDocumentShapes::add);
246-
writer.write("body: body,");
247246
}
247+
// Always set the body,
248+
writer.write("body: body,");
248249
});
249250
});
250251

@@ -388,25 +389,29 @@ private List<HttpBinding> writeRequestBody(
388389
HttpBindingIndex bindingIndex
389390
) {
390391
TypeScriptWriter writer = context.getWriter();
391-
List<HttpBinding> documentBindings = bindingIndex.getRequestBindings(operation, Location.DOCUMENT);
392-
documentBindings.sort(Comparator.comparing(HttpBinding::getMemberName));
393-
List<HttpBinding> payloadBindings = bindingIndex.getRequestBindings(operation, Location.PAYLOAD);
392+
// Write the default `body` property.
393+
writer.write("let body: any;");
394394

395-
if (!documentBindings.isEmpty()) {
396-
// Write the default `body` property.
397-
writer.write("let body: any;");
398-
serializeInputDocument(context, operation, documentBindings);
399-
return documentBindings;
400-
}
395+
// Handle a payload binding explicitly.
396+
List<HttpBinding> payloadBindings = bindingIndex.getRequestBindings(operation, Location.PAYLOAD);
401397
if (!payloadBindings.isEmpty()) {
402-
// Write the default `body` property.
403-
writer.write("let body: any;");
404398
// There can only be one payload binding.
405399
HttpBinding payloadBinding = payloadBindings.get(0);
406400
serializeInputPayload(context, operation, payloadBinding);
407401
return payloadBindings;
408402
}
409403

404+
// If we have document bindings or need a defaulted request body,
405+
// use the input document serialization.
406+
List<HttpBinding> documentBindings = bindingIndex.getRequestBindings(operation, Location.DOCUMENT);
407+
if (!documentBindings.isEmpty() || bindingIndex.getRequestBindings(operation).isEmpty()) {
408+
documentBindings.sort(Comparator.comparing(HttpBinding::getMemberName));
409+
410+
serializeInputDocument(context, operation, documentBindings);
411+
return documentBindings;
412+
}
413+
414+
// Otherwise, we have no bindings to add shapes from.
410415
return ListUtils.of();
411416
}
412417

@@ -606,6 +611,9 @@ protected void writeDefaultHeaders(GenerationContext context, OperationShape ope
606611
* {@code body} variable that will be serialized as the request body.
607612
* This variable will already be defined in scope.
608613
*
614+
* Implementations MUST properly fill the body parameter even if no
615+
* document bindings are present.
616+
*
609617
* <p>For example:
610618
*
611619
* <pre>{@code

0 commit comments

Comments
 (0)