diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index 0ccf65862e7..3844943005d 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -36,6 +36,7 @@ namespace {{modelPackage}}; use \ArrayAccess; + /** * {{classname}} Class Doc Comment * @@ -62,7 +63,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple {{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - + static function swaggerTypes() { return self::$swaggerTypes{{#parentSchema}} + parent::swaggerTypes(){{/parentSchema}}; } @@ -75,7 +76,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple {{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - + static function attributeMap() { return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap; } @@ -88,7 +89,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple {{#vars}}'{{name}}' => '{{setter}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - + static function setters() { return {{#parentSchema}}parent::setters() + {{/parentSchema}}self::$setters; } @@ -126,13 +127,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple * Associative array for storing property values * @var mixed[] */ - protected $container = array({{#vars}} - /** - * $container['{{{name}}}']{{#description}} {{{description}}}{{/description}} - * @var {{datatype}} - */ - '{{{name}}}' => {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}, - {{/vars}}); + protected $container = array(); /** * Constructor @@ -140,18 +135,22 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple */ public function __construct(array $data = null) { - {{#parentSchema}}parent::__construct($data);{{/parentSchema}} - {{#discriminator}}// Initialize discriminator property with the model name. + {{#parentSchema}} + parent::__construct($data); + + {{/parentSchema}} + {{#vars}} + $this->container['{{name}}'] = isset($data['{{name}}']) ? $data['{{name}}'] : {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}; + {{/vars}} + {{#discriminator}} + + // Initialize discriminator property with the model name. $discrimintor = array_search('{{discriminator}}', self::$attributeMap); $this->container[$discrimintor] = static::$swaggerModelName; {{/discriminator}} - - if ($data != null) { - {{#vars}}$this->container['{{name}}'] = $data['{{name}}'];{{#hasMore}} - {{/hasMore}}{{/vars}} - } } {{#vars}} + /** * Gets {{name}} * @return {{datatype}} diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index 068fb8054d8..1245e50f3b5 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -842,6 +842,9 @@ definitions: properties: className: type: string + color: + type: string + default: 'red' AnimalFarm: type: array items: diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 0695eb9d981..9feabf142cf 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -450,6 +450,20 @@ class PetApiTest extends \PHPUnit_Framework_TestCase } } + // test if default values works + public function testDefaultValues() + { + // add some animals to the farm to make sure the ArrayAccess + // interface works + $dog = new Swagger\Client\Model\Dog(); + $animal = new Swagger\Client\Model\Animal(); + + // assert we can look up the animals in the farm by array + // indices (let's try a random order) + $this->assertSame('red', $dog->getColor()); + $this->assertSame('red', $animal->getColor()); + } + } ?>