Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/protobuf-schema-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ additionalProperties:
numberedFieldNumberList: true
startEnumsWithUnspecified: true
wrapComplexType: false
aggregateModelsName: data
typeMappings:
object: "google.protobuf.Struct"
importMappings:
Expand Down
1 change: 1 addition & 0 deletions docs/generators/protobuf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|addJsonNameAnnotation|Append "json_name" annotation to message field when the specification name differs from the protobuf field name| |false|
|aggregateModelsName|Aggregated model filename. If set, all generated models will be combined into this single file.| |null|
|numberedFieldNumberList|Field numbers in order.| |false|
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
|wrapComplexType|Generate Additional message for complex type| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.google.common.base.CaseFormat;

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

public static final String WRAP_COMPLEX_TYPE = "wrapComplexType";

public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";

private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);

@Setter protected String packageName = "openapitools";

@Setter protected String aggregateModelsName = null;

private boolean numberedFieldNumberList = false;

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

@Override
Expand Down Expand Up @@ -228,6 +235,10 @@ public void processOpts() {
this.wrapComplexType = convertPropertyToBooleanAndWriteBack(WRAP_COMPLEX_TYPE);
}

if (additionalProperties.containsKey(AGGREGATE_MODELS_NAME)) {
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
}

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
}

Expand Down Expand Up @@ -650,7 +661,35 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
.forEach(importFromList -> this.addImport(objs, parentCM, importFromList));
}
}
return objs;
return aggregateModelsName == null ? objs : aggregateModels(objs);
}

/**
* Aggregates all individual model definitions into a single entry.
*
* @param objs the original map of model names to their respective entries
* @return a new {@link Map} containing a single entry keyed by {@code aggregateModelsName} with
* combined models and imports from all provided entries
*/
public Map<String, ModelsMap> aggregateModels(Map<String, ModelsMap> objs) {
Map<String, ModelsMap> objects = new HashMap<>();
ModelsMap aggregateObj = objs.values().stream()
.findFirst()
.orElse(new ModelsMap());

List<ModelMap> models = objs.values().stream()
.flatMap(modelsMap -> modelsMap.getModels().stream())
.collect(Collectors.toList());

Set<Map<String, String>> imports = objs.values().stream()
.flatMap(modelsMap -> modelsMap.getImports().stream())
.filter(importMap -> !importMap.get("import").startsWith("models/"))
.collect(Collectors.toSet());

aggregateObj.setModels(models);
aggregateObj.setImports(new ArrayList<>(imports));
objects.put(this.aggregateModelsName, aggregateObj);
return objects;
}

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

if (this.aggregateModelsName != null) {
List<Map<String, String>> aggregate_imports = Collections.singletonList(Collections
.singletonMap(IMPORT, toModelImport(this.aggregateModelsName)));
objs.setImports(aggregate_imports);
}
return objs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ import public "{{{.}}}.proto";
}
{{/isEnum}}
{{/model}}

{{/models}}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ message Cat {
int32 age = 2;

}

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ message Category {
map<string, StringArray> document = 3;

}

Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ message Dog {
Breed breed = 2;

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ message Pet {
Category category = 2;

}

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ message StringArray {
repeated string string_array = 1;

}

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ message StringMap {
map<string, string> string_map = 1;

}

Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ message Tag {
TagName name = 2;

}

Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ message TagName {
StringMap map = 3;
}
}

Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
README.md
models/api_response.proto
models/cat.proto
models/category.proto
models/dog.proto
models/order.proto
models/other_test.proto
models/pet.proto
models/pets_get_request.proto
models/pets_post_request.proto
models/tag.proto
models/user.proto
models/data.proto
services/default_service.proto
services/pet_service.proto
services/store_service.proto
Expand Down

This file was deleted.

22 changes: 0 additions & 22 deletions samples/config/petstore/protobuf-schema-config/models/cat.proto

This file was deleted.

This file was deleted.

Loading
Loading