[Protobuf-Schema] Add numbered field number list switch (#10893)

* [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 <cermak@merica.cz>
This commit is contained in:
Tomáš Čermák 2021-11-23 03:43:34 +01:00 committed by GitHub
parent 49e9911b3f
commit e5159ef8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -7,6 +7,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Option | Description | Values | Default | | Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- | | ------ | ----------- | ------ | ------- |
|numberedFieldNumberList|Field numbers in order.| |false|
|startEnumsWithUnknown|Introduces &quot;UNKNOWN&quot; as the first element of enumerations.| |false| |startEnumsWithUnknown|Introduces &quot;UNKNOWN&quot; as the first element of enumerations.| |false|
## IMPORT MAPPING ## IMPORT MAPPING

View File

@ -47,12 +47,16 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
private static final String IMPORTS = "imports"; private static final String IMPORTS = "imports";
public static final String NUMBERED_FIELD_NUMBER_LIST = "numberedFieldNumberList";
public static final String START_ENUMS_WITH_UNKNOWN = "startEnumsWithUnknown"; public static final String START_ENUMS_WITH_UNKNOWN = "startEnumsWithUnknown";
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class); private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
protected String packageName = "openapitools"; protected String packageName = "openapitools";
private boolean numberedFieldNumberList = false;
private boolean startEnumsWithUnknown = false; private boolean startEnumsWithUnknown = false;
@Override @Override
@ -149,6 +153,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
cliOptions.clear(); 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); 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)); 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)) { if (additionalProperties.containsKey(this.START_ENUMS_WITH_UNKNOWN)) {
this.startEnumsWithUnknown = convertPropertyToBooleanAndWriteBack(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) { for (CodegenProperty var : cm.vars) {
// add x-protobuf-type: repeated if it's an array // add x-protobuf-type: repeated if it's an array
if (Boolean.TRUE.equals(var.isArray)) { if (Boolean.TRUE.equals(var.isArray)) {
@ -265,11 +275,17 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
} }
// Add x-protobuf-index, unless already specified // Add x-protobuf-index, unless already specified
try { if(this.numberedFieldNumberList) {
var.vendorExtensions.putIfAbsent("x-protobuf-index", generateFieldNumberFromString(var.getName())); var.vendorExtensions.putIfAbsent("x-protobuf-index", index);
} catch (ProtoBufIndexComputationException e) { index++;
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)"); 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)");
}
} }
} }
} }