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

[2.7] Class "ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\ConstantScoreFilterExtension" not found #5005

Closed
darthf1 opened this issue Sep 23, 2022 · 14 comments · Fixed by #5283

Comments

@darthf1
Copy link
Contributor

darthf1 commented Sep 23, 2022

When upgrading 2.6.8 to 2.7.0 and running my existing test suite, I get the following error:

Error : Class "ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\ConstantScoreFilterExtension" not found
 /home/www/app/var/cache/test/ContainerOSOLgNS/getApiPlatform_Elasticsearch_RequestBodySearchExtension_ConstantScoreFilterService.php:20
 /home/www/app/var/cache/test/ContainerOSOLgNS/Infrastructure_Shared_Symfony_KernelTestDebugContainer.php:860
 /home/www/app/var/cache/test/ContainerOSOLgNS/getApiPlatform_Elasticsearch_CollectionDataProviderService.php:23
 /home/www/app/vendor/api-platform/core/src/Core/Bridge/Elasticsearch/DataProvider/CollectionDataProvider.php:120
 /home/www/app/src/Infrastructure/Shared/ApiPlatform/DataProvider/SubresourceDataProvider.php:48
 /home/www/app/vendor/api-platform/core/src/Core/Bridge/Symfony/Bundle/DataProvider/TraceableChainSubresourceDataProvider.php:63
 /home/www/app/vendor/api-platform/core/src/Core/DataProvider/OperationDataProviderTrait.php:90
 /home/www/app/vendor/api-platform/core/src/Core/EventListener/ReadListener.php:130
 /home/www/app/vendor/symfony/event-dispatcher/Debug/WrappedListener.php:115
 /home/www/app/vendor/symfony/event-dispatcher/EventDispatcher.php:230
 /home/www/app/vendor/symfony/event-dispatcher/EventDispatcher.php:59
 /home/www/app/vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:153
 /home/www/app/vendor/symfony/http-kernel/HttpKernel.php:129
 /home/www/app/vendor/symfony/http-kernel/HttpKernel.php:75
 /home/www/app/vendor/symfony/http-kernel/Kernel.php:202
 /home/www/app/vendor/symfony/http-kernel/HttpKernelBrowser.php:65
 /home/www/app/vendor/symfony/framework-bundle/KernelBrowser.php:172
 /home/www/app/vendor/symfony/browser-kit/AbstractBrowser.php:370
 /home/www/app/vendor/api-platform/core/src/Symfony/Bundle/Test/Client.php:122
@rendler-denis
Copy link
Contributor

Having the same issue and switching to the new interfaces and classes is way longer than we have time right now.
I also found this:
image

I see two issues with this:

  1. the new class is final and thus cannot be extended
  2. how should the class be made available since the condition of false will never allow PHP to execute that code?

Any help is appreciated. Thanks

@soyuka
Copy link
Member

soyuka commented Sep 23, 2022

This is how we managed to deprecate the old namespace, it should work without hassle. see https://github.com/api-platform/core/blob/2.7/src/deprecation.php#L442

Or is it using autowiring?

@rendler-denis
Copy link
Contributor

hey @soyuka .
thanks for the reply.
what I found is that the system triggers the autoloading and the problem is that the if will always be false. so, the class will never be defined. this in turn will trigger a class not found exception and everything will end there.
also, looking at the code, even if the if would be triggered the new class is also a final class, thus it cannot be inherited.

@soyuka
Copy link
Member

soyuka commented Sep 29, 2022

it's a hack we use and that works if you've an issue with autowiring or a php error let me know we don't cover IDE issues.

@soyuka soyuka closed this as completed Sep 29, 2022
@darthf1
Copy link
Contributor Author

darthf1 commented Sep 29, 2022

@soyuka this is not an IDE issue, but a real runtime issue. As explained by @rendler-denis it happens with autoloading, and in my case the error is triggered by my test suite. So not an error that can be ignored (IDE, phpstan, etc.) but an issue preventing running the app.

Can you reopen?

@rendler-denis
Copy link
Contributor

rendler-denis commented Sep 29, 2022

it's a hack we use and that works if you've an issue with autowiring or a php error let me know we don't cover IDE issues.

hey, @soyuka

I do not understand what do you mean by IDE issue?
My issue is during runtime. And my question was how is the if triggered? if it is something in my code I am really glad to change it, but can't seem to find out how to trigger the if.
the second issue I found is that the class that is defined inside the if extends a class that is marked as final. as far as I know PHP does not allow that.
I am trying to understand if this is a bug so that we can report it as such or I am doing something terribly wrong. I am just seeking for some advice not trying to cast stones.
I appreciate any assistance to get me going in the right direction.

@soyuka soyuka reopened this Sep 29, 2022
@soyuka
Copy link
Member

soyuka commented Sep 29, 2022

Please send me a reproducer. All the classes and interfaces that are mentioned here have aliases. We fixed some of them we forgot and they're available in the new 2.7.1 version. Please update and let me know how I can help.

@rendler-denis unless I have a typo in the class names you're showing here there's no issue on our end. What is your runtime error and how can I reproduce?

the second issue I found is that the class that is defined inside the if extends a class that is marked as final. as far as I know PHP does not allow that.

This is done on purpose in our code to trick the php namespace behavior in thinking the class is correctly defined. We're aliasing the class using class-alias.

@epinxteren
Copy link

epinxteren commented Oct 4, 2022

The file: ttps://github.com/api-platform/core/blob/2.7/src/deprecation.php#L442 says that the classes are deprecated.

`
// These classes are deprecated but we don't want aliases as the interfaces changed
$deprecatedClassesWithoutAliases = [
...
ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\ConstantScoreFilterExtension::class => ApiPlatform\Elasticsearch\Extension\ConstantScoreFilterExtension::class,
...

`

Any ideas how to use Elasticsearch in 2.7?

@darthf1
Copy link
Contributor Author

darthf1 commented Oct 10, 2022

@soyuka, apologies for the delay, please find the reproducer here.

When creating the reproducer, I noticed the error started when I requested a sub-resource URL. Since there is no subresource provider for elasticsearch, I created my own DataProvider which re-uses the elasticsearch collection provider.

On v2.7.2 the error goes away when I decorate the service, instead of injecting the provider as an argument:

from:

services:
    App\ApiPlatform\DataProvider\MyCollectionDataProvider:
      arguments:
        $collectionDataProvider: '@api_platform.elasticsearch.collection_data_provider'

to

services:
    App\ApiPlatform\DataProvider\MyCollectionDataProvider:
      decorates: 'api_platform.elasticsearch.collection_data_provider'

However, this changes the output of the test.

There was 1 failure:

1) App\Tests\Test::testGetMyEntities
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
     '@context' => '/api/contexts/MyOtherEntity'
     '@id' => '/api/my_entities/48e73400-4361-46b3-8d99-d9d6f4b256a1/my_other_entities'
     '@type' => 'hydra:Collection'
-    'hydra:member' => Array &1 (
-        0 => Array &2 (
-            '@id' => '/api/my_other_entities/48e73400-4361-46b3-8d99-d9d6f4b256a1'
-            '@type' => 'MyOtherEntity'
-            'id' => '48e73400-4361-46b3-8d99-d9d6f4b256a1'
-            'description' => 'first-description'
-        )
-        1 => Array &3 (
-            '@id' => '/api/my_other_entities/b1f085bb-a92d-4122-a547-0ea44ee4956f'
-            '@type' => 'MyOtherEntity'
-            'id' => 'b1f085bb-a92d-4122-a547-0ea44ee4956f'
-            'description' => 'second-description'
-        )
-    )
-    'hydra:totalItems' => 2
+    'hydra:member' => Array &1 ()
+    'hydra:totalItems' => 0
 )

@darthf1
Copy link
Contributor Author

darthf1 commented Oct 24, 2022

If there's a solution requiring changing something on my end, without migrating to the new metadata system, I'm open for suggestions :)

@darthf1
Copy link
Contributor Author

darthf1 commented Nov 3, 2022

friendly ping @soyuka. Would love to get started on the v2.7 to v3 migration :)

@darthf1
Copy link
Contributor Author

darthf1 commented Nov 15, 2022

@grandiandrea
Copy link

Any news on this?

@darthf1
Copy link
Contributor Author

darthf1 commented Dec 23, 2022

#5283 fixed the issue for me!

Thanks a lot @vincentchalamon

@darthf1 darthf1 closed this as completed Dec 23, 2022
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 a pull request may close this issue.

5 participants