Skip to content

Commit

Permalink
enum zero index is UNSPECIFIED suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyxilu66 authored and lucy66hw committed Jan 15, 2025
1 parent 5785743 commit 9d71328
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/generators/protobuf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ title: Documentation for the protobuf-schema Generator
## CONFIG OPTIONS
These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.

| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|numberedFieldNumberList|Field numbers in order.| |false|
|startEnumsWithUnknown|Introduces "UNKNOWN" as the first element of enumerations.| |false|
| Option | Description | Values | Default |
|---------------------------|--------------------------------------------------------------------------| ------ | ------- |
| numberedFieldNumberList | Field numbers in order. | |false|
| startEnumsWithUnspecified | Introduces "UNSPECIFIED" as the first element of enumerations. | |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf

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_UNSPECIFIED = "startEnumsWithUnspecified";

private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);

@Setter protected String packageName = "openapitools";

private boolean numberedFieldNumberList = false;

private boolean startEnumsWithUnknown = false;
private boolean startEnumsWithUnspecified = false;

@Override
public CodegenType getTag() {
Expand Down Expand Up @@ -164,7 +164,7 @@ public ProtobufSchemaCodegen() {
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_UNSPECIFIED, "Introduces \"UNSPECIFIED\" as the first element of enumerations.", startEnumsWithUnspecified);
}

@Override
Expand Down Expand Up @@ -195,8 +195,8 @@ public void processOpts() {
this.numberedFieldNumberList = convertPropertyToBooleanAndWriteBack(NUMBERED_FIELD_NUMBER_LIST);
}

if (additionalProperties.containsKey(this.START_ENUMS_WITH_UNKNOWN)) {
this.startEnumsWithUnknown = convertPropertyToBooleanAndWriteBack(START_ENUMS_WITH_UNKNOWN);
if (additionalProperties.containsKey(this.START_ENUMS_WITH_UNSPECIFIED)) {
this.startEnumsWithUnspecified = convertPropertyToBooleanAndWriteBack(START_ENUMS_WITH_UNSPECIFIED);
}

supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
Expand Down Expand Up @@ -233,6 +233,7 @@ public void addEnumValuesPrefix(Map<String, Object> allowableValues, String pref
String name = (String) value.get("name");
value.put("name", prefix + "_" + name);
value.put("value", "\"" + prefix + "_" + name + "\"");

}
}

Expand All @@ -249,22 +250,21 @@ public void addEnumValuesPrefix(Map<String, Object> allowableValues, String pref
*
* @param allowableValues allowable values
*/
public void addUnknownToAllowableValues(Map<String, Object> allowableValues) {
if (startEnumsWithUnknown) {
public void addUnspecifiedToAllowableValues(Map<String, Object> allowableValues) {
if (startEnumsWithUnspecified) {
if (allowableValues.containsKey("enumVars")) {
List<Map<String, Object>> enumVars = (List<Map<String, Object>>) allowableValues.get("enumVars");

HashMap<String, Object> unknown = new HashMap<String, Object>();
unknown.put("name", "UNKNOWN");
unknown.put("isString", "false");
unknown.put("value", "\"UNKNOWN\"");

enumVars.add(0, unknown);
HashMap<String, Object> unspecified = new HashMap<String, Object>();
unspecified.put("name", "UNSPECIFIED");
unspecified.put("isString", "false");
unspecified.put("value", "\"UNSPECIFIED\"");
enumVars.add(0, unspecified);
}

if (allowableValues.containsKey("values")) {
List<String> values = (List<String>) allowableValues.get("values");
values.add(0, "UNKNOWN");
values.add(0, "UNSPECIFIED");
}
}
}
Expand All @@ -291,7 +291,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {

if (cm.isEnum) {
Map<String, Object> allowableValues = cm.getAllowableValues();
addUnknownToAllowableValues(allowableValues);
addUnspecifiedToAllowableValues(allowableValues);
addEnumValuesPrefix(allowableValues, cm.getClassname());
if (allowableValues.containsKey("enumVars")) {
List<Map<String, Object>> enumVars = (List<Map<String, Object>>) allowableValues.get("enumVars");
Expand Down Expand Up @@ -319,7 +319,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

if (var.isEnum) {
addUnknownToAllowableValues(var.allowableValues);
addUnspecifiedToAllowableValues(var.allowableValues);
addEnumValuesPrefix(var.allowableValues, var.getEnumName());

if (var.allowableValues.containsKey("enumVars")) {
Expand Down

0 comments on commit 9d71328

Please sign in to comment.