mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
Set discriminator property in constructor
When using inheritance let the constructor set the discriminator property to the model name. This avoids redundant and error prone stuff like: ``` $animal = new \Swagger\Client\Model\Dog(); $animal->setClassName('Dog'); ```
This commit is contained in:
parent
c159239d1b
commit
65d677c385
@ -48,6 +48,12 @@ use \ArrayAccess;
|
||||
*/
|
||||
class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayAccess
|
||||
{
|
||||
/**
|
||||
* The original name of the model.
|
||||
* @var string
|
||||
*/
|
||||
static $swaggerModelName = '{{name}}';
|
||||
|
||||
/**
|
||||
* Array of property to type mappings. Used for (de)serialization
|
||||
* @var string[]
|
||||
@ -115,6 +121,11 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
|
||||
public function __construct(array $data = null)
|
||||
{
|
||||
{{#parent}}parent::__construct($data);{{/parent}}
|
||||
{{#discriminator}}// Initialize discriminator property with the model name.
|
||||
$discrimintor = array_search('{{discriminator}}', self::$attributeMap);
|
||||
$this->{$discrimintor} = static::$swaggerModelName;
|
||||
{{/discriminator}}
|
||||
|
||||
if ($data != null) {
|
||||
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
|
||||
{{/hasMore}}{{/vars}}
|
||||
|
@ -402,6 +402,13 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertSame('Dog', $new_dog->getClassName());
|
||||
}
|
||||
|
||||
// test if discriminator is initialized automatically
|
||||
public function testDiscriminatorInitialization()
|
||||
{
|
||||
$new_dog = new Swagger\Client\Model\Dog();
|
||||
$this->assertSame('Dog', $new_dog->getClassName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user