Problem/Motivation
In #2438017-49: Entity reference does not validate auto-created entities it has been found out that the recursive validation is broken in the case where a constraint validator is triggering a new validation execution chain involving the same constraint validator, in which case the execution context of the current constraint validator will be exchanged from the triggered validation chain.
The problem relies in an optimization, which is instantiating the constraint validators only once, which in the case of a recursive validation is wrong.
The Drupal optimization is in \Drupal\Core\Validation\ConstraintValidatorFactory::getInstance().
Symfony offers a similar optimization in \Symfony\Component\Validator\ConstraintValidatorFactory::getInstance()
Proposed resolution
Constraint validators, which could trigger a validation execution should not be shared, but always newly instantiated.
Two possible approaches:
- Introduce
\Symfony\Component\Validator\ConstraintValidatorInterface::isShareable(), which should be implemented by each constraint validator and ones which are triggering validation should returnFALSE. While this solution will keep the optimization, it doesn't necessary prevent from mistakes. - Remove the optimization and don't share the constraint validators.
The second option might increase the used memory, when validating a nested entity structure or a lot of entities in a single request, but is more stable and secure than the first one.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 2935405.patch | 7.23 KB | borisson_ |
Comments
Comment #2
hchonovComment #3
hchonovI've created a kernel test using the entity system as it easier to understand what is happening.
Comment #5
hchonovThis is blocking #2438017: Entity reference does not validate auto-created entities.
Comment #7
borisson_Fixed coding standards, this patch looks good and I like the test coverage, but I think someone with more knowledge about the validation system should RTBC this patch.
Comment #17
smustgrave commentedSo the fix here appears to be around ConstraintValidatorFactory which was already fixed in https://www.drupal.org/project/drupal/issues/3096811 so this can probably be closed as duplicate?
And the blocker tag could then be removed correct?
Comment #18
borisson_Yes, you're right @smustgrave