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

Exception when persisting class unlisted in disciminator map (Issue 867) #1887

Merged
merged 3 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 34 additions & 14 deletions lib/Doctrine/ODM/MongoDB/DocumentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,27 +734,47 @@ public function createReference(object $document, array $referenceMapping)
* class that is not defined in the discriminator map may only have a
* discriminator field and no value, so default to the full class name.
*/
if (isset($class->discriminatorField)) {
$reference[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
$discriminatorField = null;
watari marked this conversation as resolved.
Show resolved Hide resolved
$discriminatorValue = null;
if (isset($referenceMapping['discriminatorField'])) {
$discriminatorField = $referenceMapping['discriminatorField'];
if (isset($referenceMapping['discriminatorMap'])) {
$pos = array_search($class->name, $referenceMapping['discriminatorMap']);
if ($pos !== false) {
$discriminatorValue = $pos;
}
} else {
$discriminatorValue = $class->name;
}
} else {
$discriminatorField = $class->discriminatorField;
$discriminatorValue = $class->discriminatorValue;
}

if (isset($discriminatorField)) {
watari marked this conversation as resolved.
Show resolved Hide resolved
if ($discriminatorValue === null) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
$reference[$discriminatorField] = $discriminatorValue;

/* Add a discriminator value if the referenced document is not mapped
* explicitly to a targetDocument class.
*/
if (! isset($referenceMapping['targetDocument'])) {
} elseif (! isset($referenceMapping['targetDocument'])) {
$discriminatorField = $referenceMapping['discriminatorField'];
$discriminatorValue = isset($referenceMapping['discriminatorMap'])
? array_search($class->name, $referenceMapping['discriminatorMap'])
: $class->name;

/* If the discriminator value was not found in the map, use the full
* class name. In the future, it may be preferable to throw an
* exception here (perhaps based on some strictness option).
*
* @see PersistenceBuilder::prepareEmbeddedDocumentValue()
*/
if ($discriminatorValue === false) {

$discriminatorMap = null;
if (isset($referenceMapping['discriminatorMap'])) {
$discriminatorMap = $referenceMapping['discriminatorMap'];
}
if (! isset($discriminatorMap)) {
watari marked this conversation as resolved.
Show resolved Hide resolved
$discriminatorValue = $class->name;
} else {
$discriminatorValue = array_search($class->name, $discriminatorMap);

if ($discriminatorValue === false) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
}

$reference[$discriminatorField] = $discriminatorValue;
Expand Down
23 changes: 23 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function count;
use function get_class;
use function in_array;
use function interface_exists;
use function is_array;
use function is_string;
use function is_subclass_of;
Expand Down Expand Up @@ -1902,6 +1903,28 @@ public function mapField(array $mapping) : array
$mapping['discriminatorField'] = self::DEFAULT_DISCRIMINATOR_FIELD;
}
watari marked this conversation as resolved.
Show resolved Hide resolved

if (isset($mapping['targetDocument']) && ! class_exists($mapping['targetDocument']) && ! interface_exists($mapping['targetDocument'])) {
watari marked this conversation as resolved.
Show resolved Hide resolved
$fullClassName = $this->reflClass->getNamespaceName() . '\\' . $mapping['targetDocument'];
if (! class_exists($fullClassName) && ! interface_exists($fullClassName)) {
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
throw MappingException::nonExistingClass($mapping['targetDocument']);
}

$mapping['targetDocument'] = $fullClassName;
}

if (isset($mapping['discriminatorMap'])) {
foreach ($mapping['discriminatorMap'] as $value => $targetDocument) {
if (class_exists($targetDocument) || interface_exists($targetDocument)) {
continue;
}
$fullClassName = $this->reflClass->getNamespaceName() . '\\' . $targetDocument;
watari marked this conversation as resolved.
Show resolved Hide resolved
if (! class_exists($fullClassName) && ! interface_exists($fullClassName)) {
throw MappingException::nonExistingClass($fullClassName);
}
$mapping['discriminatorMap'][$value] = $fullClassName;
}
}

if (isset($mapping['version'])) {
$mapping['notSaved'] = true;
$this->setVersionMapping($mapping);
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public static function invalidClassInDiscriminatorMap(string $className, string
return new self(sprintf("Document class '%s' used in the discriminator map of class '%s' does not exist.", $className, $owningClass));
}

public static function unlistedClassInDiscriminatorMap(string $className) : self
{
return new self(sprintf('Document class "%s" is unlisted in the discriminator map.', $className));
}

public static function invalidDiscriminatorValue(string $value, string $owningClass) : self
{
return new self(sprintf("Discriminator value '%s' used in the declaration of class '%s' does not exist.", $value, $owningClass));
Expand Down
43 changes: 29 additions & 14 deletions lib/Doctrine/ODM/MongoDB/Persisters/PersistenceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\ODM\MongoDB\UnitOfWork;
Expand Down Expand Up @@ -96,7 +97,13 @@ public function prepareInsertData($document)

// add discriminator if the class has one
if (isset($class->discriminatorField)) {
$insertData[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
if ($class->discriminatorValue === null) {
alcaeus marked this conversation as resolved.
Show resolved Hide resolved
if (! empty($class->discriminatorMap)) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
$class->discriminatorValue = $class->name;
}
$insertData[$class->discriminatorField] = $class->discriminatorValue;
}

return $insertData;
Expand Down Expand Up @@ -277,7 +284,13 @@ public function prepareUpsertData($document)

// add discriminator if the class has one
if (isset($class->discriminatorField)) {
$updateData['$set'][$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
if ($class->discriminatorValue === null) {
if (! empty($class->discriminatorMap)) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
$class->discriminatorValue = $class->name;
}
$updateData['$set'][$class->discriminatorField] = $class->discriminatorValue;
}

return $updateData;
Expand Down Expand Up @@ -379,17 +392,13 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo
*/
if (! isset($embeddedMapping['targetDocument'])) {
$discriminatorField = $embeddedMapping['discriminatorField'];
$discriminatorValue = isset($embeddedMapping['discriminatorMap'])
? array_search($class->name, $embeddedMapping['discriminatorMap'])
: $class->name;

/* If the discriminator value was not found in the map, use the full
* class name. In the future, it may be preferable to throw an
* exception here (perhaps based on some strictness option).
*
* @see DocumentManager::createDBRef()
*/
if ($discriminatorValue === false) {
if (! empty($embeddedMapping['discriminatorMap'])) {
$discriminatorValue = array_search($class->name, $embeddedMapping['discriminatorMap']);

if ($discriminatorValue === false) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
} else {
$discriminatorValue = $class->name;
}

Expand All @@ -401,7 +410,13 @@ public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDo
* discriminator field and no value, so default to the full class name.
*/
if (isset($class->discriminatorField)) {
$embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
if ($class->discriminatorValue === null) {
if (! empty($class->discriminatorMap)) {
throw MappingException::unlistedClassInDiscriminatorMap($class->name);
}
$class->discriminatorValue = $class->name;
}
$embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue;
}

// Ensure empty embedded documents are stored as BSON objects
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public function testEmbeddedDocumentWithDifferentFieldNameAnnotation()
$embedded = new EmbeddedDocumentWithId();
$embedded->id = (string) new ObjectId();

$firstEmbedded = new EmbedDocumentWithAnotherEmbed();
$firstEmbedded = new EmbeddedDocumentWithAnotherEmbedded();
$firstEmbedded->embed = $embedded;

$secondEmbedded = clone $firstEmbedded;
Expand Down Expand Up @@ -668,7 +668,7 @@ class ChangeEmbeddedWithNameAnnotationTest
/**
* @ODM\EmbeddedDocument
*/
class EmbedDocumentWithAnotherEmbed
class EmbeddedDocumentWithAnotherEmbedded
{
/** @ODM\EmbedOne(targetDocument=EmbeddedDocumentWithId::class, name="m_id") */
public $embed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class GH267User
/** @ODM\Field(type="string") */
protected $name;

/** @ODM\ReferenceOne(name="company", targetDocument=GH267Company::class, discriminatorMap={"seller"="SellerCompany", "buyer"="BuyerCompany"}, inversedBy="users") */
/** @ODM\ReferenceOne(name="company", targetDocument=GH267Company::class, discriminatorMap={"seller"="GH267SellerCompany", "buyer"="GH267BuyerCompany"}, inversedBy="users") */
watari marked this conversation as resolved.
Show resolved Hide resolved
protected $company;

public function __construct($name)
Expand Down
Loading