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
commit e95e0c88cb
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) {

View File

@ -25,7 +25,7 @@ function {{classname}}() {
}
{{/required}}{{/allParams}}
// create path and map variables
var {{localVariablePrefix}}path = '{{basePath}}' + self.replaceAll(self.replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}}
var {{localVariablePrefix}}path = '{{basePath}}' + replaceAll(replaceAll("{{{path}}}", "\\{format\\}","json"){{#pathParams}}
, "\\{" + "{{baseName}}" + "\\}", {{localVariablePrefix}}apiClient.escapeString({{{paramName}}}.toString())){{/pathParams}});
var queryParams = {};
@ -42,7 +42,7 @@ function {{classname}}() {
{{localVariablePrefix}}formParams.put("{{baseName}}", {{paramName}});
{{/formParams}}
path += self.createQueryString(queryParams);
path += createQueryString(queryParams);
if (console) {
console.log('path: ' + path);
@ -71,7 +71,10 @@ function {{classname}}() {
/**
* @returns {{{returnType}}}
*/
var myResponse = response;
{{#returnTypeIsPrimitive}} var myResponse = response;{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}} var myResponse = new {{{returnType}}}();
myResponse.constructFromObject(response);{{/returnTypeIsPrimitive}}
callback(myResponse, textStatus, jqXHR);
});
@ -86,28 +89,28 @@ function {{classname}}() {
{{/operations}}
self.replaceAll = function (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
function replaceAll (haystack, needle, replace) {
var result= haystack;
if (needle !=null && replace!=null) {
result= haystack.replace(new RegExp(needle, 'g'), replace);
}
return result;
}
return result;
}
self.createQueryString = function (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
function createQueryString (queryParams) {
var queryString ='';
var i = 0;
for (var queryParamName in queryParams) {
if (i==0) {
queryString += '?' ;
} else {
queryString += '&' ;
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
}
queryString += queryParamName + '=' + encodeURIComponent(queryParams[queryParamName]);
i++;
return queryString;
}
return queryString;
}
}

View File

@ -5,16 +5,22 @@
* {{description}}
**/{{/description}}
function {{classname}}() { {{#parent}}/* extends {{{parent}}}*/{{/parent}}
var self = this;
{{#vars}}
/**{{#description}}
* {{{description}}}{{/description}}
* datatype: {{{datatypeWithEnum}}}{{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
self.{{name}} = {{{defaultValue}}};
{{/vars}}
var self = this;
{{#vars}}
/**{{#description}}
* {{{description}}}{{/description}}
* datatype: {{{datatypeWithEnum}}}{{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
self.{{name}} = {{{defaultValue}}};
{{/vars}}
self.constructFromObject = function(data) {
{{#vars}}
self.{{name}} = {{{defaultValueWithParam}}}
{{/vars}}
}
{{#vars}}
/**{{#description}}