Skip to content

Commit

Permalink
[Java] Getter/Setter naming convention not followed in generated mode…
Browse files Browse the repository at this point in the history
…ls (OpenAPITools#2095)

fix the getter/setter when the second letter of the field name is already uppercase (following the JavaBeans API specification)
  • Loading branch information
karismann authored and wing328 committed Feb 18, 2019
1 parent 315d89d commit e418331
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ public String toGetter(String name) {
}

/**
* Output the Getter name, e.g. getSize
* Output the Setter name, e.g. setSize
*
* @param name the name of the property
* @return setter name based on naming convention
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1378,10 +1378,12 @@ public void setSupportJava6(boolean value) {
this.supportJava6 = value;
}

@Override
public String toRegularExpression(String pattern) {
return escapeText(pattern);
}

@Override
public boolean convertPropertyToBoolean(String propertyKey) {
boolean booleanValue = false;
if (additionalProperties.containsKey(propertyKey)) {
Expand All @@ -1391,6 +1393,7 @@ public boolean convertPropertyToBoolean(String propertyKey) {
return booleanValue;
}

@Override
public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}
Expand All @@ -1416,6 +1419,33 @@ public String sanitizeTag(String tag) {
return tag;
}

/**
* Camelize the method name of the getter and setter
*
* @param name string to be camelized
* @return Camelized string
*/
@Override
public String getterAndSetterCapitalize(String name) {
boolean lowercaseFirstLetter = false;
if (name == null || name.length() == 0) {
return name;
}
name = toVarName(name);
//
// Let the property name capitalized
// except when the first letter of the property name is lowercase and the second letter is uppercase
// Refer to section 8.8: Capitalization of inferred names of the JavaBeans API specification
// http://download.oracle.com/otn-pub/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/beans.101.pdf)
//
if (name.length() > 1 && Character.isLowerCase(name.charAt(0)) && Character.isUpperCase(name.charAt(1))) {
lowercaseFirstLetter = true;
}
return camelize(name, lowercaseFirstLetter);
}



@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void list2DPropertyTest() {
Assert.assertTrue(property.isContainer);
}

@Test(description = "convert a model with restriced characters")
@Test(description = "convert a model with restricted characters")
public void restrictedCharactersPropertiesTest() {
final Schema schema = new Schema()
.description("a sample model")
Expand Down Expand Up @@ -466,8 +466,8 @@ public void secondCharUpperCaseNamesTest() {

final CodegenProperty property = cm.vars.get(0);
Assert.assertEquals(property.baseName, "pId");
Assert.assertEquals(property.getter, "getPId");
Assert.assertEquals(property.setter, "setPId");
Assert.assertEquals(property.getter, "getpId");
Assert.assertEquals(property.setter, "setpId");
Assert.assertEquals(property.dataType, "String");
Assert.assertEquals(property.name, "pId");
Assert.assertEquals(property.defaultValue, null);
Expand Down Expand Up @@ -505,6 +505,34 @@ public void firstTwoUpperCaseLetterNamesTest() {
Assert.assertFalse(property.isContainer);
}

@Test(description = "convert a model with an all upper-case letter and one non letter property names")
public void allUpperCaseOneNonLetterNamesTest() {
final Schema schema = new Schema()
.description("a model with a property name starting with two upper-case letters")
.addProperties("ATT_NAME", new StringSchema())
.addRequiredItem("ATT_NAME");
final DefaultCodegen codegen = new JavaClientCodegen();
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
codegen.setOpenAPI(openAPI);
final CodegenModel cm = codegen.fromModel("sample", schema);

Assert.assertEquals(cm.name, "sample");
Assert.assertEquals(cm.classname, "Sample");
Assert.assertEquals(cm.vars.size(), 1);

final CodegenProperty property = cm.vars.get(0);
Assert.assertEquals(property.baseName, "ATT_NAME");
Assert.assertEquals(property.getter, "getATTNAME");
Assert.assertEquals(property.setter, "setATTNAME");
Assert.assertEquals(property.dataType, "String");
Assert.assertEquals(property.name, "ATT_NAME");
Assert.assertEquals(property.defaultValue, null);
Assert.assertEquals(property.baseType, "String");
Assert.assertFalse(property.hasMore);
Assert.assertTrue(property.required);
Assert.assertFalse(property.isContainer);
}

@Test(description = "convert hyphens per issue 503")
public void hyphensTest() {
final Schema schema = new Schema()
Expand Down

0 comments on commit e418331

Please sign in to comment.