[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 |
| ------ | ----------- | ------ | ------- |
|numberedFieldNumberList|Field numbers in order.| |false|
|startEnumsWithUnknown|Introduces &quot;UNKNOWN&quot; as the first element of enumerations.| |false|
## IMPORT MAPPING

View File

@ -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,6 +275,11 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
}
// Add x-protobuf-index, unless already specified
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) {
@ -273,6 +288,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
}
}
}
}
return objs;
}