Skip to content

Commit b9d2aad

Browse files
committed
Add defaults to ServiceIOTypes
1 parent 9fd0e61 commit b9d2aad

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Comparator;
1919
import java.util.List;
2020
import java.util.Optional;
21+
import java.util.Set;
22+
import java.util.function.Consumer;
2123
import java.util.function.Function;
2224
import java.util.stream.Collectors;
2325
import software.amazon.smithy.codegen.core.Symbol;
@@ -95,8 +97,15 @@ public void run() {
9597
// Normalize the input and output types of the command to account for
9698
// things like an operation adding input where there once wasn't any
9799
// input, adding output, naming differences between services, etc.
98-
writeInputOutputTypeUnion("ServiceInputTypes", writer, operationIndex::getInput);
99-
writeInputOutputTypeUnion("ServiceOutputTypes", writer, operationIndex::getOutput);
100+
writeInputOutputTypeUnion("ServiceInputTypes", writer, operationIndex::getInput, writer -> {
101+
// Use an empty object if an operation doesn't define input.
102+
writer.write("| {}");
103+
});
104+
writeInputOutputTypeUnion("ServiceOutputTypes", writer, operationIndex::getOutput, writer -> {
105+
// Use a MetadataBearer if an operation doesn't define output.
106+
writer.addImport("MetadataBearer", "__MetadataBearer", TypeScriptDependency.AWS_SDK_TYPES.packageName);
107+
writer.write("| __MetadataBearer");
108+
});
100109

101110
generateConfig();
102111
writer.write("");
@@ -106,17 +115,23 @@ public void run() {
106115
private void writeInputOutputTypeUnion(
107116
String typeName,
108117
TypeScriptWriter writer,
109-
Function<OperationShape, Optional<StructureShape>> mapper
118+
Function<OperationShape, Optional<StructureShape>> mapper,
119+
Consumer<TypeScriptWriter> defaultTypeGenerator
110120
) {
111121
TopDownIndex topDownIndex = model.getKnowledge(TopDownIndex.class);
112-
List<Symbol> symbols = topDownIndex.getContainedOperations(service).stream()
122+
Set<OperationShape> containedOperations = topDownIndex.getContainedOperations(service);
123+
List<Symbol> symbols = containedOperations.stream()
113124
.flatMap(operation -> OptionalUtils.stream(mapper.apply(operation)))
114125
.map(symbolProvider::toSymbol)
115126
.sorted(Comparator.comparing(Symbol::getName))
116127
.collect(Collectors.toList());
117128

118129
writer.write("export type $L = ", typeName);
119130
writer.indent();
131+
// If we have less symbols than operations, at least one doesn't have a type, so add the default.
132+
if (containedOperations.size() != symbols.size()) {
133+
defaultTypeGenerator.accept(writer);
134+
}
120135
for (int i = 0; i < symbols.size(); i++) {
121136
writer.write("| $T$L", symbols.get(i), i == symbols.size() - 1 ? ";" : "");
122137
}

0 commit comments

Comments
 (0)