mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-02 21:50:55 +00:00
Better control on setter, getter (for boolean, non-boolean) (#6177)
* better control on setter, boolean getter * improve test casees for getter and setter
This commit is contained in:
parent
66f2f70634
commit
e807b81471
@ -207,4 +207,11 @@ public interface CodegenConfig {
|
||||
void setIgnoreFilePathOverride(String ignoreFileOverride);
|
||||
|
||||
String getIgnoreFilePathOverride();
|
||||
|
||||
String toBooleanGetter(String name);
|
||||
|
||||
String toSetter(String name);
|
||||
|
||||
String toGetter(String name);
|
||||
|
||||
}
|
||||
|
@ -1230,6 +1230,36 @@ public class DefaultCodegen {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the Getter name for boolean property, e.g. getActive
|
||||
*
|
||||
* @param name the name of the property
|
||||
* @return getter name based on naming convention
|
||||
*/
|
||||
public String toBooleanGetter(String name) {
|
||||
return "get" + getterAndSetterCapitalize(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the Getter name, e.g. getSize
|
||||
*
|
||||
* @param name the name of the property
|
||||
* @return getter name based on naming convention
|
||||
*/
|
||||
public String toGetter(String name) {
|
||||
return "get" + getterAndSetterCapitalize(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the Getter name, e.g. getSize
|
||||
*
|
||||
* @param name the name of the property
|
||||
* @return setter name based on naming convention
|
||||
*/
|
||||
public String toSetter(String name) {
|
||||
return "set" + getterAndSetterCapitalize(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the API (class) name (capitalized) ending with "Api"
|
||||
* Return DefaultApi if name is empty
|
||||
@ -1518,8 +1548,8 @@ public class DefaultCodegen {
|
||||
property.description = escapeText(p.getDescription());
|
||||
property.unescapedDescription = p.getDescription();
|
||||
property.title = p.getTitle();
|
||||
property.getter = "get" + getterAndSetterCapitalize(name);
|
||||
property.setter = "set" + getterAndSetterCapitalize(name);
|
||||
property.getter = toGetter(name);
|
||||
property.setter = toSetter(name);
|
||||
String example = toExampleValue(p);
|
||||
if(!"null".equals(example)) {
|
||||
property.example = example;
|
||||
@ -1660,7 +1690,7 @@ public class DefaultCodegen {
|
||||
}
|
||||
if (p instanceof BooleanProperty) {
|
||||
property.isBoolean = true;
|
||||
property.getter = "is" + getterAndSetterCapitalize(name);
|
||||
property.getter = toBooleanGetter(name);
|
||||
}
|
||||
if (p instanceof BinaryProperty) {
|
||||
property.isBinary = true;
|
||||
|
@ -1228,4 +1228,14 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
return camelize(sanitizeName(tag));
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the Getter name for boolean property, e.g. isActive
|
||||
*
|
||||
* @param name the name of the property
|
||||
* @return getter name based on naming convention
|
||||
*/
|
||||
public String toBooleanGetter(String name) {
|
||||
return "is" + getterAndSetterCapitalize(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.Xml;
|
||||
import io.swagger.models.parameters.QueryParameter;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.BooleanProperty;
|
||||
import io.swagger.models.properties.ByteArrayProperty;
|
||||
import io.swagger.models.properties.DateTimeProperty;
|
||||
import io.swagger.models.properties.DecimalProperty;
|
||||
@ -653,4 +654,20 @@ public class JavaModelTest {
|
||||
Assert.assertEquals(property3.xmlPrefix, "my");
|
||||
}
|
||||
|
||||
@Test(description = "convert a boolean parameter")
|
||||
public void booleanParameterTest() {
|
||||
final BooleanProperty property = new BooleanProperty();
|
||||
final DefaultCodegen codegen = new JavaClientCodegen();
|
||||
final CodegenProperty cp = codegen.fromProperty("property", property);
|
||||
|
||||
Assert.assertEquals(cp.baseName, "property");
|
||||
Assert.assertEquals(cp.datatype, "Boolean");
|
||||
Assert.assertEquals(cp.name, "property");
|
||||
Assert.assertEquals(cp.baseType, "Boolean");
|
||||
Assert.assertTrue(cp.isNotContainer);
|
||||
Assert.assertTrue(cp.isBoolean);
|
||||
Assert.assertEquals(cp.getter, "isProperty");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class StaticOperationTest {
|
||||
Assert.assertEquals(cp.baseType, "boolean");
|
||||
Assert.assertTrue(cp.isNotContainer);
|
||||
Assert.assertTrue(cp.isBoolean);
|
||||
Assert.assertEquals(cp.getter, "isProperty");
|
||||
Assert.assertEquals(cp.getter, "getProperty");
|
||||
}
|
||||
|
||||
@Test(description = "convert a complex parameter")
|
||||
|
@ -42,7 +42,7 @@ public class Cat extends Animal {
|
||||
* @return declawed
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
public Boolean getDeclawed() {
|
||||
public Boolean isDeclawed() {
|
||||
return declawed;
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,7 @@ public class Order {
|
||||
* @return complete
|
||||
**/
|
||||
@ApiModelProperty(value = "")
|
||||
public Boolean getComplete() {
|
||||
public Boolean isComplete() {
|
||||
return complete;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user