forked from loafle/openapi-generator-original
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
@ -14,13 +14,13 @@ import com.samskivert.mustache.Mustache.Compiler;
|
|||||||
|
|
||||||
public interface CodegenConfig {
|
public interface CodegenConfig {
|
||||||
CodegenType getTag();
|
CodegenType getTag();
|
||||||
|
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
String getHelp();
|
String getHelp();
|
||||||
|
|
||||||
Map<String, Object> additionalProperties();
|
Map<String, Object> additionalProperties();
|
||||||
|
|
||||||
Map<String, Object> vendorExtensions();
|
Map<String, Object> vendorExtensions();
|
||||||
|
|
||||||
String testPackage();
|
String testPackage();
|
||||||
@ -92,7 +92,7 @@ public interface CodegenConfig {
|
|||||||
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
|
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, Swagger swagger);
|
||||||
|
|
||||||
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
|
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
|
||||||
|
|
||||||
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);
|
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);
|
||||||
@ -118,7 +118,7 @@ public interface CodegenConfig {
|
|||||||
Map<String, String> modelDocTemplateFiles();
|
Map<String, String> modelDocTemplateFiles();
|
||||||
|
|
||||||
Set<String> languageSpecificPrimitives();
|
Set<String> languageSpecificPrimitives();
|
||||||
|
|
||||||
Map<String, String> reservedWordsMappings();
|
Map<String, String> reservedWordsMappings();
|
||||||
|
|
||||||
void preprocessSwagger(Swagger swagger);
|
void preprocessSwagger(Swagger swagger);
|
||||||
@ -136,11 +136,11 @@ public interface CodegenConfig {
|
|||||||
String toApiTestFilename(String name);
|
String toApiTestFilename(String name);
|
||||||
|
|
||||||
String toModelTestFilename(String name);
|
String toModelTestFilename(String name);
|
||||||
|
|
||||||
String toApiDocFilename(String name);
|
String toApiDocFilename(String name);
|
||||||
|
|
||||||
String toModelDocFilename(String name);
|
String toModelDocFilename(String name);
|
||||||
|
|
||||||
String toModelImport(String name);
|
String toModelImport(String name);
|
||||||
|
|
||||||
String toApiImport(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);
|
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> postProcessAllModels(Map<String, Object> objs);
|
||||||
|
|
||||||
Map<String, Object> postProcessModels(Map<String, Object> objs);
|
Map<String, Object> postProcessModels(Map<String, Object> objs);
|
||||||
|
|
||||||
Map<String, Object> postProcessOperations(Map<String, Object> objs);
|
Map<String, Object> postProcessOperations(Map<String, Object> objs);
|
||||||
@ -207,4 +207,11 @@ public interface CodegenConfig {
|
|||||||
void setIgnoreFilePathOverride(String ignoreFileOverride);
|
void setIgnoreFilePathOverride(String ignoreFileOverride);
|
||||||
|
|
||||||
String getIgnoreFilePathOverride();
|
String getIgnoreFilePathOverride();
|
||||||
|
|
||||||
|
String toBooleanGetter(String name);
|
||||||
|
|
||||||
|
String toSetter(String name);
|
||||||
|
|
||||||
|
String toGetter(String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1230,6 +1230,36 @@ public class DefaultCodegen {
|
|||||||
return name;
|
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"
|
* Output the API (class) name (capitalized) ending with "Api"
|
||||||
* Return DefaultApi if name is empty
|
* Return DefaultApi if name is empty
|
||||||
@ -1518,8 +1548,8 @@ public class DefaultCodegen {
|
|||||||
property.description = escapeText(p.getDescription());
|
property.description = escapeText(p.getDescription());
|
||||||
property.unescapedDescription = p.getDescription();
|
property.unescapedDescription = p.getDescription();
|
||||||
property.title = p.getTitle();
|
property.title = p.getTitle();
|
||||||
property.getter = "get" + getterAndSetterCapitalize(name);
|
property.getter = toGetter(name);
|
||||||
property.setter = "set" + getterAndSetterCapitalize(name);
|
property.setter = toSetter(name);
|
||||||
String example = toExampleValue(p);
|
String example = toExampleValue(p);
|
||||||
if(!"null".equals(example)) {
|
if(!"null".equals(example)) {
|
||||||
property.example = example;
|
property.example = example;
|
||||||
@ -1660,7 +1690,7 @@ public class DefaultCodegen {
|
|||||||
}
|
}
|
||||||
if (p instanceof BooleanProperty) {
|
if (p instanceof BooleanProperty) {
|
||||||
property.isBoolean = true;
|
property.isBoolean = true;
|
||||||
property.getter = "is" + getterAndSetterCapitalize(name);
|
property.getter = toBooleanGetter(name);
|
||||||
}
|
}
|
||||||
if (p instanceof BinaryProperty) {
|
if (p instanceof BinaryProperty) {
|
||||||
property.isBinary = true;
|
property.isBinary = true;
|
||||||
|
@ -1228,4 +1228,14 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
|||||||
return camelize(sanitizeName(tag));
|
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.Xml;
|
||||||
import io.swagger.models.parameters.QueryParameter;
|
import io.swagger.models.parameters.QueryParameter;
|
||||||
import io.swagger.models.properties.ArrayProperty;
|
import io.swagger.models.properties.ArrayProperty;
|
||||||
|
import io.swagger.models.properties.BooleanProperty;
|
||||||
import io.swagger.models.properties.ByteArrayProperty;
|
import io.swagger.models.properties.ByteArrayProperty;
|
||||||
import io.swagger.models.properties.DateTimeProperty;
|
import io.swagger.models.properties.DateTimeProperty;
|
||||||
import io.swagger.models.properties.DecimalProperty;
|
import io.swagger.models.properties.DecimalProperty;
|
||||||
@ -653,4 +654,20 @@ public class JavaModelTest {
|
|||||||
Assert.assertEquals(property3.xmlPrefix, "my");
|
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.assertEquals(cp.baseType, "boolean");
|
||||||
Assert.assertTrue(cp.isNotContainer);
|
Assert.assertTrue(cp.isNotContainer);
|
||||||
Assert.assertTrue(cp.isBoolean);
|
Assert.assertTrue(cp.isBoolean);
|
||||||
Assert.assertEquals(cp.getter, "isProperty");
|
Assert.assertEquals(cp.getter, "getProperty");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(description = "convert a complex parameter")
|
@Test(description = "convert a complex parameter")
|
||||||
|
@ -42,7 +42,7 @@ public class Cat extends Animal {
|
|||||||
* @return declawed
|
* @return declawed
|
||||||
**/
|
**/
|
||||||
@ApiModelProperty(value = "")
|
@ApiModelProperty(value = "")
|
||||||
public Boolean getDeclawed() {
|
public Boolean isDeclawed() {
|
||||||
return declawed;
|
return declawed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +196,7 @@ public class Order {
|
|||||||
* @return complete
|
* @return complete
|
||||||
**/
|
**/
|
||||||
@ApiModelProperty(value = "")
|
@ApiModelProperty(value = "")
|
||||||
public Boolean getComplete() {
|
public Boolean isComplete() {
|
||||||
return complete;
|
return complete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user