diff --git a/bin/configs/protobuf-schema-config.yaml b/bin/configs/protobuf-schema-config.yaml index e1fcd9f97e85..f53cff357308 100644 --- a/bin/configs/protobuf-schema-config.yaml +++ b/bin/configs/protobuf-schema-config.yaml @@ -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" diff --git a/docs/generators/protobuf-schema.md b/docs/generators/protobuf-schema.md index 6cf2250ab4b0..fc8616619336 100644 --- a/docs/generators/protobuf-schema.md +++ b/docs/generators/protobuf-schema.md @@ -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| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java index 225d9fab5518..6dee36ccfe94 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java @@ -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 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 @@ -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 { diff --git a/modules/openapi-generator/src/main/resources/protobuf-schema/api.mustache b/modules/openapi-generator/src/main/resources/protobuf-schema/api.mustache index 8f804af4b6c3..fd22c4d7dcbf 100644 --- a/modules/openapi-generator/src/main/resources/protobuf-schema/api.mustache +++ b/modules/openapi-generator/src/main/resources/protobuf-schema/api.mustache @@ -3,6 +3,9 @@ syntax = "proto3"; package {{#lambda.lowercase}}{{{packageName}}}.{{{apiPackage}}}.{{{classname}}};{{/lambda.lowercase}} +{{#customOptionsApi}} +{{{.}}} +{{/customOptionsApi}} import "google/protobuf/empty.proto"; {{#imports}} {{#import}} diff --git a/modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache b/modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache index 817bb5f1e607..c0ec053732c1 100644 --- a/modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache +++ b/modules/openapi-generator/src/main/resources/protobuf-schema/model.mustache @@ -3,6 +3,9 @@ syntax = "proto3"; package {{#lambda.lowercase}}{{{packageName}}};{{/lambda.lowercase}} +{{#customOptionsModel}} +{{{.}}} +{{/customOptionsModel}} {{#imports}} {{#import}} import public "{{{.}}}.proto"; diff --git a/samples/config/petstore/protobuf-schema-config/models/data.proto b/samples/config/petstore/protobuf-schema-config/models/data.proto index 45298f62de1a..934cd994cc4c 100644 --- a/samples/config/petstore/protobuf-schema-config/models/data.proto +++ b/samples/config/petstore/protobuf-schema-config/models/data.proto @@ -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 { diff --git a/samples/config/petstore/protobuf-schema-config/services/default_service.proto b/samples/config/petstore/protobuf-schema-config/services/default_service.proto index 762534348232..ed0454f7ad46 100644 --- a/samples/config/petstore/protobuf-schema-config/services/default_service.proto +++ b/samples/config/petstore/protobuf-schema-config/services/default_service.proto @@ -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"; diff --git a/samples/config/petstore/protobuf-schema-config/services/pet_service.proto b/samples/config/petstore/protobuf-schema-config/services/pet_service.proto index c083e9a55cd6..2ecee05014f4 100644 --- a/samples/config/petstore/protobuf-schema-config/services/pet_service.proto +++ b/samples/config/petstore/protobuf-schema-config/services/pet_service.proto @@ -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"; diff --git a/samples/config/petstore/protobuf-schema-config/services/store_service.proto b/samples/config/petstore/protobuf-schema-config/services/store_service.proto index 62c3aaf00ab6..4a435c8fe67c 100644 --- a/samples/config/petstore/protobuf-schema-config/services/store_service.proto +++ b/samples/config/petstore/protobuf-schema-config/services/store_service.proto @@ -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"; diff --git a/samples/config/petstore/protobuf-schema-config/services/user_service.proto b/samples/config/petstore/protobuf-schema-config/services/user_service.proto index 888cd12a3ff9..2573cfa4edae 100644 --- a/samples/config/petstore/protobuf-schema-config/services/user_service.proto +++ b/samples/config/petstore/protobuf-schema-config/services/user_service.proto @@ -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";