forked from loafle/openapi-generator-original
[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:
committed by
William Cheng
parent
ad8aa7dc0e
commit
594af33fb8
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -225,7 +225,7 @@ public class JavaModelTest {
|
||||
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")
|
||||
@@ -466,8 +466,8 @@ public class JavaModelTest {
|
||||
|
||||
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);
|
||||
@@ -505,6 +505,34 @@ public class JavaModelTest {
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user