Merge pull request #1680 from wing328/php_default_value

[PHP] add default value to PHP model
This commit is contained in:
wing328 2015-12-09 15:39:34 +08:00
commit 36dfa15f93
3 changed files with 53 additions and 13 deletions

View File

@ -6,10 +6,7 @@ import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.*;
import java.io.File;
import java.util.Arrays;
@ -276,10 +273,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(type);
}
public String toDefaultValue(Property p) {
return "null";
}
public void setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
}
@ -377,4 +370,51 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return camelize(sanitizeName(operationId), true);
}
/**
* Return the default value of the property
*
* @param p Swagger property object
* @return string presentation of the default value of the property
*/
@Override
public String toDefaultValue(Property p) {
if (p instanceof StringProperty) {
StringProperty dp = (StringProperty) p;
if (dp.getDefault() != null) {
return "'" + dp.getDefault().toString() + "'";
}
} 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 null;
}
}

View File

@ -89,7 +89,7 @@ class {{classname}} implements ArrayAccess
* ${{name}} {{#description}}{{{description}}}{{/description}}
* @var {{datatype}}
*/
protected ${{name}};
protected ${{name}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}};
{{/vars}}
/**

View File

@ -42,7 +42,7 @@ public class PhpModelTest {
Assert.assertEquals(property1.baseName, "id");
Assert.assertEquals(property1.datatype, "int");
Assert.assertEquals(property1.name, "id");
Assert.assertEquals(property1.defaultValue, "null");
Assert.assertEquals(property1.defaultValue, null);
Assert.assertEquals(property1.baseType, "int");
Assert.assertTrue(property1.hasMore);
Assert.assertTrue(property1.required);
@ -53,7 +53,7 @@ public class PhpModelTest {
Assert.assertEquals(property2.baseName, "name");
Assert.assertEquals(property2.datatype, "string");
Assert.assertEquals(property2.name, "name");
Assert.assertEquals(property2.defaultValue, "null");
Assert.assertEquals(property2.defaultValue, null);
Assert.assertEquals(property2.baseType, "string");
Assert.assertTrue(property2.hasMore);
Assert.assertTrue(property2.required);
@ -65,7 +65,7 @@ public class PhpModelTest {
Assert.assertEquals(property3.complexType, "\\DateTime");
Assert.assertEquals(property3.datatype, "\\DateTime");
Assert.assertEquals(property3.name, "created_at");
Assert.assertEquals(property3.defaultValue, "null");
Assert.assertEquals(property3.defaultValue, null);
Assert.assertEquals(property3.baseType, "\\DateTime");
Assert.assertNull(property3.hasMore);
Assert.assertNull(property3.required);
@ -92,7 +92,7 @@ public class PhpModelTest {
Assert.assertEquals(property1.baseName, "id");
Assert.assertEquals(property1.datatype, "int");
Assert.assertEquals(property1.name, "id");
Assert.assertEquals(property1.defaultValue, "null");
Assert.assertEquals(property1.defaultValue, null);
Assert.assertEquals(property1.baseType, "int");
Assert.assertTrue(property1.hasMore);
Assert.assertTrue(property1.required);