Skip to content

Commit 24c7cb9

Browse files
author
andrewwilsonnew
committed
add custom options
1 parent 11c5611 commit 24c7cb9

9 files changed

Lines changed: 54 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
@@ -9,6 +9,14 @@ additionalProperties:
99
startEnumsWithUnspecified: true
1010
wrapComplexType: false
1111
aggregateModelsName: data
12+
customOptionsApi: |
13+
option java_multiple_files = true;
14+
option java_package = "com.example.tutorial.protos.api";
15+
option java_outer_classname = "ExampleProtos";
16+
customOptionsModel: |
17+
option java_multiple_files = false;
18+
option java_package = "com.example.tutorial.protos.model";
19+
option java_outer_classname = "ExampleProtos";
1220
typeMappings:
1321
object: "google.protobuf.Struct"
1422
importMappings:

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
@@ -68,12 +68,22 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
6868

6969
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
7070

71+
public static final String CUSTOM_OPTIONS_API = "customOptionsApi";
72+
73+
public static final String CUSTOM_OPTIONS_MODEL = "customOptionsModel";
74+
7175
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
7276

7377
@Setter protected String packageName = "openapitools";
7478

7579
@Setter protected String aggregateModelsName = null;
7680

81+
@SuppressWarnings("unused")
82+
@Setter protected String customOptionsApi = null;
83+
84+
@SuppressWarnings("unused")
85+
@Setter protected String customOptionsModel = null;
86+
7787
private boolean numberedFieldNumberList = false;
7888

7989
private boolean startEnumsWithUnspecified = false;
@@ -193,6 +203,8 @@ public ProtobufSchemaCodegen() {
193203
addSwitch(ADD_JSON_NAME_ANNOTATION, "Append \"json_name\" annotation to message field when the specification name differs from the protobuf field name", addJsonNameAnnotation);
194204
addSwitch(WRAP_COMPLEX_TYPE, "Generate Additional message for complex type", wrapComplexType);
195205
addOption(AGGREGATE_MODELS_NAME, "Aggregated model filename. If set, all generated models will be combined into this single file.", null);
206+
addOption(CUSTOM_OPTIONS_API, "Custom options for the api files.", null);
207+
addOption(CUSTOM_OPTIONS_MODEL, "Custom options for the model files.", null);
196208
}
197209

198210
@Override
@@ -239,6 +251,14 @@ public void processOpts() {
239251
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
240252
}
241253

254+
if (additionalProperties.containsKey(CUSTOM_OPTIONS_API)) {
255+
this.setCustomOptionsApi((String) additionalProperties.get(CUSTOM_OPTIONS_API));
256+
}
257+
258+
if (additionalProperties.containsKey(CUSTOM_OPTIONS_MODEL)) {
259+
this.setCustomOptionsModel((String) additionalProperties.get(CUSTOM_OPTIONS_MODEL));
260+
}
261+
242262
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
243263
}
244264

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)