Skip to content

[Validator] Add warning about closure not being cachable #15172

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

Merged
merged 1 commit into from
Mar 31, 2021

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Mar 31, 2021

Follow up from symfony/symfony#40645

The annotation cache will not work with Closure.

If I understand correctly, it is not possible to have external dependencies in your custom validator AND get your annotation cached. You have to chose one or the other.

@Nyholm
Copy link
Member Author

Nyholm commented Mar 31, 2021

The alternative is to use Yaml, XML or PHP.

@javiereguiluz javiereguiluz force-pushed the validator-callback-cache branch from 196692f to ebddb78 Compare March 31, 2021 11:15
@javiereguiluz javiereguiluz merged commit c09eacc into symfony:4.4 Mar 31, 2021
@javiereguiluz
Copy link
Member

A good addition! Thanks Tobias.

nicolas-grekas added a commit to symfony/symfony that referenced this pull request Apr 1, 2021
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Dont store cache misses on warmup

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38694
| License       | MIT
| Doc PR        | symfony/symfony-docs#15172

When we are warming the annotation cache, we are reading all annotation into an `ArrayAdapter`. When we are done we move the values to a `PhpArrayAdapter` to store them in a file.

@Seldaek [found out](#38694 (comment)) that when you are using a custom constraint with a `Symfony\Component\Validator\Constraints\Callback`, there is a strange error like:

> Can use "yield from" only with arrays and Traversables

That is because the `Closure` in the `Symfony\Component\Validator\Constraints\Callback` cannot be serialised and saved to cache. But since the `ArrayAdapter` is also [storing misses as null](#35362), the null values are understood as real values.

When all values are moved to the `PhpArrayAdapter` and we ask the cache for a value (via Doctrine's `CacheProvider`), it will return `null` as a value instead of `false` as a cache miss. And `null` is not something one could "yield from".

Commits
-------

27a22b3 [FrameworkBundle] Dont store cache misses on warmup
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Apr 1, 2021
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Dont store cache misses on warmup

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #38694
| License       | MIT
| Doc PR        | symfony/symfony-docs#15172

When we are warming the annotation cache, we are reading all annotation into an `ArrayAdapter`. When we are done we move the values to a `PhpArrayAdapter` to store them in a file.

@Seldaek [found out](symfony/symfony#38694 (comment)) that when you are using a custom constraint with a `Symfony\Component\Validator\Constraints\Callback`, there is a strange error like:

> Can use "yield from" only with arrays and Traversables

That is because the `Closure` in the `Symfony\Component\Validator\Constraints\Callback` cannot be serialised and saved to cache. But since the `ArrayAdapter` is also [storing misses as null](symfony/symfony#35362), the null values are understood as real values.

When all values are moved to the `PhpArrayAdapter` and we ask the cache for a value (via Doctrine's `CacheProvider`), it will return `null` as a value instead of `false` as a cache miss. And `null` is not something one could "yield from".

Commits
-------

27a22b34af [FrameworkBundle] Dont store cache misses on warmup
@Nyholm Nyholm deleted the validator-callback-cache branch April 6, 2021 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants