Skip to content

Commit

Permalink
Merge pull request #11799 from HypeMC/enum-array-error
Browse files Browse the repository at this point in the history
Fix invalid enum value in array of enums
  • Loading branch information
greg0ire authored Jan 22, 2025
2 parents fff085b + 9bd7242 commit c2a4932
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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);
}
}

0 comments on commit c2a4932

Please sign in to comment.