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

Array to string conversion when invalid value in enum array column #11796

Open
maximecolin opened this issue Jan 21, 2025 · 2 comments
Open

Array to string conversion when invalid value in enum array column #11796

maximecolin opened this issue Jan 21, 2025 · 2 comments
Labels

Comments

@maximecolin
Copy link
Contributor

maximecolin commented Jan 21, 2025

Bug Report

Q A
Version 3.3.1
Previous Version if the bug is a regression not sure, it worked some time ago in 2.x

Summary

When querying an entity having an array of enum containing a value that does not exists in the PHP enum, I got the following error:

Uncaught PHP Exception ErrorException: "Warning: Array to string conversion" at SimpleObjectHydrator.php line 148 {"exception":"[object] (ErrorException(code: 0): Warning: Array to string conversion at /srv/app/back/vendor/doctrine/orm/src/Internal/Hydration/SimpleObjectHydrator.php:148)"}

Current behavior

Using a json column with enumType, if the database contains a value that is not in the enum, so an exception is thrown by AbstractHydrator::buildEnum then converted to another exception in SimpleObjectHydrator. During this conversion, the original value is casted into string. In case of array of enum (json or simple array), $originalValue contains an array of string that can not be casted to string. So we got the "Array to string conversion" error instead of a proper error about a case not listed in the enum.

This case is pretty common, for exemple when a case have been removed from the PHP enum, or when a value that have been added on a branch, when switching to another branch, the value is still in the database but does not exist anymore on the PHP enum.

Expected behavior

A proper exception explaining a case in the database is not listed in the enum like for single enum case columns.

How to reproduce

  1. Create an entity with an array of enum (example bellow)
  2. Add in database an array containing a value not the enum cases
  3. Fetch the entity

I also created a test that reproduce this error : #11795

@greg0ire
Copy link
Member

This was introduced in 9d5ab4c

@HypeMC , can you please take a look at this?

@HypeMC
Copy link
Contributor

HypeMC commented Jan 22, 2025

@greg0ire This seems to be a 3.x exclusive bug, it works fine on 2.x, see #11799. The problem is that 2.x uses the ORM ReflectionEnumProperty, which handles invalid values:

public function setValue(mixed $object, mixed $value = null): void
{
if ($value !== null) {
if (is_array($value)) {
$value = array_map(fn (int|string|BackedEnum $item): BackedEnum => $this->initializeEnumValue($object, $item), $value);
} else {
$value = $this->initializeEnumValue($object, $value);
}
}
$this->originalReflectionProperty->setValue($object, $value);
}
private function initializeEnumValue(object $object, int|string|BackedEnum $value): BackedEnum
{
if ($value instanceof BackedEnum) {
return $value;
}
$enumType = $this->enumType;
try {
return $enumType::from($value);
} catch (ValueError $e) {
throw MappingException::invalidEnumValue(
$object::class,
$this->originalReflectionProperty->name,
(string) $value,
$enumType,
$e,
);
}
}

while 3.x uses EnumReflectionProperty from the persistence repo, which doesn't:

https://github.com/doctrine/persistence/blob/133bb2825572ee59e506c0bb7d6c9658506619a4/src/Persistence/Reflection/EnumReflectionProperty.php#L106-L122

I'm guessing the solution would be to handle this in EnumReflectionProperty. If that's the case, I'll gladly open a PR.

Also, it'd probably be good to merge #11799 to prevent any future bugs in 2.x.

Scratch that, I was wrong. The reproducer wasn't complete, it was missing $rsm->addEnumResult('s__supported_units', Unit::class);. With that line of code, I get the error on 2.x as well. See #11799 for possible fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants