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

fix: Set twig.exception_listener as service parent #5059

Merged
merged 2 commits into from Jan 12, 2023

Conversation

paulandrieux
Copy link
Contributor

Q A
Branch? 2.7
License MIT

On branch 2.7, the service api_platform.error_listener defines exception_listener as parent. https://github.com/api-platform/core/blob/main/src/Symfony/Bundle/Resources/config/api.xml#L170

Twig v4.4 deletes the definition of exception_listener and replaces it by twig.exception_listener https://github.com/symfony/twig-bundle/blob/4.4/DependencyInjection/Compiler/ExceptionListenerPass.php#L43

So, when using api_platform 2.7 and twig 4.4, the following error occurs at cache clear :

In ResolveChildDefinitionsPass.php line 76:
                                                                                                 
  Service "api_platform.error_listener": Parent definition "exception_listener" does not exist.

This PR checks if twig is enabled, and changes the parent of api_platform.error_listener from exception_listener to twig.exception_listener.

I'm not convinced that this is the best condition, nor this is the right place to do this check, any advice ?

@soyuka
Copy link
Member

soyuka commented Oct 7, 2022

Not a huge fan of fixing 2.7 for this as we changed this in 3.0. Can't we fix the twig version on lowest instead ?

@paulandrieux
Copy link
Contributor Author

The nearest lower compliant Twig version would be 4.3, therefore it means that ApiPlatform 2.7 could not run on Symfony 4.4, which is a LTS largely used.
I made a less intrusive fix, only dereferencing the parent exception_listener service that twig is deleting

@paulandrieux
Copy link
Contributor Author

For record, a workaround can be to redifine the api_platform.error_listener service in project scope :

//config/services.yml
services:
[...]
    api_platform.error_listener:
        class: ApiPlatform\Symfony\EventListener\ErrorListener
        arguments:
            $controller: "@api_platform.action.exception"
            $logger: "@logger"
            $debug: "%kernel.debug%"

@aaa2000
Copy link
Contributor

aaa2000 commented Oct 14, 2022

On a web application with an api, I have a response in the application/problem+json format instead of html format for the web application.

For record, a workaround can be to redifine the api_platform.error_listener service in project scope :

//config/services.yml
services:
[...]
    api_platform.error_listener:
        class: ApiPlatform\Symfony\EventListener\ErrorListener
        arguments:
            $controller: "@api_platform.action.exception"
            $logger: "@logger"
            $debug: "%kernel.debug%"

Maybe another workaround is to use an alias

exception_listener: '@twig.exception_listener'

@soyuka
Copy link
Member

soyuka commented Oct 24, 2022

can you fix the tests ?

@stale
Copy link

stale bot commented Jan 3, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 3, 2023
@alexislefebvre
Copy link
Contributor

Dear bot, please wait.

@stale stale bot removed the stale label Jan 3, 2023
@llupa
Copy link
Contributor

llupa commented Jan 11, 2023

Very interested in this PR! @paulandrieux Could you find it in your heart to fix the tests too? 😄

@paulandrieux
Copy link
Contributor Author

@soyuka Tests are now fixed

@soyuka soyuka merged commit ab6822f into api-platform:2.7 Jan 12, 2023
@soyuka
Copy link
Member

soyuka commented Jan 12, 2023

perfect thanks @paulandrieux !

soyuka added a commit that referenced this pull request Jan 14, 2023
* chore: switch from deprecated “loose” email validation mode to “html5” (#5326)

* chore: replace `Request::getContentType` with `Request::getContentTypeFormat` if available (#5325)

* fix: Set twig.exception_listener as service parent (#5059)

* fix(symfony): remove dependency on potentially inexistant `exception_listener` service

* define argument keys on service redefinition

* fix: avoid unneeded use of covariance to keep compatibility with PHP < 7.4 (#5327)

* fix(symfony): http cache wrong metadata argument

* chore: changelog 2.7.6

Co-authored-by: Mathieu <mathieu.lechat@les-tilleuls.coop>
Co-authored-by: Paul Andrieux <paul@appventus.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants