[Java] Getter/Setter naming convention not followed in generated models (#2095)

fix the getter/setter when the second letter of the field name is already uppercase (following the JavaBeans API specification)
This commit is contained in:
Vincent Devos
2019-02-18 11:00:35 +01:00
committed by William Cheng
parent ad8aa7dc0e
commit 594af33fb8
3 changed files with 62 additions and 4 deletions

View File

@@ -1551,7 +1551,7 @@ public class DefaultCodegen implements CodegenConfig {
}
/**
* 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

View File

@@ -1378,10 +1378,12 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
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)) {
@@ -1391,6 +1393,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return booleanValue;
}
@Override
public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}
@@ -1416,6 +1419,33 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
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) {