Skip to content

Commit b4b4153

Browse files
committed
Protocol codegen fixes and cleanup
This commit includes various fixes for setting response members, importing types, and documentation updates.
1 parent 8544716 commit b4b4153

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ protected final void generateDeserFunction(
306306
writer.openBlock("const $L = (\n"
307307
+ " output: any,\n"
308308
+ " context: SerdeContext\n"
309-
+ "): $L => {", "}", methodName, symbol.getName(), () -> functionBody.accept(context, shape));
309+
+ "): $T => {", "}", methodName, symbol, () -> functionBody.accept(context, shape));
310310
writer.write("");
311311
}
312312

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ private void generateSerFunction(
299299

300300
writer.addImport(symbol, symbol.getName());
301301
writer.openBlock("const $L = (\n"
302-
+ " input: $L,\n"
302+
+ " input: $T,\n"
303303
+ " context: SerdeContext\n"
304-
+ "): any => {", "}", methodName, symbol.getName(), () -> functionBody.accept(context, shape));
304+
+ "): any => {", "}", methodName, symbol, () -> functionBody.accept(context, shape));
305305
writer.write("");
306306
}
307307

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Comparator;
2121
import java.util.List;
2222
import java.util.Set;
23+
import java.util.TreeMap;
2324
import java.util.TreeSet;
2425
import java.util.logging.Logger;
2526
import software.amazon.smithy.codegen.core.CodegenException;
@@ -39,6 +40,7 @@
3940
import software.amazon.smithy.model.shapes.Shape;
4041
import software.amazon.smithy.model.shapes.ShapeIndex;
4142
import software.amazon.smithy.model.shapes.StringShape;
43+
import software.amazon.smithy.model.shapes.StructureShape;
4244
import software.amazon.smithy.model.shapes.TimestampShape;
4345
import software.amazon.smithy.model.traits.HttpTrait;
4446
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
@@ -352,7 +354,7 @@ private String getInputValue(
352354
Shape target
353355
) {
354356
if (isNativeSimpleType(target)) {
355-
return dataSource;
357+
return dataSource + ".toString()";
356358
} else if (target instanceof TimestampShape) {
357359
HttpBindingIndex httpIndex = context.getModel().getKnowledge(HttpBindingIndex.class);
358360
Format format = httpIndex.determineTimestampFormat(member, bindingType, getDocumentTimestampFormat());
@@ -373,9 +375,9 @@ private String getInputValue(
373375
* blob. By default, this base64 encodes content in headers and query strings,
374376
* and passes through for payloads.
375377
*
376-
* @param dataSource The in-code location of the data to provide an output of
377-
* ({@code output.foo}, {@code entry}, etc.)
378378
* @param bindingType How this value is bound to the operation input.
379+
* @param dataSource The in-code location of the data to provide an input of
380+
* ({@code input.foo}, {@code entry}, etc.)
379381
* @return Returns a value or expression of the input blob.
380382
*/
381383
private String getBlobInputParam(Location bindingType, String dataSource) {
@@ -397,8 +399,8 @@ private String getBlobInputParam(Location bindingType, String dataSource) {
397399
* relies on the HTTP implementation for query strings.
398400
*
399401
* @param bindingType How this value is bound to the operation input.
400-
* @param dataSource The in-code location of the data to provide an output of
401-
* ({@code output.foo}, {@code entry}, etc.)
402+
* @param dataSource The in-code location of the data to provide an input of
403+
* ({@code input.foo}, {@code entry}, etc.)
402404
* @return Returns a value or expression of the input collection.
403405
*/
404406
private String getCollectionInputParam(
@@ -482,18 +484,19 @@ private void generateOperationDeserializer(
482484
// Only set a type and the members if we have output.
483485
operation.getOutput().ifPresent(outputId -> {
484486
writer.write("__type: $S,", outputId.getName());
485-
List<HttpBinding> documentBindings = bindingIndex.getResponseBindings(operation, Location.DOCUMENT);
486-
// Track all shapes bound to the document so their deserializers may be generated.
487-
documentBindings.forEach(binding -> {
488-
// Set all the members to undefined to meet type constraints.
489-
writer.write("$L: undefined,", binding.getMemberName());
490-
Shape target = shapeIndex.getShape(binding.getMember().getTarget()).get();
491-
documentDeserializingShapes.add(target);
492-
});
487+
// Set all the members to undefined to meet type constraints.
488+
StructureShape target = shapeIndex.getShape(outputId).get().asStructureShape().get();
489+
new TreeMap<>(target.getAllMembers())
490+
.forEach((memberName, memberShape) -> writer.write("$L: undefined,", memberName));
493491
});
494492
});
495493
readHeaders(context, operation, bindingIndex);
496-
readResponseBody(context, operation, bindingIndex);
494+
List<HttpBinding> documentBindings = readResponseBody(context, operation, bindingIndex);
495+
// Track all shapes bound to the document so their deserializers may be generated.
496+
documentBindings.forEach(binding -> {
497+
Shape target = shapeIndex.getShape(binding.getMember().getTarget()).get();
498+
documentDeserializingShapes.add(target);
499+
});
497500
writer.write("return Promise.resolve(contents);");
498501
});
499502
writer.write("");

0 commit comments

Comments
 (0)