Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Describing attributes in .yaml doesn`t apply to virtual properties (method) #2956

@wwwitalya

Description

@wwwitalya

API Platform version(s) affected: 4.2.6 + Symfony
Describing attributes in .yaml doesn`t apply to virtual properties (method)

Description
If we try to describe virtual property (like method) in resource YAML then extra attributes doesn`t read and apply to property because of bug in ApiPlatform\Metadata\Property\Factory\ExtractorPropertyMetadataFactory https://github.com/api-platform/metadata/blob/3.1/Property/Factory/ExtractorPropertyMetadataFactory.php line 48

        if (
            !property_exists($resourceClass, $property) && !interface_exists($resourceClass)
            || null === ($propertyMetadata = $this->extractor->getProperties()[$resourceClass][$property] ?? null)
        ) {
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
        }

If we using propertyExtractor why do we need straight check of property exists?
Just for record, PHP attribute #[ApiProperty] works as expected.

How to reproduce

Entity/User.php
class User {
    public function isAdmin(): bool
    {
        return true;
    }
}
config/api/user.yaml
properties:
  App\Entity\User
    admin:
      security: "user.isAdmin() === true"

Because of no property named '$admin' in class property from .yaml doesn`t apply.

Possible Solution
I think property_exists is useless condition and should be removed.

if (null === ($propertyMetadata = $this->extractor->getProperties()[$resourceClass][$property] ?? null))
{
     return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
}

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