add support for clean response object creation in the API class #1294

This commit is contained in:
Johannes Fiala
2015-12-05 17:44:45 +01:00
5 changed files with 142 additions and 42 deletions

View File

@@ -5,7 +5,7 @@ import java.util.Map;
public class CodegenProperty {
public String baseName, complexType, getter, setter, description, datatype, datatypeWithEnum,
name, min, max, defaultValue, baseType, containerType;
name, min, max, defaultValue, defaultValueWithParam, baseType, containerType;
public String unescapedDescription;

View File

@@ -2,6 +2,7 @@ package io.swagger.codegen;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import io.swagger.codegen.examples.ExampleGenerator;
import io.swagger.models.ArrayModel;
import io.swagger.models.ComposedModel;
@@ -42,11 +43,13 @@ import io.swagger.models.properties.PropertyBuilder.PropertyId;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.Json;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
@@ -574,6 +577,52 @@ public class DefaultCodegen {
return "null";
}
}
/**
* Return the property initialized from a data object
* Useful for initialization with a plain object in Javascript
*
* @param name Name of the property object
* @param p Swagger property object
* @return string presentation of the default value of the property
*/
public String toDefaultValueWithParam(String name, Property p) {
if (p instanceof StringProperty) {
return "data." + name + ";";
} else if (p instanceof BooleanProperty) {
return "data." + name + ";";
} else if (p instanceof DateProperty) {
return "data." + name + ";";
} else if (p instanceof DateTimeProperty) {
return "data." + name + ";";
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return "data." + name + ";";
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return "data." + name + ";";
} else if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return "data." + name + ";";
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
return "data." + name + ";";
} else {
return "data." + name + ";";
}
}
/**
* returns the swagger type for the property
@@ -835,6 +884,8 @@ public class DefaultCodegen {
property.setter = "set" + getterAndSetterCapitalize(name);
property.example = p.getExample();
property.defaultValue = toDefaultValue(p);
property.defaultValueWithParam = toDefaultValueWithParam(name, p);
property.jsonSchema = Json.pretty(p);
property.isReadOnly = p.getReadOnly();

View File

@@ -76,7 +76,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
"Object",
"byte[]")
);
instantiationTypes.put("array", "ArrayList");
instantiationTypes.put("array", "Array");
instantiationTypes.put("map", "HashMap");
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC));
@@ -171,10 +171,10 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
}
additionalProperties.put("fullJavaUtil", fullJavaUtil);
additionalProperties.put("javaUtilPrefix", javaUtilPrefix);
if (fullJavaUtil) {
typeMapping.put("array", "java.util.List");
typeMapping.put("map", "java.util.Map");
*/
//if (fullJavaUtil) {
typeMapping.put("array", "Array");
/*typeMapping.put("map", "java.util.Map");
typeMapping.put("DateTime", "java.util.Date");
typeMapping.remove("List");
importMapping.remove("Date");
@@ -186,9 +186,9 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
importMapping.remove("Set");
importMapping.remove("DateTime");
instantiationTypes.put("array", "java.util.ArrayList");
instantiationTypes.put("map", "java.util.HashMap");
}
instantiationTypes.put("map", "java.util.HashMap");*/
//}
/*
this.sanitizeConfig();
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
@@ -319,7 +319,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">";
return getSwaggerType(p); // TODO: + "/* <" + getTypeDeclaration(inner) + "> */";
} else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
@@ -369,6 +369,46 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
return super.toDefaultValue(p);
}
@Override
public String toDefaultValueWithParam(String name, Property p) {
if (p instanceof ArrayProperty) {
final ArrayProperty ap = (ArrayProperty) p;
final String pattern;
if (fullJavaUtil) {
pattern = "new java.util.ArrayList<%s>()";
} else {
pattern = "new ArrayList<%s>()" ;
}
return String.format(pattern, getTypeDeclaration(ap.getItems()))+ ";";
} else if (p instanceof MapProperty) {
final MapProperty ap = (MapProperty) p;
final String pattern;
if (fullJavaUtil) {
pattern = "new java.util.HashMap<String, %s>()";
} else {
pattern = "new HashMap<String, %s>()";
}
return String.format(pattern, getTypeDeclaration(ap.getAdditionalProperties()))+ ";";
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
return "data." + name + ";";
// added for Javascript
} else if (p instanceof RefProperty) {
RefProperty rp = (RefProperty)p;
System.out.println("rp: " + rp.getName() + rp.getAccess() + rp.getDescription() + rp.getExample() + rp.getFormat() + rp.getSimpleRef() + rp.getTitle() + rp.getType());
return "new " +rp.getSimpleRef() + "(data." + name + ");";
}
System.out.println("property: " + p);
return super.toDefaultValueWithParam(name, p);
}
@Override
public String getSwaggerType(Property p) {