18
18
import java .util .Comparator ;
19
19
import java .util .List ;
20
20
import java .util .Optional ;
21
+ import java .util .Set ;
22
+ import java .util .function .Consumer ;
21
23
import java .util .function .Function ;
22
24
import java .util .stream .Collectors ;
23
25
import software .amazon .smithy .codegen .core .Symbol ;
@@ -95,8 +97,15 @@ public void run() {
95
97
// Normalize the input and output types of the command to account for
96
98
// things like an operation adding input where there once wasn't any
97
99
// 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
+ });
100
109
101
110
generateConfig ();
102
111
writer .write ("" );
@@ -106,17 +115,23 @@ public void run() {
106
115
private void writeInputOutputTypeUnion (
107
116
String typeName ,
108
117
TypeScriptWriter writer ,
109
- Function <OperationShape , Optional <StructureShape >> mapper
118
+ Function <OperationShape , Optional <StructureShape >> mapper ,
119
+ Consumer <TypeScriptWriter > defaultTypeGenerator
110
120
) {
111
121
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 ()
113
124
.flatMap (operation -> OptionalUtils .stream (mapper .apply (operation )))
114
125
.map (symbolProvider ::toSymbol )
115
126
.sorted (Comparator .comparing (Symbol ::getName ))
116
127
.collect (Collectors .toList ());
117
128
118
129
writer .write ("export type $L = " , typeName );
119
130
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
+ }
120
135
for (int i = 0 ; i < symbols .size (); i ++) {
121
136
writer .write ("| $T$L" , symbols .get (i ), i == symbols .size () - 1 ? ";" : "" );
122
137
}
0 commit comments