Skip to content

Commit e418331

Browse files
karismannwing328
authored andcommitted
[Java] Getter/Setter naming convention not followed in generated models (OpenAPITools#2095)
fix the getter/setter when the second letter of the field name is already uppercase (following the JavaBeans API specification)
1 parent 315d89d commit e418331

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ public String toGetter(String name) {
15511551
}
15521552

15531553
/**
1554-
* Output the Getter name, e.g. getSize
1554+
* Output the Setter name, e.g. setSize
15551555
*
15561556
* @param name the name of the property
15571557
* @return setter name based on naming convention

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,12 @@ public void setSupportJava6(boolean value) {
13781378
this.supportJava6 = value;
13791379
}
13801380

1381+
@Override
13811382
public String toRegularExpression(String pattern) {
13821383
return escapeText(pattern);
13831384
}
13841385

1386+
@Override
13851387
public boolean convertPropertyToBoolean(String propertyKey) {
13861388
boolean booleanValue = false;
13871389
if (additionalProperties.containsKey(propertyKey)) {
@@ -1391,6 +1393,7 @@ public boolean convertPropertyToBoolean(String propertyKey) {
13911393
return booleanValue;
13921394
}
13931395

1396+
@Override
13941397
public void writePropertyBack(String propertyKey, boolean value) {
13951398
additionalProperties.put(propertyKey, value);
13961399
}
@@ -1416,6 +1419,33 @@ public String sanitizeTag(String tag) {
14161419
return tag;
14171420
}
14181421

1422+
/**
1423+
* Camelize the method name of the getter and setter
1424+
*
1425+
* @param name string to be camelized
1426+
* @return Camelized string
1427+
*/
1428+
@Override
1429+
public String getterAndSetterCapitalize(String name) {
1430+
boolean lowercaseFirstLetter = false;
1431+
if (name == null || name.length() == 0) {
1432+
return name;
1433+
}
1434+
name = toVarName(name);
1435+
//
1436+
// Let the property name capitalized
1437+
// except when the first letter of the property name is lowercase and the second letter is uppercase
1438+
// Refer to section 8.8: Capitalization of inferred names of the JavaBeans API specification
1439+
// http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf)
1440+
//
1441+
if (name.length() > 1 && Character.isLowerCase(name.charAt(0)) && Character.isUpperCase(name.charAt(1))) {
1442+
lowercaseFirstLetter = true;
1443+
}
1444+
return camelize(name, lowercaseFirstLetter);
1445+
}
1446+
1447+
1448+
14191449
@Override
14201450
public void postProcessFile(File file, String fileType) {
14211451
if (file == null) {

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void list2DPropertyTest() {
225225
Assert.assertTrue(property.isContainer);
226226
}
227227

228-
@Test(description = "convert a model with restriced characters")
228+
@Test(description = "convert a model with restricted characters")
229229
public void restrictedCharactersPropertiesTest() {
230230
final Schema schema = new Schema()
231231
.description("a sample model")
@@ -466,8 +466,8 @@ public void secondCharUpperCaseNamesTest() {
466466

467467
final CodegenProperty property = cm.vars.get(0);
468468
Assert.assertEquals(property.baseName, "pId");
469-
Assert.assertEquals(property.getter, "getPId");
470-
Assert.assertEquals(property.setter, "setPId");
469+
Assert.assertEquals(property.getter, "getpId");
470+
Assert.assertEquals(property.setter, "setpId");
471471
Assert.assertEquals(property.dataType, "String");
472472
Assert.assertEquals(property.name, "pId");
473473
Assert.assertEquals(property.defaultValue, null);
@@ -505,6 +505,34 @@ public void firstTwoUpperCaseLetterNamesTest() {
505505
Assert.assertFalse(property.isContainer);
506506
}
507507

508+
@Test(description = "convert a model with an all upper-case letter and one non letter property names")
509+
public void allUpperCaseOneNonLetterNamesTest() {
510+
final Schema schema = new Schema()
511+
.description("a model with a property name starting with two upper-case letters")
512+
.addProperties("ATT_NAME", new StringSchema())
513+
.addRequiredItem("ATT_NAME");
514+
final DefaultCodegen codegen = new JavaClientCodegen();
515+
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
516+
codegen.setOpenAPI(openAPI);
517+
final CodegenModel cm = codegen.fromModel("sample", schema);
518+
519+
Assert.assertEquals(cm.name, "sample");
520+
Assert.assertEquals(cm.classname, "Sample");
521+
Assert.assertEquals(cm.vars.size(), 1);
522+
523+
final CodegenProperty property = cm.vars.get(0);
524+
Assert.assertEquals(property.baseName, "ATT_NAME");
525+
Assert.assertEquals(property.getter, "getATTNAME");
526+
Assert.assertEquals(property.setter, "setATTNAME");
527+
Assert.assertEquals(property.dataType, "String");
528+
Assert.assertEquals(property.name, "ATT_NAME");
529+
Assert.assertEquals(property.defaultValue, null);
530+
Assert.assertEquals(property.baseType, "String");
531+
Assert.assertFalse(property.hasMore);
532+
Assert.assertTrue(property.required);
533+
Assert.assertFalse(property.isContainer);
534+
}
535+
508536
@Test(description = "convert hyphens per issue 503")
509537
public void hyphensTest() {
510538
final Schema schema = new Schema()

0 commit comments

Comments
 (0)