Skip to content

Commit 059fdca

Browse files
author
andrewwilsonnew
committed
improving with simple enum names
1 parent 11c5611 commit 059fdca

2 files changed

Lines changed: 88 additions & 8 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ProtobufSchemaCodegen.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
6666

6767
public static final String WRAP_COMPLEX_TYPE = "wrapComplexType";
6868

69+
public static final String SIMPLE_ENUM_NAME = "enumSimpleName";
70+
6971
public static final String AGGREGATE_MODELS_NAME = "aggregateModelsName";
7072

7173
private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class);
@@ -82,6 +84,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
8284

8385
private boolean wrapComplexType = true;
8486

87+
private boolean simpleEnumName = false;
88+
8589
@Override
8690
public CodegenType getTag() {
8791
return CodegenType.SCHEMA;
@@ -192,6 +196,7 @@ public ProtobufSchemaCodegen() {
192196
addSwitch(START_ENUMS_WITH_UNSPECIFIED, "Introduces \"UNSPECIFIED\" as the first element of enumerations.", startEnumsWithUnspecified);
193197
addSwitch(ADD_JSON_NAME_ANNOTATION, "Append \"json_name\" annotation to message field when the specification name differs from the protobuf field name", addJsonNameAnnotation);
194198
addSwitch(WRAP_COMPLEX_TYPE, "Generate Additional message for complex type", wrapComplexType);
199+
addSwitch(SIMPLE_ENUM_NAME, "Use a simple name for enums", simpleEnumName);
195200
addOption(AGGREGATE_MODELS_NAME, "Aggregated model filename. If set, all generated models will be combined into this single file.", null);
196201
}
197202

@@ -219,22 +224,26 @@ public void processOpts() {
219224
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
220225
}
221226

222-
if (additionalProperties.containsKey(this.NUMBERED_FIELD_NUMBER_LIST)) {
227+
if (additionalProperties.containsKey(NUMBERED_FIELD_NUMBER_LIST)) {
223228
this.numberedFieldNumberList = convertPropertyToBooleanAndWriteBack(NUMBERED_FIELD_NUMBER_LIST);
224229
}
225230

226-
if (additionalProperties.containsKey(this.START_ENUMS_WITH_UNSPECIFIED)) {
231+
if (additionalProperties.containsKey(START_ENUMS_WITH_UNSPECIFIED)) {
227232
this.startEnumsWithUnspecified = convertPropertyToBooleanAndWriteBack(START_ENUMS_WITH_UNSPECIFIED);
228233
}
229234

230-
if (additionalProperties.containsKey(this.ADD_JSON_NAME_ANNOTATION)) {
235+
if (additionalProperties.containsKey(ADD_JSON_NAME_ANNOTATION)) {
231236
this.addJsonNameAnnotation = convertPropertyToBooleanAndWriteBack(ADD_JSON_NAME_ANNOTATION);
232237
}
233238

234-
if (additionalProperties.containsKey(this.WRAP_COMPLEX_TYPE)) {
239+
if (additionalProperties.containsKey(WRAP_COMPLEX_TYPE)) {
235240
this.wrapComplexType = convertPropertyToBooleanAndWriteBack(WRAP_COMPLEX_TYPE);
236241
}
237242

243+
if (additionalProperties.containsKey(SIMPLE_ENUM_NAME)) {
244+
this.simpleEnumName = convertPropertyToBooleanAndWriteBack(SIMPLE_ENUM_NAME);
245+
}
246+
238247
if (additionalProperties.containsKey(AGGREGATE_MODELS_NAME)) {
239248
this.setAggregateModelsName((String) additionalProperties.get(AGGREGATE_MODELS_NAME));
240249
}
@@ -489,16 +498,15 @@ public void addEnumValuesPrefix(Map<String, Object> allowableValues, String pref
489498
prefix = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, prefix);
490499
for (Map<String, Object> value : enumVars) {
491500
String name = (String) value.get("name");
492-
value.put("name", prefix + "_" + name);
493-
value.put("value", "\"" + prefix + "_" + name + "\"");
494-
501+
value.put("name", simpleEnumName ? name : prefix + "_" + name);
502+
value.put("value", simpleEnumName ? name : "\"" + prefix + "_" + name + "\"");
495503
}
496504
}
497505

498506
if (allowableValues.containsKey("values")) {
499507
List<Object> values = (List<Object>) allowableValues.get("values");
500508
for (Object value : values) {
501-
value = prefix + "_" + String.valueOf(value);
509+
value = simpleEnumName ? value : prefix + "_" + value;
502510
}
503511
}
504512
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/protobuf/ProtobufSchemaCodegenTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
package org.openapitools.codegen.protobuf;
1818

1919
import io.swagger.v3.oas.models.OpenAPI;
20+
import io.swagger.v3.oas.models.media.IntegerSchema;
21+
import io.swagger.v3.oas.models.media.Schema;
22+
import io.swagger.v3.oas.models.media.StringSchema;
2023
import org.openapitools.codegen.ClientOptInput;
2124
import org.openapitools.codegen.CodegenModel;
25+
import org.openapitools.codegen.CodegenProperty;
2226
import org.openapitools.codegen.DefaultGenerator;
2327
import org.openapitools.codegen.TestUtils;
2428
import org.openapitools.codegen.config.CodegenConfigurator;
@@ -34,8 +38,12 @@
3438
import java.nio.file.Files;
3539
import java.nio.file.Path;
3640
import java.nio.file.Paths;
41+
import java.util.Arrays;
3742
import java.util.List;
43+
import java.util.Map;
3844

45+
import static org.openapitools.codegen.TestUtils.createCodegenModelWrapper;
46+
import static org.openapitools.codegen.languages.ProtobufSchemaCodegen.SIMPLE_ENUM_NAME;
3947
import static org.testng.Assert.assertEquals;
4048

4149
public class ProtobufSchemaCodegenTest {
@@ -141,4 +149,68 @@ public void modelTest() {
141149
Assert.assertEquals(simpleName.classname, "DollarModel");
142150
Assert.assertEquals(simpleName.classVarName, "dollar_model");
143151
}
152+
153+
@Test(description = "support complex enum values")
154+
public void supportComplexEnumValues() {
155+
testEnumValues(false);
156+
}
157+
158+
@Test(description = "support simple enum values")
159+
public void supportSimpleEnumValues() {
160+
testEnumValues(true);
161+
}
162+
163+
private void testEnumValues(boolean simpleEnumValue) {
164+
final Schema model = new Schema()
165+
.description("a sample model")
166+
.addProperties("testStringEnum", new StringSchema()._enum(Arrays.asList("foo", "bar")))
167+
.addProperties("testIntEnum", new IntegerSchema().addEnumItem(1).addEnumItem(2));
168+
final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen();
169+
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
170+
codegen.setOpenAPI(openAPI);
171+
final CodegenModel cm = codegen.fromModel("sample", model);
172+
codegen.additionalProperties().put(SIMPLE_ENUM_NAME, simpleEnumValue);
173+
codegen.processOpts();
174+
codegen.postProcessModels(createCodegenModelWrapper(cm));
175+
176+
final CodegenProperty property1 = cm.vars.get(0);
177+
Assert.assertEquals(property1.baseName, "testStringEnum");
178+
Assert.assertEquals(property1.dataType, "string");
179+
Assert.assertEquals(property1.baseType, "string");
180+
Assert.assertEquals(property1.datatypeWithEnum, "Test_string_enum");
181+
Assert.assertEquals(property1.name, "test_string_enum");
182+
Assert.assertTrue(property1.isEnum);
183+
Assert.assertEquals(property1.allowableValues.size(), 2);
184+
Assert.assertEquals(((List<String>) property1.allowableValues.get("values")).size(), 2);
185+
List<Map<String, Object>> enumVars1 = (List<Map<String, Object>>) property1.allowableValues.get("enumVars");
186+
Assert.assertEquals(enumVars1.size(), 2);
187+
188+
Assert.assertEquals(enumVars1.get(0).get("name"), simpleEnumValue ? "FOO" : "TEST_STRING_ENUM_FOO");
189+
Assert.assertEquals(enumVars1.get(0).get("value"), simpleEnumValue ? "FOO" : "\"TEST_STRING_ENUM_FOO\"");
190+
Assert.assertEquals(enumVars1.get(0).get("isString"), false);
191+
192+
Assert.assertEquals(enumVars1.get(1).get("name"), simpleEnumValue ? "BAR" : "TEST_STRING_ENUM_BAR");
193+
Assert.assertEquals(enumVars1.get(1).get("value"), simpleEnumValue ? "BAR" : "\"TEST_STRING_ENUM_BAR\"");
194+
Assert.assertEquals(enumVars1.get(1).get("isString"), false);
195+
196+
final CodegenProperty property2 = cm.vars.get(1);
197+
Assert.assertEquals(property2.baseName, "testIntEnum");
198+
Assert.assertEquals(property2.dataType, "int32");
199+
Assert.assertEquals(property2.baseType, "int32");
200+
Assert.assertEquals(property2.datatypeWithEnum, "Test_int_enum");
201+
Assert.assertEquals(property2.name, "test_int_enum");
202+
Assert.assertTrue(property2.isEnum);
203+
Assert.assertEquals(property2.allowableValues.size(), 2);
204+
Assert.assertEquals(((List<String>) property2.allowableValues.get("values")).size(), 2);
205+
List<Map<String, Object>> enumVars2 = (List<Map<String, Object>>) property2.allowableValues.get("enumVars");
206+
Assert.assertEquals(enumVars2.size(), 2);
207+
208+
Assert.assertEquals(enumVars2.get(0).get("name"), simpleEnumValue ? "_1" : "TEST_INT_ENUM__1");
209+
Assert.assertEquals(enumVars2.get(0).get("value"), simpleEnumValue ? "_1" : "\"TEST_INT_ENUM__1\"");
210+
Assert.assertEquals(enumVars2.get(0).get("isString"), false);
211+
212+
Assert.assertEquals(enumVars2.get(1).get("name"), simpleEnumValue ? "_2" : "TEST_INT_ENUM__2");
213+
Assert.assertEquals(enumVars2.get(1).get("value"), simpleEnumValue ? "_2" : "\"TEST_INT_ENUM__2\"");
214+
Assert.assertEquals(enumVars2.get(1).get("isString"), false);
215+
}
144216
}

0 commit comments

Comments
 (0)