Skip to content

Commit 0a21bf8

Browse files
authored
Fix streaming type types when they are required (#140)
* fix streaming member typing for required members * use command input/output shape for public typing instead of shapes in protocol
1 parent 3a73fba commit 0a21bf8

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

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
@@ -211,7 +211,8 @@ private void writeStreamingInputType(String typeName, StructureShape inputShape,
211211
Symbol inputSymbol = symbolProvider.toSymbol(inputShape);
212212
writer.openBlock("export type $L = Omit<$T, $S> & {", "};", typeName, inputSymbol,
213213
streamingMember.getMemberName(), () -> {
214-
writer.write("$1L?: $2T[$1S]|string|Uint8Array|Buffer;", streamingMember.getMemberName(), inputSymbol);
214+
writer.write("$1L$2L: $3T[$1S]|string|Uint8Array|Buffer;", streamingMember.getMemberName(),
215+
streamingMember.isRequired() ? "" : "?", inputSymbol);
215216
});
216217
}
217218

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@
2626
import software.amazon.smithy.codegen.core.SymbolProvider;
2727
import software.amazon.smithy.codegen.core.SymbolReference;
2828
import software.amazon.smithy.model.Model;
29-
import software.amazon.smithy.model.knowledge.OperationIndex;
3029
import software.amazon.smithy.model.knowledge.TopDownIndex;
3130
import software.amazon.smithy.model.shapes.OperationShape;
3231
import software.amazon.smithy.model.shapes.ServiceShape;
33-
import software.amazon.smithy.model.shapes.StructureShape;
3432
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
3533
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
3634
import software.amazon.smithy.utils.OptionalUtils;
@@ -90,33 +88,23 @@ static String getResolvedConfigTypeName(Symbol symbol) {
9088

9189
@Override
9290
public void run() {
93-
OperationIndex operationIndex = model.getKnowledge(OperationIndex.class);
9491
writer.addImport("Client", "__Client", "@aws-sdk/smithy-client");
9592
writer.addImport("ClientDefaultValues", "__ClientDefaultValues", "./runtimeConfig");
9693

9794
// Normalize the input and output types of the command to account for
9895
// things like an operation adding input where there once wasn't any
9996
// input, adding output, naming differences between services, etc.
100-
writeInputOutputTypeUnion("ServiceInputTypes", writer, operationIndex::getInput,
101-
writer -> {
102-
// Use an empty object if an operation doesn't define input.
103-
writer.write("| {}");
104-
},
105-
// Input types don't need modification.
106-
Function.identity());
107-
writeInputOutputTypeUnion("ServiceOutputTypes", writer, operationIndex::getOutput,
108-
writer -> {
109-
// Use a MetadataBearer if an operation doesn't define output.
110-
writer.addImport("MetadataBearer", "__MetadataBearer",
111-
TypeScriptDependency.AWS_SDK_TYPES.packageName);
112-
writer.write("| __MetadataBearer");
113-
},
114-
// Command output shape types should be MetadataBearers in the output union.
115-
type -> {
116-
writer.addImport("MetadataBearer", "__MetadataBearer",
117-
TypeScriptDependency.AWS_SDK_TYPES.packageName);
118-
return type + " & __MetadataBearer";
119-
});
97+
writeInputOutputTypeUnion("ServiceInputTypes", writer,
98+
operationSymbol -> operationSymbol.getProperty("inputType", Symbol.class), writer -> {
99+
// Use an empty object if an operation doesn't define input.
100+
writer.write("| {}");
101+
});
102+
writeInputOutputTypeUnion("ServiceOutputTypes", writer,
103+
operationSymbol -> operationSymbol.getProperty("outputType", Symbol.class), writer -> {
104+
// Use a MetadataBearer if an operation doesn't define output.
105+
writer.addImport("MetadataBearer", "__MetadataBearer", TypeScriptDependency.AWS_SDK_TYPES.packageName);
106+
writer.write("| __MetadataBearer");
107+
});
120108

121109
generateConfig();
122110
writer.write("");
@@ -126,15 +114,15 @@ public void run() {
126114
private void writeInputOutputTypeUnion(
127115
String typeName,
128116
TypeScriptWriter writer,
129-
Function<OperationShape, Optional<StructureShape>> mapper,
130-
Consumer<TypeScriptWriter> defaultTypeGenerator,
131-
Function<String, String> typeModifier
117+
Function<Symbol, Optional<Symbol>> mapper,
118+
Consumer<TypeScriptWriter> defaultTypeGenerator
132119
) {
133120
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
134121
Set<OperationShape> containedOperations = topDownIndex.getContainedOperations(service);
122+
135123
List<Symbol> symbols = containedOperations.stream()
136-
.flatMap(operation -> OptionalUtils.stream(mapper.apply(operation)))
137124
.map(symbolProvider::toSymbol)
125+
.flatMap(operation -> OptionalUtils.stream(mapper.apply(operation)))
138126
.sorted(Comparator.comparing(Symbol::getName))
139127
.collect(Collectors.toList());
140128

@@ -145,8 +133,7 @@ private void writeInputOutputTypeUnion(
145133
defaultTypeGenerator.accept(writer);
146134
}
147135
for (int i = 0; i < symbols.size(); i++) {
148-
String lineEnding = (i == symbols.size() - 1) ? ";" : "";
149-
writer.write("| " + typeModifier.apply("$T") + "$L", symbols.get(i), lineEnding);
136+
writer.write("| $T$L", symbols.get(i), i == symbols.size() - 1 ? ";" : "");
150137
}
151138
writer.dedent();
152139
writer.write("");

0 commit comments

Comments
 (0)