forked from loafle/openapi-generator-original
[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:
parent
49e9911b3f
commit
e5159ef8d5
@ -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
|
||||
|
@ -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)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user