Core: ReflectionException: Class not found

Created on 20 Jan 2020  路  17Comments  路  Source: api-platform/core

API Platform version(s) affected: 2.5.4 (from commit https://github.com/api-platform/core/commit/baf054234eff3b9276b6783efb825bff7099d5c9)

Description / How to reproduce
Loading the api docs causes several "class not found" ReflectionExceptions.
Screenshot from 2020-01-20 13-53-51
Possibly related to https://github.com/api-platform/core/issues/3344

Possible Solution
Seems to be related to src/JsonSchema/TypeFactory:102. If the check in changed from and to or, everything works (working example below)

        if ($this->isResourceClass($className) || true !== $readableLink) {
            return [
                'type' => 'string',
                'format' => 'iri-reference',
            ];
        }

Unfortunately I don't know the internals enough to know if this breaks anything.

Most helpful comment

FYi, I've got a funny one, because of that error in a vendor @return Returns blablablab:

https://github.com/KnpLabs/DoctrineBehaviors/blob/9cc036ee32483c306fb32b4efbf33014ddfb654f/src/Model/Translatable/TranslatableMethods.php#L149

I got errors like

In SchemaFactory.php line 216:

  Class Lib\Core\Entity\Catalog\Reference\Returns does not exist  

Because in a class in namespace Lib\Core\Entity\Catalog\Reference i've a use TranslatableTrait which use TranslatableMethods

All 17 comments

Definitely related.

btw it looks like this class really doesn't exist, you have a bad import.

I've checked, import is correct. The error message is gone if in the related trait I remove the use for the EventDispatcherInterface statement and replace the class references with the FQN. But the error appears with all used traits that have properties that are used in ApiResource entities. I guess this means that the class resolver has a bug somewhere.

Example of when it throws an exception

<?php

namespace App\DependencyInjection;

use \Symfony\Component\EventDispatcher\EventDispatcherInterface;

trait EventDispatcherAwareTrait
{

    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @inheritDoc
     */
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }
}

No exception after this change

<?php

namespace App\DependencyInjection;

trait EventDispatcherAwareTrait
{

    /**
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @inheritDoc
     */
    public function setEventDispatcher(\Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }
}

Not this namespace, the one you have in your Entity: App\Entity\Custom\InternalNote\EventDispatcherInterface should be \Symfony\Component\EventDispatcher\EventDispatcherInterface in the error. I can not reproduce this with the same code on my end.

Now trying to reproduce the $ref but same...

@soyuka That's the point - I don't have that property defined in the Entity. It is defined in the trait I pasted above and used in the Entity.

Let me be more verbose:
I have an entity \App\Entity\Custom\InternalNote\InternalNote

<?php

namespace App\Entity\Custom\InternalNote;

use App\Contract\Entity as EntityContract;

class InternalNote implements EntityContract\Audit\AuditableInterface
{
    use EntityContract\Audit\AuditableTrait;
    ...
}

AuditableTrait content is

<?php

namespace App\Contract\Entity\Audit;

use App\DependencyInjection\EventDispatcherAwareTrait;

trait AuditableTrait
{
    use EventDispatcherAwareTrait;
    ...
}

EventDispatcherAwareTrait content is

<?php

namespace App\DependencyInjection;

use \Symfony\Component\EventDispatcher\EventDispatcherInterface;

trait EventDispatcherAwareTrait
{

    /**
     * @var EventDispatcherInterface
     */
    protected $eventDispatcher;

    /**
     * @inheritDoc
     */
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
    {
        $this->eventDispatcher = $eventDispatcher;
    }
}

The entity does not redeclare the $eventDispatcher property nor any other property from that trait "chain".

nevermind I got to reproduce the bug! thanks

So, I tracked the bugs to php documentor I'll open issues their regarding these issues.

In the mean time you should use the FQDN in the Trait property... I'll try to find a quicker solution to these problems.

FYi, I've got a funny one, because of that error in a vendor @return Returns blablablab:

https://github.com/KnpLabs/DoctrineBehaviors/blob/9cc036ee32483c306fb32b4efbf33014ddfb654f/src/Model/Translatable/TranslatableMethods.php#L149

I got errors like

In SchemaFactory.php line 216:

  Class Lib\Core\Entity\Catalog\Reference\Returns does not exist  

Because in a class in namespace Lib\Core\Entity\Catalog\Reference i've a use TranslatableTrait which use TranslatableMethods

@soyuka Do you have some news about this issue ?
Because i tried to used https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md and i have :
Class App\Entity\TranslationInterface does not exist

Symfony : 5.0.9
API Platform : 2.5.6

error

i'm still use version 2.5.3 cause of this issue :(

@soyuka Do you have some news about this issue ?
Because i tried to used https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md and i have :
Class App\Entity\TranslationInterface does not exist

Symfony : 5.0.9
API Platform : 2.5.6

error

Same issue.
Symfony: 5.1.7
API Platform: 2.5.7

EDIT:

As @soyuka mentioned

In the mean time you should use the FQDN in the Trait property [...]

For knplabs/doctrine-behaviors bundle, you have to override classes below and remove the @var or use the FQDN for the @var :

This arrises if you you do something like this:

@ODM\ReferenceOne(targetDocument=Services::class, storeAs="id" , inversedBy="system") and the class doesn't exist. The error should be more exact if possible.

@soyuka Do you have some news about this issue ?
Because i tried to used https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md and i have :
Class App\Entity\TranslationInterface does not exist
Symfony : 5.0.9
API Platform : 2.5.6
error

Same issue.
Symfony: 5.1.7
API Platform: 2.5.7

EDIT:

As @soyuka mentioned

In the mean time you should use the FQDN in the Trait property [...]

For knplabs/doctrine-behaviors bundle, you have to override classes below and remove the @var or use the FQDN for the @var :

@vic-blt: tried this unsuccessfully, would you have more details or an example on your work around?

tried this unsuccessfully, would you have more details or an example on your work around?

Unfortunately no because I opted in for another API solution.
I'm currently using API Gateway of AWS.

Was this page helpful?
0 / 5 - 0 ratings