Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for failing doctrine object constructor on embeddable class #1031

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Construction/DoctrineObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public function construct(DeserializationVisitorInterface $visitor, ClassMetadat
$identifierList[$name] = $data[$dataName];
}

if (empty($identifierList)) {
// $classMetadataFactory->isTransient() fails on embeddable class with file metadata driver
// https://github.com/doctrine/persistence/issues/37
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

// Entity update, load it from database
$object = $objectManager->find($metadata->name, $identifierList);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<embeddable name="JMS\Serializer\Tests\Fixtures\Doctrine\Embeddable\BlogPostSeo">
<field name="metaTitle" column="meta_title" type="string" nullable="false"/>
</embeddable>
</doctrine-mapping>
67 changes: 61 additions & 6 deletions tests/Serializer/Doctrine/ObjectConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\UnitOfWork;
use Doctrine\ORM\Version as ORMVersion;
use JMS\Serializer\Builder\CallbackDriverFactory;
use JMS\Serializer\Builder\DefaultDriverFactory;
use JMS\Serializer\Construction\DoctrineObjectConstructor;
Expand All @@ -29,6 +31,7 @@
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use JMS\Serializer\Tests\Fixtures\Doctrine\Embeddable\BlogPostSeo;
use JMS\Serializer\Tests\Fixtures\Doctrine\Entity\Author;
use JMS\Serializer\Tests\Fixtures\Doctrine\IdentityFields\Server;
use JMS\Serializer\Tests\Fixtures\DoctrinePHPCR\Author as DoctrinePHPCRAuthor;
Expand Down Expand Up @@ -212,6 +215,40 @@ public function testNamingForIdentifierColumnIsConsidered()
);
}

public function testFallbackOnEmbeddableClassWithXmlDriver()
{
if (ORMVersion::compare('2.5') >= 0) {
$this->markTestSkipped('Not using Doctrine ORM >= 2.5 with Embedded entities');
}

$fallback = $this->getMockBuilder(ObjectConstructorInterface::class)->getMock();
$fallback->expects($this->once())->method('construct');

$connection = $this->createConnection();
$entityManager = $this->createXmlEntityManager($connection);

$this->registry = $registry = new SimpleBaseManagerRegistry(
static function ($id) use ($connection, $entityManager) {
switch ($id) {
case 'default_connection':
return $connection;

case 'default_manager':
return $entityManager;

default:
throw new \RuntimeException(sprintf('Unknown service id "%s".', $id));
}
}
);

$type = ['name' => BlogPostSeo::class, 'params' => []];
$class = new ClassMetadata(BlogPostSeo::class);

$constructor = new DoctrineObjectConstructor($this->registry, $fallback, DoctrineObjectConstructor::ON_MISSING_FALLBACK);
$constructor->construct($this->visitor, $class, ['metaTitle' => 'test'], $type, $this->context);
}

protected function setUp()
{
$this->visitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock();
Expand Down Expand Up @@ -265,20 +302,38 @@ private function createConnection()
]);
}

private function createEntityManager(Connection $con)
private function createEntityManager(Connection $con, ?Configuration $cfg = null)
{
$cfg = new Configuration();
$cfg->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [
__DIR__ . '/../../Fixtures/Doctrine/Entity',
__DIR__ . '/../../Fixtures/Doctrine/IdentityFields',
]));
if (!$cfg) {
$cfg = new Configuration();
$cfg->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader(), [
__DIR__ . '/../../Fixtures/Doctrine/Entity',
__DIR__ . '/../../Fixtures/Doctrine/IdentityFields',
]));
}

$cfg->setAutoGenerateProxyClasses(true);
$cfg->setProxyNamespace('JMS\Serializer\DoctrineProxy');
$cfg->setProxyDir(sys_get_temp_dir() . '/serializer-test-proxies');

return EntityManager::create($con, $cfg);
}

/**
* @param Connection $con
*
* @return EntityManager
*/
private function createXmlEntityManager(Connection $con)
{
$cfg = new Configuration();
$cfg->setMetadataDriverImpl(new XmlDriver([
__DIR__ . '/../../Fixtures/Doctrine/XmlMapping',
]));

return $this->createEntityManager($con, $cfg);
}

/**
* @return SerializerInterface
*/
Expand Down