diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index f19787f50b..6f292e854a 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -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}} diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 3af1064f71..240f32eca4 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -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()); + } + } ?>