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:
wing328 2017-07-26 19:57:50 +08:00 committed by GitHub
parent 66f2f70634
commit e807b81471
7 changed files with 77 additions and 13 deletions

View File

@ -14,13 +14,13 @@ import com.samskivert.mustache.Mustache.Compiler;
public interface CodegenConfig {
CodegenType getTag();
String getName();
String getHelp();
Map<String, Object> additionalProperties();
Map<String, Object> vendorExtensions();
String testPackage();
@ -92,7 +92,7 @@ public interface CodegenConfig {
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);
@ -118,7 +118,7 @@ public interface CodegenConfig {
Map<String, String> modelDocTemplateFiles();
Set<String> languageSpecificPrimitives();
Map<String, String> reservedWordsMappings();
void preprocessSwagger(Swagger swagger);
@ -136,11 +136,11 @@ public interface CodegenConfig {
String toApiTestFilename(String name);
String toModelTestFilename(String name);
String toApiDocFilename(String name);
String toModelDocFilename(String name);
String toModelImport(String name);
String toApiImport(String name);
@ -148,7 +148,7 @@ public interface CodegenConfig {
void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations);
Map<String, Object> postProcessAllModels(Map<String, Object> objs);
Map<String, Object> postProcessModels(Map<String, Object> objs);
Map<String, Object> postProcessOperations(Map<String, Object> objs);
@ -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);
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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")

View File

@ -42,7 +42,7 @@ public class Cat extends Animal {
* @return declawed
**/
@ApiModelProperty(value = "")
public Boolean getDeclawed() {
public Boolean isDeclawed() {
return declawed;
}

View File

@ -196,7 +196,7 @@ public class Order {
* @return complete
**/
@ApiModelProperty(value = "")
public Boolean getComplete() {
public Boolean isComplete() {
return complete;
}