From e5159ef8d5a7f39777eaefe5d7fc0c4c83f79c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20=C4=8Cerm=C3=A1k?= Date: Tue, 23 Nov 2021 03:43:34 +0100 Subject: [PATCH] [Protobuf-Schema] Add numbered field number list switch (#10893) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Protobuf-Schema] Add numbered field number list switch * [Protobuf-Schema] Add numbered field number list switch * [Protobuf-Schema] Added switch * [Docs][Protobuf-Schema] Generated docs Co-authored-by: Tomáš Čermák --- docs/generators/protobuf-schema.md | 1 + .../languages/ProtobufSchemaCodegen.java | 26 +++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/generators/protobuf-schema.md b/docs/generators/protobuf-schema.md index 53e5334ef97..a7d1f1d095b 100644 --- a/docs/generators/protobuf-schema.md +++ b/docs/generators/protobuf-schema.md @@ -7,6 +7,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl | Option | Description | Values | Default | | ------ | ----------- | ------ | ------- | +|numberedFieldNumberList|Field numbers in order.| |false| |startEnumsWithUnknown|Introduces "UNKNOWN" as the first element of enumerations.| |false| ## IMPORT MAPPING 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 30a8b4c3c32..5f55e9d47f0 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 @@ -47,12 +47,16 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf private static final String IMPORTS = "imports"; + public static final String NUMBERED_FIELD_NUMBER_LIST = "numberedFieldNumberList"; + public static final String START_ENUMS_WITH_UNKNOWN = "startEnumsWithUnknown"; private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class); protected String packageName = "openapitools"; + private boolean numberedFieldNumberList = false; + private boolean startEnumsWithUnknown = false; @Override @@ -149,6 +153,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf cliOptions.clear(); + addSwitch(NUMBERED_FIELD_NUMBER_LIST, "Field numbers in order.", numberedFieldNumberList); addSwitch(START_ENUMS_WITH_UNKNOWN, "Introduces \"UNKNOWN\" as the first element of enumerations.", startEnumsWithUnknown); } @@ -169,6 +174,10 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME)); } + if (additionalProperties.containsKey(this.NUMBERED_FIELD_NUMBER_LIST)) { + this.numberedFieldNumberList = convertPropertyToBooleanAndWriteBack(NUMBERED_FIELD_NUMBER_LIST); + } + if (additionalProperties.containsKey(this.START_ENUMS_WITH_UNKNOWN)) { this.startEnumsWithUnknown = convertPropertyToBooleanAndWriteBack(START_ENUMS_WITH_UNKNOWN); } @@ -239,6 +248,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf } } + int index = 1; for (CodegenProperty var : cm.vars) { // add x-protobuf-type: repeated if it's an array if (Boolean.TRUE.equals(var.isArray)) { @@ -265,11 +275,17 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf } // Add x-protobuf-index, unless already specified - try { - var.vendorExtensions.putIfAbsent("x-protobuf-index", generateFieldNumberFromString(var.getName())); - } catch (ProtoBufIndexComputationException e) { - LOGGER.error("Exception when assigning a index to a protobuf field", e); - var.vendorExtensions.putIfAbsent("x-protobuf-index", "Generated field number is in reserved range (19000, 19999)"); + if(this.numberedFieldNumberList) { + var.vendorExtensions.putIfAbsent("x-protobuf-index", index); + index++; + } + else { + try { + var.vendorExtensions.putIfAbsent("x-protobuf-index", generateFieldNumberFromString(var.getName())); + } catch (ProtoBufIndexComputationException e) { + LOGGER.error("Exception when assigning a index to a protobuf field", e); + var.vendorExtensions.putIfAbsent("x-protobuf-index", "Generated field number is in reserved range (19000, 19999)"); + } } } }