Skip to content

Commit 5e446b4

Browse files
author
Andrew Wilson
authored
[feat] [protobuf] Improve protobuf generator by adding custom options for api and model files (#21075)
* add custom options * fixing docs * typo
1 parent b4378a6 commit 5e446b4

10 files changed

Lines changed: 56 additions & 0 deletions

File tree

bin/configs/protobuf-schema-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ additionalProperties:
1010
wrapComplexType: false
1111
supportMultipleResponses: false
1212
aggregateModelsName: data
13+
customOptionsApi: |
14+
option java_multiple_files = true;
15+
option java_package = "com.example.tutorial.protos.api";
16+
option java_outer_classname = "ExampleProtos";
17+
customOptionsModel: |
18+
option java_multiple_files = false;
19+
option java_package = "com.example.tutorial.protos.model";
20+
option java_outer_classname = "ExampleProtos";
1321
useSimplifiedEnumNames: true
1422
typeMappings:
1523
object: "google.protobuf.Struct"

docs/generators/protobuf-schema.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2020
| ------ | ----------- | ------ | ------- |
2121
|addJsonNameAnnotation|Append "json_name" annotation to message field when the specification name differs from the protobuf field name| |false|
2222
|aggregateModelsName|Aggregated model filename. If set, all generated models will be combined into this single file.| |null|
23+
|customOptionsApi|Custom options for the api files.| |null|
24+
|customOptionsModel|Custom options for the model files.| |null|
2325
|numberedFieldNumberList|Field numbers in order.| |false|
2426
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
2527
|supportMultipleResponses|Support multiple responses| |true|

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
7070

7171
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
7272

73+
public static final String CUSTOM_OPTIONS_API = "customOptionsApi";
74+
75+
public static final String CUSTOM_OPTIONS_MODEL = "customOptionsModel";
76+
7377
public static final String SUPPORT_MULTIPLE_RESPONSES = "supportMultipleResponses";
7478

7579
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
@@ -78,6 +82,12 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
7882

7983
@Setter protected String aggregateModelsName = null;
8084

85+
@SuppressWarnings("unused")
86+
@Setter protected String customOptionsApi = null;
87+
88+
@SuppressWarnings("unused")
89+
@Setter protected String customOptionsModel = null;
90+
8191
private boolean numberedFieldNumberList = false;
8292

8393
private boolean startEnumsWithUnspecified = false;
@@ -203,6 +213,8 @@ public ProtobufSchemaCodegen() {
203213
addSwitch(USE_SIMPLIFIED_ENUM_NAMES, "Use a simple name for enums", useSimplifiedEnumNames);
204214
addSwitch(SUPPORT_MULTIPLE_RESPONSES, "Support multiple responses", supportMultipleResponses);
205215
addOption(AGGREGATE_MODELS_NAME, "Aggregated model filename. If set, all generated models will be combined into this single file.", null);
216+
addOption(CUSTOM_OPTIONS_API, "Custom options for the api files.", null);
217+
addOption(CUSTOM_OPTIONS_MODEL, "Custom options for the model files.", null);
206218
}
207219

208220
@Override
@@ -253,6 +265,14 @@ public void processOpts() {
253265
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
254266
}
255267

268+
if (additionalProperties.containsKey(CUSTOM_OPTIONS_API)) {
269+
this.setCustomOptionsApi((String) additionalProperties.get(CUSTOM_OPTIONS_API));
270+
}
271+
272+
if (additionalProperties.containsKey(CUSTOM_OPTIONS_MODEL)) {
273+
this.setCustomOptionsModel((String) additionalProperties.get(CUSTOM_OPTIONS_MODEL));
274+
}
275+
256276
if (additionalProperties.containsKey(this.SUPPORT_MULTIPLE_RESPONSES)) {
257277
this.supportMultipleResponses = convertPropertyToBooleanAndWriteBack(SUPPORT_MULTIPLE_RESPONSES);
258278
} else {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ syntax = "proto3";
33

44
package {{#lambda.lowercase}}{{{packageName}}}.{{{apiPackage}}}.{{{classname}}};{{/lambda.lowercase}}
55

6+
{{#customOptionsApi}}
7+
{{{.}}}
8+
{{/customOptionsApi}}
69
import "google/protobuf/empty.proto";
710
{{#imports}}
811
{{#import}}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ syntax = "proto3";
33

44
package {{#lambda.lowercase}}{{{packageName}}};{{/lambda.lowercase}}
55

6+
{{#customOptionsModel}}
7+
{{{.}}}
8+
{{/customOptionsModel}}
69
{{#imports}}
710
{{#import}}
811
import public "{{{.}}}.proto";

samples/config/petstore/protobuf-schema-config/models/data.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ syntax = "proto3";
1212

1313
package petstore;
1414

15+
option java_multiple_files = false;
16+
option java_package = "com.example.tutorial.protos.model";
17+
option java_outer_classname = "ExampleProtos";
18+
1519
import public "google/protobuf/struct.proto";
1620

1721
message ApiResponse {

samples/config/petstore/protobuf-schema-config/services/default_service.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ syntax = "proto3";
1212

1313
package petstore.services.defaultservice;
1414

15+
option java_multiple_files = true;
16+
option java_package = "com.example.tutorial.protos.api";
17+
option java_outer_classname = "ExampleProtos";
18+
1519
import "google/protobuf/empty.proto";
1620
import public "models/data.proto";
1721

samples/config/petstore/protobuf-schema-config/services/pet_service.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ syntax = "proto3";
1212

1313
package petstore.services.petservice;
1414

15+
option java_multiple_files = true;
16+
option java_package = "com.example.tutorial.protos.api";
17+
option java_outer_classname = "ExampleProtos";
18+
1519
import "google/protobuf/empty.proto";
1620
import public "models/data.proto";
1721

samples/config/petstore/protobuf-schema-config/services/store_service.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ syntax = "proto3";
1212

1313
package petstore.services.storeservice;
1414

15+
option java_multiple_files = true;
16+
option java_package = "com.example.tutorial.protos.api";
17+
option java_outer_classname = "ExampleProtos";
18+
1519
import "google/protobuf/empty.proto";
1620
import public "models/data.proto";
1721

samples/config/petstore/protobuf-schema-config/services/user_service.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ syntax = "proto3";
1212

1313
package petstore.services.userservice;
1414

15+
option java_multiple_files = true;
16+
option java_package = "com.example.tutorial.protos.api";
17+
option java_outer_classname = "ExampleProtos";
18+
1519
import "google/protobuf/empty.proto";
1620
import public "models/data.proto";
1721

0 commit comments

Comments
 (0)