From 24c7cb9cec74e7f43da977d1196cb46185a47775 Mon Sep 17 00:00:00 2001 From: andrewwilsonnew Date: Fri, 11 Apr 2025 11:44:05 +0100 Subject: [PATCH 1/3] add custom options --- bin/configs/protobuf-schema-config.yaml | 8 ++++++++ .../languages/ProtobufSchemaCodegen.java | 20 +++++++++++++++++++ .../resources/protobuf-schema/api.mustache | 3 +++ .../resources/protobuf-schema/model.mustache | 3 +++ .../protobuf-schema-config/models/data.proto | 4 ++++ .../services/default_service.proto | 4 ++++ .../services/pet_service.proto | 4 ++++ .../services/store_service.proto | 4 ++++ .../services/user_service.proto | 4 ++++ 9 files changed, 54 insertions(+) diff --git a/bin/configs/protobuf-schema-config.yaml b/bin/configs/protobuf-schema-config.yaml index 5000698d79f6..8e0609a854f9 100644 --- a/bin/configs/protobuf-schema-config.yaml +++ b/bin/configs/protobuf-schema-config.yaml @@ -9,6 +9,14 @@ additionalProperties: startEnumsWithUnspecified: true wrapComplexType: 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"; typeMappings: object: "google.protobuf.Struct" importMappings: 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 362249ab3be2..3a4143fb6584 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 @@ -68,12 +68,22 @@ 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"; + private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class); @Setter protected String packageName = "openapitools"; @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; @@ -193,6 +203,8 @@ public ProtobufSchemaCodegen() { addSwitch(ADD_JSON_NAME_ANNOTATION, "Append \"json_name\" annotation to message field when the specification name differs from the protobuf field name", addJsonNameAnnotation); addSwitch(WRAP_COMPLEX_TYPE, "Generate Additional message for complex type", wrapComplexType); 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 @@ -239,6 +251,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)); + } + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); } 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 cab1a8ac794b..d9da7e706a57 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 0a3854cdef4b..bdf82870ac87 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"; From 7907d1eef030d16d41baa31cef38ef0f310534e2 Mon Sep 17 00:00:00 2001 From: andrewwilsonnew Date: Fri, 11 Apr 2025 12:02:09 +0100 Subject: [PATCH 2/3] fixing docs --- docs/generators/protobuf-schema.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/generators/protobuf-schema.md b/docs/generators/protobuf-schema.md index d3ddc215aeea..7bca05a8318d 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| From a60b9951c3e16316b7d08c2bf013e8aaa6807b9b Mon Sep 17 00:00:00 2001 From: andrewwilsonnew Date: Fri, 11 Apr 2025 12:08:58 +0100 Subject: [PATCH 3/3] typo --- .../openapitools/codegen/languages/ProtobufSchemaCodegen.java | 1 + 1 file changed, 1 insertion(+) 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 044a02f51c1a..6f8b2ef6818d 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 @@ -262,6 +262,7 @@ public void processOpts() { 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);