Magento 2.4 Rest API Error – Method’s Return Type Must Be Specified Using @return Annotation

apimagento2.4

I had been trying to use rest api for getting Configurable Products (Child products) using the rest API –
https:url//rest/V1/configurable-products/sku/children

But, I am getting this framework error –
"Method's return type must be specified using @return annotation. See Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::load()"

Log Trace:

"trace": "#0
/var/www/html/vendor/magento/framework/Reflection/TypeProcessor.php(291):
Magento\Framework\Reflection\TypeProcessor->getMethodReturnAnnotation(Object(Laminas\Code\Reflection\MethodReflection))\n#1
/var/www/html/vendor/magento/framework/Reflection/MethodsMap.php(180):
Magento\Framework\Reflection\TypeProcessor->getGetterReturnType(Object(Laminas\Code\Reflection\MethodReflection))\n#2
/var/www/html/vendor/magento/framework/Reflection/MethodsMap.php(115):
Magento\Framework\Reflection\MethodsMap->getMethodMapViaReflection('Magento\\Catalog…')\n#3
/var/www/html/vendor/magento/framework/Reflection/DataObjectProcessor.php(83):
Magento\Framework\Reflection\MethodsMap->getMethodsMap('Magento\\Catalog…')\n#4
/var/www/html/vendor/magento/framework/Reflection/CustomAttributesProcessor.php(76):
Magento\Framework\Reflection\DataObjectProcessor->buildOutputDataArray(Object(Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor),
'Magento\\Catalog…')\n#5
/var/www/html/vendor/magento/framework/Reflection/CustomAttributesProcessor.php(53):
Magento\Framework\Reflection\CustomAttributesProcessor->convertCustomAttribute(Object(Magento\Framework\Api\AttributeValue),
'\\Magento\\Catalo…')\n#6
/var/www/html/generated/code/Magento/Framework/Reflection/CustomAttributesProcessor/Proxy.php(95):
Magento\Framework\Reflection\CustomAttributesProcessor->buildOutputDataArray(Object(Magento\Catalog\Model\Product\Interceptor),
'\\Magento\\Catalo…')\n#7
/var/www/html/vendor/magento/framework/Reflection/DataObjectProcessor.php(107):
Magento\Framework\Reflection\CustomAttributesProcessor\Proxy->buildOutputDataArray(Object(Magento\Catalog\Model\Product\Interceptor),
'\\Magento\\Catalo…')\n#8
/var/www/html/vendor/magento/framework/Webapi/ServiceOutputProcessor.php(118):
Magento\Framework\Reflection\DataObjectProcessor->buildOutputDataArray(Object(Magento\Catalog\Model\Product\Interceptor),
'\\Magento\\Catalo…')\n#9
/var/www/html/vendor/magento/framework/Webapi/ServiceOutputProcessor.php(78):
Magento\Framework\Webapi\ServiceOutputProcessor->convertValue(Array,
'\\Magento\\Catalo…')\n#10
/var/www/html/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(97):
Magento\Framework\Webapi\ServiceOutputProcessor->process(Array,
'Magento\\Configu…', 'getChildren')\n#11
/var/www/html/vendor/magento/module-webapi/Controller/Rest.php(188):
Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy))\n#12
/var/www/html/vendor/magento/framework/Interception/Interceptor.php(58):
Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#13
/var/www/html/vendor/magento/framework/Interception/Interceptor.php(138):
Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch',
Array)\n#14
/var/www/html/vendor/magento/framework/Interception/Interceptor.php(153):
Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))\n#15
/var/www/html/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(23):
Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch',
Array, Array)\n#16
/var/www/html/vendor/magento/framework/App/Http.php(116):
Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#17
/var/www/html/vendor/magento/framework/App/Bootstrap.php(264):
Magento\Framework\App\Http->launch()\n#18
/var/www/html/pub/index.php(33):
Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))\n#19
{main}" }

Upon Investigation, I found that –
In vendor\magento\module-configurable-product\Model\LinkManagement.php has the function "public function getChildren($sku)". When I change the following line (104) –
$this->dataObjectHelper->populateWithArray($productDataObject,
$attributes, \Magento\Catalog\Api\Data\ProductInterface::class);

to

$this->dataObjectHelper->populateWithArray($productDataObject,
[], \Magento\Catalog\Api\Data\ProductInterface::class);

There is no API error anymore, But the API output looks like –

[
    {
        "sku": null,
        "status": 1,
        "product_links": [],
        "tier_prices": []
    },
    {
        "sku": null,
        "status": 1,
        "product_links": [],
        "tier_prices": []
    },
    {
        "sku": null,
        "status": 1,
        "product_links": [],
        "tier_prices": []
    },
    ...
]

With no sku info.

What could be the issue here, that I am missing?

Best Answer

You are receiving this error from this file - vendor/magento/framework/Reflection/TypeProcessor.php::getMethodReturnAnnotation, which throws an InvalidArgumentException.

$this->dataObjectHelper->populateWithArray() calls the _setDataValues() method, which in turn calls the setComplexValue() method. The setComplexValue() method ultimately calls the getMethodReturnAnnotation() method by calling the getMethodReturnType() method.

No error is thrown when you pass the $attributes parameter as an empty array because _setDataValues() loops over the attributes array, which is skipped.

A debugger like xdebug can help to easily debug the issue. You should add breakpoints in the flow mentioned above to detect the exact issue.

Related Topic