- π©πͺGermany donquixote
The Reflection::getParameterClassName() needs to somehow deal with union types.
But what would be a suitable return value in case of union types?A possible backwards compatible solution could be:
- Let Reflection::getParameterClassName() return NULL, if the type is a union or intersection type.
- Optionally, add another parameter to control throwing an exception instead of returning NULL. OR introduce another method like "::demandParameterClassName()",
- Add a new method Reflection::getParameterClassNames() that also supports union types. This one would return NULL for intersection types.
- Alternatively, a new method could receive the type itself as a parameter, so it could also be used with return types.
- All the existing calling code should treat the NULL case as if there is no parameter type at all. Gradually, each piece of calling code could be updated to also support union types.---------
This said: For the example controller, I think it would be better to simply use "Node" or "NodeInterface" as the parameter type, and then within the method call instanceof to work with the more specific bundle classes.
As a workaround, you could also rename the parameter from `$node` to something like `$article_or_post` where you are sure that no entity type exists with this name. - π³π±Netherlands spadxiii
I ran into this with a rest controller myself where it had "string|int" as its parameter.
Added a small change to the if-statement in \Drupal\Component\Utility\Reflection::getParameterClassName:
method_exists($parameter->getType(), 'isBuiltin')
I'm not sure how to handle union types in this case, but this patch allowed me to prevent the error from happening :)
- πΊπ¦Ukraine voleger Ukraine, Rivne
The same error appears when generating a proxy class for a class with union types used in definitions.
php core/scripts/generate-proxy-class.php '\Drupal\Core\Batch\BatchProcessor' "core/lib/Drupal/Core"
.
Affected me working on π Create an interface and initial class for the batch processor service Needs work - Merge request !12419Issue #3315276: Fix fatal for union entity types β (Open) created by acbramley
- π¦πΊAustralia acbramley
Finally getting around to this one, I think for now we just return NULL and don't automate the entity upcasting because I can't think of a good way that you could safely upcast an entity parameter if it's a union type?
Perhaps only if all of them are a subclass of
$entity_type->getClass();
? - πΊπΈUnited States smustgrave
Sorry to be that guy
Can we clean up the issue summary, appears to be missing section with proposed solution