Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1349,10 +1349,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 @@ -1362,6 +1364,7 @@ public boolean convertPropertyToBoolean(String propertyKey) {
return booleanValue;
}

@Override
public void writePropertyBack(String propertyKey, boolean value) {
additionalProperties.put(propertyKey, value);
}
Expand All @@ -1387,6 +1390,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 @@ -227,7 +227,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 @@ -468,8 +468,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 @@ -507,6 +507,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