forked from loafle/openapi-generator-original
[PHP] Fix default values in derived classes
Fixes flaw in 9f40a82310b36764e5d8ee055983c888ac4d88a6.
This commit is contained in:
parent
34ec63e908
commit
f5a802d9af
@ -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}}
|
||||
|
@ -842,6 +842,9 @@ definitions:
|
||||
properties:
|
||||
className:
|
||||
type: string
|
||||
color:
|
||||
type: string
|
||||
default: 'red'
|
||||
AnimalFarm:
|
||||
type: array
|
||||
items:
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user