Skip to content

Commit

Permalink
Fix operation index file codegen (#1025)
Browse files Browse the repository at this point in the history
- For services with no operations, export an empty module and use
  default service input/output types
- Move operation index writing outside of operation loop
  • Loading branch information
Steven Yuan authored Oct 11, 2023
1 parent 49f75b4 commit 3a5afa4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,12 @@ static void writeIndex(

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
if (containedOperations.isEmpty()) {
writer.write("export {};");
} else {
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}
}

fileManifest.writeFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,24 @@ private void generateCommands(GenerateServiceDirective<TypeScriptCodegenContext,
ProtocolGenerator protocolGenerator = directive.context().protocolGenerator();
ApplicationProtocol applicationProtocol = directive.context().applicationProtocol();

// Write operation index files
if (settings.generateClient()) {
CommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
}
if (settings.generateServerSdk()) {
ServerCommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
}

// Generate each operation for the service.
for (OperationShape operation : directive.operations()) {
// Right now this only generates stubs
if (settings.generateClient()) {
CommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
delegator.useShapeWriter(operation, commandWriter -> new CommandGenerator(
settings, model, operation, symbolProvider, commandWriter,
runtimePlugins, protocolGenerator, applicationProtocol).run());
}

if (settings.generateServerSdk()) {
ServerCommandGenerator.writeIndex(model, service, symbolProvider, fileManifest);
delegator.useShapeWriter(operation, commandWriter -> new ServerCommandGenerator(
settings, model, operation, symbolProvider, commandWriter,
protocolGenerator, applicationProtocol).run());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,12 @@ static void writeIndex(

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
if (containedOperations.isEmpty()) {
writer.write("export {};");
} else {
for (OperationShape operation : containedOperations) {
writer.write("export * from \"./$L\";", symbolProvider.toSymbol(operation).getName());
}
}

fileManifest.writeFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private void writeInputOutputTypeUnion(
writer.write("export type $L = ", typeName);
writer.indent();
// If we have less symbols than operations, at least one doesn't have a type, so add the default.
if (containedOperations.size() != symbols.size()) {
if (containedOperations.size() != symbols.size() || containedOperations.isEmpty()) {
defaultTypeGenerator.accept(writer);
}
for (int i = 0; i < symbols.size(); i++) {
Expand Down

0 comments on commit 3a5afa4

Please sign in to comment.