[feat] [protobuf] Improve protobuf generator by adding custom options for api and model files (#21075)

* add custom options

* fixing docs

* typo
This commit is contained in:
Andrew Wilson 2025-04-17 06:11:34 +01:00 committed by GitHub
parent b4378a6277
commit 5e446b4147
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 56 additions and 0 deletions

View File

@ -10,6 +10,14 @@ additionalProperties:
wrapComplexType: false
supportMultipleResponses: false
aggregateModelsName: data
customOptionsApi: |
option java_multiple_files = true;
option java_package = "com.example.tutorial.protos.api";
option java_outer_classname = "ExampleProtos";
customOptionsModel: |
option java_multiple_files = false;
option java_package = "com.example.tutorial.protos.model";
option java_outer_classname = "ExampleProtos";
useSimplifiedEnumNames: true
typeMappings:
object: "google.protobuf.Struct"

View File

@ -20,6 +20,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ------ | ----------- | ------ | ------- |
|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|
|customOptionsApi|Custom options for the api files.| |null|
|customOptionsModel|Custom options for the model files.| |null|
|numberedFieldNumberList|Field numbers in order.| |false|
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
|supportMultipleResponses|Support multiple responses| |true|

View File

@ -70,6 +70,10 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
public static final String CUSTOM_OPTIONS_API = "customOptionsApi";
public static final String CUSTOM_OPTIONS_MODEL = "customOptionsModel";
public static final String SUPPORT_MULTIPLE_RESPONSES = "supportMultipleResponses";
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
@ -78,6 +82,12 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
@Setter protected String aggregateModelsName = null;
@SuppressWarnings("unused")
@Setter protected String customOptionsApi = null;
@SuppressWarnings("unused")
@Setter protected String customOptionsModel = null;
private boolean numberedFieldNumberList = false;
private boolean startEnumsWithUnspecified = false;
@ -203,6 +213,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
addSwitch(USE_SIMPLIFIED_ENUM_NAMES, "Use a simple name for enums", useSimplifiedEnumNames);
addSwitch(SUPPORT_MULTIPLE_RESPONSES, "Support multiple responses", supportMultipleResponses);
addOption(AGGREGATE_MODELS_NAME, "Aggregated model filename. If set, all generated models will be combined into this single file.", null);
addOption(CUSTOM_OPTIONS_API, "Custom options for the api files.", null);
addOption(CUSTOM_OPTIONS_MODEL, "Custom options for the model files.", null);
}
@Override
@ -253,6 +265,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
}
if (additionalProperties.containsKey(CUSTOM_OPTIONS_API)) {
this.setCustomOptionsApi((String) additionalProperties.get(CUSTOM_OPTIONS_API));
}
if (additionalProperties.containsKey(CUSTOM_OPTIONS_MODEL)) {
this.setCustomOptionsModel((String) additionalProperties.get(CUSTOM_OPTIONS_MODEL));
}
if (additionalProperties.containsKey(this.SUPPORT_MULTIPLE_RESPONSES)) {
this.supportMultipleResponses = convertPropertyToBooleanAndWriteBack(SUPPORT_MULTIPLE_RESPONSES);
} else {

View File

@ -3,6 +3,9 @@ syntax = "proto3";
package {{#lambda.lowercase}}{{{packageName}}}.{{{apiPackage}}}.{{{classname}}};{{/lambda.lowercase}}
{{#customOptionsApi}}
{{{.}}}
{{/customOptionsApi}}
import "google/protobuf/empty.proto";
{{#imports}}
{{#import}}

View File

@ -3,6 +3,9 @@ syntax = "proto3";
package {{#lambda.lowercase}}{{{packageName}}};{{/lambda.lowercase}}
{{#customOptionsModel}}
{{{.}}}
{{/customOptionsModel}}
{{#imports}}
{{#import}}
import public "{{{.}}}.proto";

View File

@ -12,6 +12,10 @@ syntax = "proto3";
package petstore;
option java_multiple_files = false;
option java_package = "com.example.tutorial.protos.model";
option java_outer_classname = "ExampleProtos";
import public "google/protobuf/struct.proto";
message ApiResponse {

View File

@ -12,6 +12,10 @@ syntax = "proto3";
package petstore.services.defaultservice;
option java_multiple_files = true;
option java_package = "com.example.tutorial.protos.api";
option java_outer_classname = "ExampleProtos";
import "google/protobuf/empty.proto";
import public "models/data.proto";

View File

@ -12,6 +12,10 @@ syntax = "proto3";
package petstore.services.petservice;
option java_multiple_files = true;
option java_package = "com.example.tutorial.protos.api";
option java_outer_classname = "ExampleProtos";
import "google/protobuf/empty.proto";
import public "models/data.proto";

View File

@ -12,6 +12,10 @@ syntax = "proto3";
package petstore.services.storeservice;
option java_multiple_files = true;
option java_package = "com.example.tutorial.protos.api";
option java_outer_classname = "ExampleProtos";
import "google/protobuf/empty.proto";
import public "models/data.proto";

View File

@ -12,6 +12,10 @@ syntax = "proto3";
package petstore.services.userservice;
option java_multiple_files = true;
option java_package = "com.example.tutorial.protos.api";
option java_outer_classname = "ExampleProtos";
import "google/protobuf/empty.proto";
import public "models/data.proto";