[PHP] Fix default values in derived classes

Fixes flaw in 9f40a82310b36764e5d8ee055983c888ac4d88a6.
This commit is contained in:
Arne Jørgensen 2016-05-06 23:36:49 +02:00
parent 34ec63e908
commit f5a802d9af
3 changed files with 33 additions and 17 deletions

View File

@ -36,6 +36,7 @@
namespace {{modelPackage}};
use \ArrayAccess;
/**
* {{classname}} Class Doc Comment
*
@ -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}}

View File

@ -842,6 +842,9 @@ definitions:
properties:
className:
type: string
color:
type: string
default: 'red'
AnimalFarm:
type: array
items:

View File

@ -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());
}
}
?>