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 invalid enum value in array of enums #11799

Merged
merged 1 commit into from
Jan 22, 2025
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
14 changes: 11 additions & 3 deletions src/Internal/Hydration/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function array_search;
use function count;
use function in_array;
use function is_array;
use function key;
use function reset;
use function sprintf;
Expand Down Expand Up @@ -143,14 +144,21 @@ protected function hydrateRowData(array $row, array &$result)
}

if ($value !== null && isset($cacheKeyInfo['enumType'])) {
$originalValue = $value;
$originalValue = $currentValue = $value;
try {
$value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']);
if (! is_array($originalValue)) {
$value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']);
} else {
$value = [];
foreach ($originalValue as $i => $currentValue) {
$value[$i] = $this->buildEnum($currentValue, $cacheKeyInfo['enumType']);
}
}
} catch (ValueError $e) {
throw MappingException::invalidEnumValue(
$entityName,
$cacheKeyInfo['fieldName'],
(string) $originalValue,
(string) $currentValue,
$cacheKeyInfo['enumType'],
$e
);
Expand Down
27 changes: 27 additions & 0 deletions tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\Internal\Hydration\HydrationException;
use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Tests\DbalTypes\GH8565EmployeePayloadType;
use Doctrine\Tests\DbalTypes\GH8565ManagerPayloadType;
use Doctrine\Tests\Mocks\ArrayResultFactory;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\Company\CompanyPerson;
use Doctrine\Tests\Models\Enums\Scale;
use Doctrine\Tests\Models\Enums\Unit;
use Doctrine\Tests\Models\GH8565\GH8565Employee;
use Doctrine\Tests\Models\GH8565\GH8565Manager;
use Doctrine\Tests\Models\GH8565\GH8565Person;
Expand Down Expand Up @@ -155,4 +158,28 @@ public function testWrongValuesShouldNotBeConvertedToPhpValue(): void
$result = $hydrator->hydrateAll($stmt, $rsm);
self::assertEquals($result[0], $expectedEntity);
}

/**
* @requires PHP 8.1
*/
public function testNotListedValueInEnumArray(): void
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Case "unknown_case" is not listed in enum "Doctrine\Tests\Models\Enums\Unit"');
$rsm = new ResultSetMapping();
$rsm->addEntityResult(Scale::class, 's');
$rsm->addFieldResult('s', 's__id', 'id');
$rsm->addFieldResult('s', 's__supported_units', 'supportedUnits');
$rsm->addEnumResult('s__supported_units', Unit::class);
$resultSet = [
[
's__id' => 1,
's__supported_units' => 'g,m,unknown_case',
],
];

$stmt = ArrayResultFactory::createFromArray($resultSet);
$hydrator = new SimpleObjectHydrator($this->entityManager);
$hydrator->hydrateAll($stmt, $rsm);
}
}