forked from loafle/openapi-generator-original
[feat] [protobuf] Improve protobuf generator by adding custom options for api and model files (#21075)
* add custom options * fixing docs * typo
This commit is contained in:
parent
b4378a6277
commit
5e446b4147
@ -10,6 +10,14 @@ additionalProperties:
|
|||||||
wrapComplexType: false
|
wrapComplexType: false
|
||||||
supportMultipleResponses: false
|
supportMultipleResponses: false
|
||||||
aggregateModelsName: data
|
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
|
useSimplifiedEnumNames: true
|
||||||
typeMappings:
|
typeMappings:
|
||||||
object: "google.protobuf.Struct"
|
object: "google.protobuf.Struct"
|
||||||
|
@ -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|
|
|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|
|
|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|
|
|numberedFieldNumberList|Field numbers in order.| |false|
|
||||||
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
|
|startEnumsWithUnspecified|Introduces "UNSPECIFIED" as the first element of enumerations.| |false|
|
||||||
|supportMultipleResponses|Support multiple responses| |true|
|
|supportMultipleResponses|Support multiple responses| |true|
|
||||||
|
@ -70,6 +70,10 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
|
|
||||||
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
|
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";
|
public static final String SUPPORT_MULTIPLE_RESPONSES = "supportMultipleResponses";
|
||||||
|
|
||||||
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
|
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
|
||||||
@ -78,6 +82,12 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
|
|
||||||
@Setter protected String aggregateModelsName = null;
|
@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 numberedFieldNumberList = false;
|
||||||
|
|
||||||
private boolean startEnumsWithUnspecified = false;
|
private boolean startEnumsWithUnspecified = false;
|
||||||
@ -203,6 +213,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
addSwitch(USE_SIMPLIFIED_ENUM_NAMES, "Use a simple name for enums", useSimplifiedEnumNames);
|
addSwitch(USE_SIMPLIFIED_ENUM_NAMES, "Use a simple name for enums", useSimplifiedEnumNames);
|
||||||
addSwitch(SUPPORT_MULTIPLE_RESPONSES, "Support multiple responses", supportMultipleResponses);
|
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(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
|
@Override
|
||||||
@ -253,6 +265,14 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
|||||||
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
|
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)) {
|
if (additionalProperties.containsKey(this.SUPPORT_MULTIPLE_RESPONSES)) {
|
||||||
this.supportMultipleResponses = convertPropertyToBooleanAndWriteBack(SUPPORT_MULTIPLE_RESPONSES);
|
this.supportMultipleResponses = convertPropertyToBooleanAndWriteBack(SUPPORT_MULTIPLE_RESPONSES);
|
||||||
} else {
|
} else {
|
||||||
|
@ -3,6 +3,9 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package {{#lambda.lowercase}}{{{packageName}}}.{{{apiPackage}}}.{{{classname}}};{{/lambda.lowercase}}
|
package {{#lambda.lowercase}}{{{packageName}}}.{{{apiPackage}}}.{{{classname}}};{{/lambda.lowercase}}
|
||||||
|
|
||||||
|
{{#customOptionsApi}}
|
||||||
|
{{{.}}}
|
||||||
|
{{/customOptionsApi}}
|
||||||
import "google/protobuf/empty.proto";
|
import "google/protobuf/empty.proto";
|
||||||
{{#imports}}
|
{{#imports}}
|
||||||
{{#import}}
|
{{#import}}
|
||||||
|
@ -3,6 +3,9 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package {{#lambda.lowercase}}{{{packageName}}};{{/lambda.lowercase}}
|
package {{#lambda.lowercase}}{{{packageName}}};{{/lambda.lowercase}}
|
||||||
|
|
||||||
|
{{#customOptionsModel}}
|
||||||
|
{{{.}}}
|
||||||
|
{{/customOptionsModel}}
|
||||||
{{#imports}}
|
{{#imports}}
|
||||||
{{#import}}
|
{{#import}}
|
||||||
import public "{{{.}}}.proto";
|
import public "{{{.}}}.proto";
|
||||||
|
@ -12,6 +12,10 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package petstore;
|
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";
|
import public "google/protobuf/struct.proto";
|
||||||
|
|
||||||
message ApiResponse {
|
message ApiResponse {
|
||||||
|
@ -12,6 +12,10 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package petstore.services.defaultservice;
|
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 "google/protobuf/empty.proto";
|
||||||
import public "models/data.proto";
|
import public "models/data.proto";
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package petstore.services.petservice;
|
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 "google/protobuf/empty.proto";
|
||||||
import public "models/data.proto";
|
import public "models/data.proto";
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package petstore.services.storeservice;
|
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 "google/protobuf/empty.proto";
|
||||||
import public "models/data.proto";
|
import public "models/data.proto";
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package petstore.services.userservice;
|
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 "google/protobuf/empty.proto";
|
||||||
import public "models/data.proto";
|
import public "models/data.proto";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user