-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Validation cache duplicates constraints #23544
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
Comments
I cannot reproduce your problem using Symfony |
@dmaicher I'll try to reproduce with your version shortly |
@dmaicher I was wrong. Affected versions start on 3.2.2. I've updated the top message. |
Ok indeed bug confirmed using |
Ok after some debugging I found the problem. It only happens when using the cache warmup because in that case the Working on a fix 😉 |
@ossinkine I proposed a fix here: #23558 Can you try it to confirm it works for you? |
@dmaicher Yes, it works for me! Great work! 👍 |
…g ArrayAdapter (dmaicher) This PR was squashed before being merged into the 3.2 branch (closes #23558). Discussion ---------- [FrameworkBundle] fix ValidatorCacheWarmer: use serializing ArrayAdapter | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23544 | License | MIT | Doc PR | - The `ValidatorCacheWarmer` was using an `ArrayAdapter` with `$storeSerialized=false`. This is a problem as inside the `LazyLoadingMetadataFactory` the metaData objects are mutated (parent class constraints are merged into it) after they have been written into the cache. So this means that currently when warming up the validator cache actually the merged metaData version is finally taken from the `ArrayAdapter` and written into the `PhpFilesAdapter`. Which then caused some duplicate constraints as the parent constraints are merged again after fetching from the cache inside `LazyLoadingMetadataFactory`. This fix makes sure we serialize objects into the `ArrayAdapter`. Writing a test case for this does not seem easy to me. Any ideas? EDIT: Maybe its even safer to just clone the object when writing it into the cache? ```diff diff --git a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php index 79ad1f2..88eaf33 100644 --- a/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php +++ b/src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php @@ -117,7 +117,7 @@ class LazyLoadingMetadataFactory implements MetadataFactoryInterface } if (null !== $this->cache) { - $this->cache->write($metadata); + $this->cache->write(clone $metadata); } ``` Opinions? Commits ------- c0556cb [FrameworkBundle] fix ValidatorCacheWarmer: use serializing ArrayAdapter
Uh oh!
There was an error while loading. Please reload this page.
Validation caching merges constraints for class and all of its parents. It's wrong behaviour, because the class already contains constraints from parent classes. As a result we have duplicated constraint.
The way to reproduce
Create two models, the second one extends the first one:
Create validation config, add constraint to the first class, the second class should be defined (empty or with some constraints)
And validation of new
Bar
object will generates two duplicated violations:Note: this code should be executed in prod environment, because test environment doesn't use validation cache by default.
The text was updated successfully, but these errors were encountered: