Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8 changes: 8 additions & 0 deletions bin/configs/protobuf-schema-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions docs/generators/protobuf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -203,6 +213,8 @@ public ProtobufSchemaCodegen() {
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
Expand Down Expand Up @@ -253,6 +265,14 @@ public void processOpts() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ syntax = "proto3";

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

{{#customOptionsApi}}
{{{.}}}
{{/customOptionsApi}}
import "google/protobuf/empty.proto";
{{#imports}}
{{#import}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ syntax = "proto3";

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

{{#customOptionsModel}}
{{{.}}}
{{/customOptionsModel}}
{{#imports}}
{{#import}}
import public "{{{.}}}.proto";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Loading