Skip to content

Commit b78200b

Browse files
committed
add SDKStreamSerdeContext interface
1 parent e88ac0e commit b78200b

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import software.amazon.smithy.model.shapes.Shape;
2929
import software.amazon.smithy.model.shapes.StructureShape;
3030
import software.amazon.smithy.model.traits.StreamingTrait;
31+
import software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency;
3132
import software.amazon.smithy.utils.SmithyUnstableApi;
3233

3334
/**
@@ -75,12 +76,14 @@ public static String getOperationSerializerContextType(
7576

7677
/**
7778
* Get context type for command deserializer function.
79+
* @param settings The TypeScript settings
7880
* @param writer The code writer.
7981
* @param model The model for the service containing the given command.
8082
* @param operation The operation shape for given command.
8183
* @return The TypeScript type for the deserializer context
8284
*/
8385
public static String getOperationDeserializerContextType(
86+
TypeScriptSettings settings,
8487
TypeScriptWriter writer,
8588
Model model,
8689
OperationShape operation
@@ -90,9 +93,15 @@ public static String getOperationDeserializerContextType(
9093
// If event stream trait exists, add corresponding serde context type to the intersection type.
9194
EventStreamIndex eventStreamIndex = EventStreamIndex.of(model);
9295
if (eventStreamIndex.getOutputInfo(operation).isPresent()) {
93-
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext", "@aws-sdk/types");
96+
writer.addImport("EventStreamSerdeContext", "__EventStreamSerdeContext",
97+
TypeScriptDependency.AWS_SDK_TYPES.packageName);
9498
contextInterfaceList.add("__EventStreamSerdeContext");
9599
}
100+
if (AddSdkStreamMixinDependency.hasStreamingBlobDeser(settings, model, operation)) {
101+
writer.addImport("SdkStreamSerdeContext", "__SdkStreamSerdeContext",
102+
TypeScriptDependency.AWS_SDK_TYPES.packageName);
103+
contextInterfaceList.add("__SdkStreamSerdeContext");
104+
}
96105
return String.join(" & ", contextInterfaceList);
97106
}
98107

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ private void writeSerde() {
303303
.write("private deserialize(")
304304
.indent()
305305
.write("output: $T,", applicationProtocol.getResponseType())
306-
.write("context: $L", CodegenUtils.getOperationDeserializerContextType(writer, model, operation))
306+
.write("context: $L",
307+
CodegenUtils.getOperationDeserializerContextType(settings, writer, model, operation))
307308
.dedent()
308309
.openBlock("): Promise<$T> {", "}", outputType, () -> writeSerdeDispatcher(false))
309310
.write("");
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* runtime-specific stream implementations.
4242
*/
4343
@SmithyInternalApi
44-
public final class SdkStreamUtilsMixin implements TypeScriptIntegration {
44+
public final class AddSdkStreamMixinDependency implements TypeScriptIntegration {
4545

4646
@Override
4747
public void addConfigInterfaceFields(
@@ -50,7 +50,7 @@ public void addConfigInterfaceFields(
5050
SymbolProvider symbolProvider,
5151
TypeScriptWriter writer
5252
) {
53-
if (!hasStreamingBlobResponse(model, settings.getService(model))) {
53+
if (!hasStreamingBlobDeser(settings, model)) {
5454
return;
5555
}
5656

@@ -68,7 +68,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
6868
SymbolProvider symbolProvider,
6969
LanguageTarget target
7070
) {
71-
if (!hasStreamingBlobResponse(model, settings.getService(model))) {
71+
if (!hasStreamingBlobDeser(settings, model)) {
7272
return Collections.emptyMap();
7373
}
7474
switch (target) {
@@ -91,16 +91,29 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
9191
}
9292
}
9393

94-
private static boolean hasStreamingBlobResponse(Model model, ServiceShape serviceShape) {
94+
private static boolean hasStreamingBlobDeser(TypeScriptSettings settings, Model model) {
95+
ServiceShape serviceShape = settings.getService(model);
9596
TopDownIndex topDownIndex = TopDownIndex.of(model);
9697
Set<OperationShape> operations = topDownIndex.getContainedOperations(serviceShape);
9798
for (OperationShape operation : operations) {
98-
StructureShape outputShape = model.expectShape(operation.getOutputShape()).asStructureShape().get();
99-
for (MemberShape member : outputShape.members()) {
100-
Shape shape = model.expectShape(member.getTarget());
101-
if (shape instanceof BlobShape && shape.hasTrait(StreamingTrait.class)) {
102-
return true;
103-
}
99+
if (hasStreamingBlobDeser(settings, model, operation)) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
106+
public static boolean hasStreamingBlobDeser(TypeScriptSettings settings, Model model, OperationShape operation) {
107+
StructureShape ioShapeToDeser;
108+
if (settings.generateServerSdk()) {
109+
ioShapeToDeser = model.expectShape(operation.getInputShape()).asStructureShape().get();
110+
} else {
111+
ioShapeToDeser = model.expectShape(operation.getOutputShape()).asStructureShape().get();
112+
}
113+
for (MemberShape member : ioShapeToDeser.members()) {
114+
Shape shape = model.expectShape(member.getTarget());
115+
if (shape instanceof BlobShape && shape.hasTrait(StreamingTrait.class)) {
116+
return true;
104117
}
105118
}
106119
return false;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,8 @@ private void generateOperationResponseDeserializer(
19991999
String errorMethodName = methodName + "Error";
20002000
// Add the normalized output type.
20012001
Symbol outputType = symbol.expectProperty("outputType", Symbol.class);
2002-
String contextType = CodegenUtils.getOperationDeserializerContextType(writer, context.getModel(), operation);
2002+
String contextType = CodegenUtils.getOperationDeserializerContextType(context.getSettings(), writer,
2003+
context.getModel(), operation);
20032004

20042005
// Handle the general response.
20052006
writer.openBlock("export const $L = async(\n"

smithy-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ software.amazon.smithy.typescript.codegen.integration.AddChecksumRequiredDepende
33
software.amazon.smithy.typescript.codegen.integration.AddDefaultsModeDependency
44
software.amazon.smithy.typescript.codegen.integration.AddHttpApiKeyAuthPlugin
55
software.amazon.smithy.typescript.codegen.integration.AddBaseServiceExceptionClass
6-
software.amazon.smithy.typescript.codegen.integration.SdkStreamUtilsMixin
6+
software.amazon.smithy.typescript.codegen.integration.AddSdkStreamMixinDependency

0 commit comments

Comments
 (0)