core icon indicating copy to clipboard operation
core copied to clipboard

ExtractorPropertyMetadataFactory does not handle calculated field

Open camilledejoye opened this issue 2 years ago • 0 comments

API Platform version(s) affected: 2.6.8

Description
When defining the API resources with a YAML or XML files it's not possible to define information for calculated fields. This is a similar issue to https://github.com/api-platform/core/issues/3528 for magic methods.

How to reproduce
Configure a resource have a calculated field:

namespace App;

class Resource
{
    // ....

    public function getInformation(): array
    {
        return [];
    }
}
# Serialization configuration
App\Resource:
  attributes:
    # ...

    information:
      groups: ['read']
# API Platform configuration
App\Resource:
  # ...

  properties:
    information:
      attributes:
        openapi_context:
          type: object
          # ...

Possible Solution
https://github.com/api-platform/core/blob/ff3aab5b196709c721960c0bb4f1d52759af737d/src/Metadata/Property/Factory/ExtractorPropertyMetadataFactory.php#L54-L59

  1. Remove the first line of the if statement to only check if we defined any anything in the configuration files
  2. Add a separate check for a method matching the property (like in the AnnotationPropertyMetadataFactory), but this will couple the name of the method to the named of the serialized field which is not ideal.

Additional Context

There is no issue when using annotation/attribute directly on the method used by the serializer:

namespace App;

class Resource
{
    // ....

    #[ApiProperty(attributes: [
        'openapi_context' => [
            'type' => 'object',
            // ...
        ],
    ])]
    public function getInformation(): array
    {
        return [];
    }
}

camilledejoye avatar Sep 15 '22 13:09 camilledejoye