Skip to content

DataObjectProcessor reflection is not using FQN for return types #34903

Open
@MeCapron

Description

@MeCapron

Preconditions (*)

  1. Magento 2.4.2-p2

Steps to reproduce (*)

  1. Add an extension attribute to the CustomerInterface with a specific Model :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
        <attribute code="myObj" type="Vendor\Merchant\Model\Response"/>
    </extension_attributes>
</config>
<?php

declare(strict_types=1);

namespace Vendor\Merchant\Model;

class MyObj
{
    private InnerAttribute $myAttribute;

    /**
     * @return InnerAttribute
     */
    public function getMyAttribute(): InnerAttribute
    {
        return $this->myAttribute;
    }

    /**
     * @param InnerAttribute $myAttribute
     */
    public function setMyAttribute(InnerAttribute $myAttribute): void
    {
        $this->myAttribute = $myAttribute;
    }
}

With InnerAttribute defined in the same folder :

<?php

declare(strict_types=1);

namespace Vendor\Merchant\Model;

class InnerAttribute
{

}
  1. Class will not be instantied in Reflection and you will get an error from vendor/magento/framework/Reflection/DataObjectProcessor.php

  2. While the getParamType from vendor/magento/framework/Reflection/TypeProcessor.php resolves the FQN when not specified, the function getGetterReturnType is not resolving the FQN which leads to an error.

Expected result (*)

  1. Reflection should be made on FQN and not on relative class name

Actual result (*)

  1. Error is thrown "Class InnerAttribute does not exist"

As a quick fix, I made the function resolving the FQN as the param does :

https://github.com/magento/magento2/blob/2.4.2-p2/lib/internal/Magento/Framework/Reflection/TypeProcessor.php#L285

    public function getGetterReturnType($methodReflection)
    {
        $returnAnnotation = $this->getMethodReturnAnnotation($methodReflection);
        $types = $returnAnnotation->getTypes();
        $returnType = null;
        foreach ($types as $type) {
            if ($type !== 'null') {
                $returnType = $type;
                break;
            }
        }

        $returnType = $this->resolveFullyQualifiedClassName($methodReflection->getDeclaringClass(), $returnType);

        $nullable = in_array('null', $types);

        return [
            'type'           => $returnType,
            'isRequired'     => !$nullable,
            'description'    => $returnAnnotation->getDescription(),
            'parameterCount' => $methodReflection->getNumberOfRequiredParameters()
        ];
    }

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Labels

Area: FrameworkComponent: AppIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Progress: dev in progressReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions