Skip to content

"No resource class found" if related entity is not explicitly in the file #1284

@chrishautenne

Description

@chrishautenne

Hello guys,
firstly thanks for your amazing work.

I would like to report something that is not specified in the documentation and that could save much time to anyone experiencing the same issue in the future.

I have 3 entities : Ground, HeadOffice and Address. A ground has an embeded (doctrine's annotation Embeded) relation with address, and headOffice has an embedded relation with address too.

I wanted to use api platform for ground and headoffice endpoints.
Everything works fine for ground, I get objects like this :

{
    "entitled": "STADE JEAN SPAETY 1",
    "address": {
      "street": "ROUTE D ENSISHEIM HR 2/3",
      "postalCode": "68190",
      "city": "UNGERSHEIM",
      "country": "France"
    }
}

But for headOffice I got this error :
No resource class found for object of type "AppBundle\Entity\Address".

After some debugging hours, I found out that the problem occured in src/Serializer/AbstractItemNormalizer.php in the method getAttributeValue, where it tries to get propertyMetadata from attribute : in the headOffice case it fails (type in property metadata returned is null)

After comparing all annotations and methods in Ground.php and in HeadOffice.php, I found out that the only difference was in the property setter for address : in ground class, the setter was type hinted "Address" but not in headOffice class

Ground :

    /**
     * Set address.
     *
     * @param Address $address
     *
     * @return Ground
     */
    public function setAddress(Address $address)
    {
        $this->address = $address;

        return $this;
    }

HeadOffice :

    /**
     * Set address.
     *
     * @param Address $address
     *
     * @return HeadOffice
     */
    public function setAddress($address) // <-- Note the missing type hint
    {
        $this->address = $address;

        return $this;
    }

After adding the type hint in headOffice, the error disappeared and I successfully managed to get my head offices.

Conclusion : If you don't explicitly specify the type hint in your class, the metadata factory fails to guess your property type. I thought it could guess it from namespace (they are all in AppBundle\Entity namespace after all) or from annotation on address property but apparently not.

Is it an issue ? Is it possible to add a warning on doc, especially here : https://api-platform.com/docs/core/serialization-groups-and-relations#normalization ? Is it possible to modify something to guess this part ?

Thanks for your answers. I'm not a specialist in serializer and I would be happy if you can explain to me (and others) if this is a good idea or not !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions