More fine-grained fix

Instead of just ignoring default values in the generated
model constructors, we modify the generator code in Java.
The template checks for null before outputting a default value.
This commit is contained in:
delenius
2016-02-16 09:40:29 -08:00
parent eec806b54e
commit 079addb6db
15 changed files with 73 additions and 260 deletions

View File

@@ -17,9 +17,17 @@ import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.DoubleProperty;
import io.swagger.models.properties.FloatProperty;
import io.swagger.models.properties.IntegerProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
@@ -55,7 +63,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
protected String localVariablePrefix = "";
protected boolean usePromises = false;
protected boolean omitModelMethods = false;
public JavascriptClientCodegen() {
super();
outputFolder = "generated-code/js";
@@ -64,7 +72,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
templateDir = "Javascript";
apiPackage = "api";
modelPackage = "model";
// reference: http://www.w3schools.com/js/js_reserved.asp
reservedWords = new HashSet<String>(
Arrays.asList(
@@ -326,15 +334,43 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
@Override
public String toDefaultValue(Property p) {
if (p instanceof ArrayProperty) {
return "[]";
} else if (p instanceof MapProperty) {
return "{}";
} else if (p instanceof RefProperty) {
return "new " + getTypeDeclaration(p) + "()";
if (p instanceof StringProperty) {
StringProperty dp = (StringProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault();
}
} else if (p instanceof BooleanProperty) {
BooleanProperty dp = (BooleanProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof DateProperty) {
// TODO
} else if (p instanceof DateTimeProperty) {
// TODO
} else if (p instanceof DoubleProperty) {
DoubleProperty dp = (DoubleProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof FloatProperty) {
FloatProperty dp = (FloatProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof IntegerProperty) {
IntegerProperty dp = (IntegerProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
} else if (p instanceof LongProperty) {
LongProperty dp = (LongProperty) p;
if (dp.getDefault() != null) {
return dp.getDefault().toString();
}
}
return super.toDefaultValue(p);
return null;
}
@Override

View File

@@ -19,16 +19,23 @@
* {{description}}
**/{{/description}}
var {{classname}} = function {{classname}}({{#mandatory}}{{this}}{{^-last}}, {{/-last}}{{/mandatory}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}}
{{#vars}}
{{#vars}}{{#required}}
/**{{#description}}
* {{{description}}}{{/description}}
* datatype: {{{datatypeWithEnum}}}{{#required}}
* required{{/required}}{{#minimum}}
* datatype: {{{datatypeWithEnum}}}
* required {{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
this['{{baseName}}'] = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}};
{{/vars}}
this['{{baseName}}'] = {{name}};{{/required}}{{^required}}{{#defaultValue}}
/**{{#description}}
* {{{description}}}{{/description}}
* datatype: {{{datatypeWithEnum}}}
* required {{#minimum}}
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
* maximum: {{maximum}}{{/maximum}}
**/
this['{{baseName}}'] = {{{defaultValue}}};{{/defaultValue}}{{/required}}{{/vars}}
};
{{classname}}.constructFromObject = function(data) {