Skip to content

Commit c8cfa16

Browse files
authored
Add options to aggregate protos to one file (#20962)
1 parent 1655275 commit c8cfa16

40 files changed

+231
-322
lines changed

bin/configs/protobuf-schema-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ additionalProperties:
88
numberedFieldNumberList: true
99
startEnumsWithUnspecified: true
1010
wrapComplexType: false
11+
aggregateModelsName: data
1112
typeMappings:
1213
object: "google.protobuf.Struct"
1314
importMappings:

docs/generators/protobuf-schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
1919
| Option | Description | Values | Default |
2020
| ------ | ----------- | ------ | ------- |
2121
|addJsonNameAnnotation|Append "json_name" annotation to message field when the specification name differs from the protobuf field name| |false|
22+
|aggregateModelsName|Aggregated model filename. If set, all generated models will be combined into this single file.| |null|
2223
|numberedFieldNumberList|Field numbers in order.| |false|
2324
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
2425
|wrapComplexType|Generate Additional message for complex type| |true|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import java.util.*;
4343
import java.util.Map.Entry;
4444
import java.util.regex.Pattern;
45+
import java.util.stream.Collectors;
46+
4547
import com.google.common.base.CaseFormat;
4648

4749
import static org.openapitools.codegen.utils.StringUtils.*;
@@ -64,10 +66,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
6466

6567
public static final String WRAP_COMPLEX_TYPE = "wrapComplexType";
6668

69+
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
70+
6771
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
6872

6973
@Setter protected String packageName = "openapitools";
7074

75+
@Setter protected String aggregateModelsName = null;
76+
7177
private boolean numberedFieldNumberList = false;
7278

7379
private boolean startEnumsWithUnspecified = false;
@@ -186,6 +192,7 @@ public ProtobufSchemaCodegen() {
186192
addSwitch(START_ENUMS_WITH_UNSPECIFIED, "Introduces \"UNSPECIFIED\" as the first element of enumerations.", startEnumsWithUnspecified);
187193
addSwitch(ADD_JSON_NAME_ANNOTATION, "Append \"json_name\" annotation to message field when the specification name differs from the protobuf field name", addJsonNameAnnotation);
188194
addSwitch(WRAP_COMPLEX_TYPE, "Generate Additional message for complex type", wrapComplexType);
195+
addOption(AGGREGATE_MODELS_NAME, "Aggregated model filename. If set, all generated models will be combined into this single file.", null);
189196
}
190197

191198
@Override
@@ -228,6 +235,10 @@ public void processOpts() {
228235
this.wrapComplexType = convertPropertyToBooleanAndWriteBack(WRAP_COMPLEX_TYPE);
229236
}
230237

238+
if (additionalProperties.containsKey(AGGREGATE_MODELS_NAME)) {
239+
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
240+
}
241+
231242
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
232243
}
233244

@@ -650,7 +661,35 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
650661
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
651662
}
652663
}
653-
return objs;
664+
return aggregateModelsName == null ? objs : aggregateModels(objs);
665+
}
666+
667+
/**
668+
* Aggregates all individual model definitions into a single entry.
669+
*
670+
* @param objs the original map of model names to their respective entries
671+
* @return a new {@link Map} containing a single entry keyed by {@code aggregateModelsName} with
672+
* combined models and imports from all provided entries
673+
*/
674+
public Map<String, ModelsMap> aggregateModels(Map<String, ModelsMap> objs) {
675+
Map<String, ModelsMap> objects = new HashMap<>();
676+
ModelsMap aggregateObj = objs.values().stream()
677+
.findFirst()
678+
.orElse(new ModelsMap());
679+
680+
List<ModelMap> models = objs.values().stream()
681+
.flatMap(modelsMap -> modelsMap.getModels().stream())
682+
.collect(Collectors.toList());
683+
684+
Set<Map<String, String>> imports = objs.values().stream()
685+
.flatMap(modelsMap -> modelsMap.getImports().stream())
686+
.filter(importMap -> !importMap.get("import").startsWith("models/"))
687+
.collect(Collectors.toSet());
688+
689+
aggregateObj.setModels(models);
690+
aggregateObj.setImports(new ArrayList<>(imports));
691+
objects.put(this.aggregateModelsName, aggregateObj);
692+
return objects;
654693
}
655694

656695
public void addImport(Map<String, ModelsMap> objs, CodegenModel cm, String importValue) {
@@ -907,6 +946,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
907946
}
908947
}
909948

949+
if (this.aggregateModelsName != null) {
950+
List<Map<String, String>> aggregate_imports = Collections.singletonList(Collections
951+
.singletonMap(IMPORT, toModelImport(this.aggregateModelsName)));
952+
objs.setImports(aggregate_imports);
953+
}
910954
return objs;
911955
}
912956

modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ import public "{{{.}}}.proto";
5050
}
5151
{{/isEnum}}
5252
{{/model}}
53+
5354
{{/models}}

samples/config/petstore/protobuf-schema-config-complex/models/cat.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ message Cat {
2020
int32 age = 2;
2121

2222
}
23+

samples/config/petstore/protobuf-schema-config-complex/models/category.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ message Category {
2323
map<string, StringArray> document = 3;
2424

2525
}
26+

samples/config/petstore/protobuf-schema-config-complex/models/dog.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ message Dog {
2828
Breed breed = 2;
2929

3030
}
31+

samples/config/petstore/protobuf-schema-config-complex/models/pet.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ message Pet {
2121
Category category = 2;
2222

2323
}
24+

samples/config/petstore/protobuf-schema-config-complex/models/string_array.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ message StringArray {
1818
repeated string string_array = 1;
1919

2020
}
21+

samples/config/petstore/protobuf-schema-config-complex/models/string_map.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ message StringMap {
1818
map<string, string> string_map = 1;
1919

2020
}
21+

0 commit comments

Comments
 (0)