cancel
Showing results for 
Search instead for 
Did you mean: 

extension attributes generate code error

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

extension attributes generate code error

Hi everyone,

 

I wrote a plugin followed with the "magento2 sample/sample-external-link" to add a "custom_test_attribute" on the product , here is "extension_attributes.xml" :

 

<?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\Catalog\Api\Data\ProductInterface"> <attribute code="custom_test_attribute" type="string" /> </extension_attributes> </config>

di.xml and plugin code:

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="ProductExtensionAttributeOperations" type="zasi\ExtensionRest\plugin\AddReviewAttributes"  disabled="false"/>
    </type>
</config>
<?php

namespace zasi\ExtensionRest\plugin;


use Magento\Catalog\Api\Data\ProductExtensionInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductExtensionFactory;

class AddReviewAttributes
{

        private $extensionFactory;

        public function __construct(ProductExtensionFactory $extensionFactory)
        {   
            $this->extensionFactory = $extensionFactory;
        }

        public function afterGetList
        (
            \Magento\Catalog\Api\ProductRepositoryInterface $subject,
            \Magento\Framework\Api\SearchResults $searchResult
        ) {
            /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
            foreach ($searchResult->getItems() as $product) {
                $this->addExternalLinksToProduct($product);
            }

            return $searchResult;
        }

        public function afterGet
        (
            \Magento\Catalog\Api\ProductRepositoryInterface $subject,
            \Magento\Catalog\Api\Data\ProductInterface $product
        ) {
            $this->addExternalLinksToProduct($product);
            return $product;
        }


        private function addExternalLinksToProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
        {
            $extensionAttributes = $product->getExtensionAttributes();

            if (empty($extensionAttributes)) {
                $extensionAttributes = $this->productExtensionFactory->create();
            }
            
            $extensionAttributes->setCustomTestAttribute('test');
            $product->setExtensionAttributes($extensionAttributes);
            return $this;
        }

}

When I use postman to get the response via "rest/V1/products" router, I got an error , it said : 

Fatal Error: 'Uncaught TypeError: Argument 1 passed to Magento\\Catalog\\Api\\Data\\ProductExtension::setCustomTestAttribute() must be an instance of number, string given, 

I found the methods genrated by magento incorrectly:

 

ProductExtensionInterface.php:

<?php
namespace Magento\Catalog\Api\Data;

/**
 * ExtensionInterface class for @see \Magento\Catalog\Api\Data\ProductInterface
 */
interface ProductExtensionInterface extends \Magento\Framework\Api\ExtensionAttributesInterface
{
 ........
    /**
     * @return number|null
     */
    public function getCustomTestAttribute();

    /**
     * @param number $customTestAttribute
     * @return $this
     */
    public function setCustomTestAttribute(\number $customTestAttribute);
}

the setCustomTestAttribute paramters is "\number $customTestAttribute" , I have to correct it manually.

 

This problem stuck me a whole day , can you guys help me to fix it? what's wrong with my codes?

 

(magento CE 2.3.3 , Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.3.12)

 

Thanks you verrrrrry much!!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: extension attributes generate code error

update my problem:

I add a new extension attribute in the xml file , but the it does not appear in generate code.

I remembered that at first I set the 'custom_test_attribute' type is 'number' , so I guest is that the code generator use some cache template

I disable all caches and upgrade again . it works Smiley Happy

View solution in original post

1 REPLY 1

Re: extension attributes generate code error

update my problem:

I add a new extension attribute in the xml file , but the it does not appear in generate code.

I remembered that at first I set the 'custom_test_attribute' type is 'number' , so I guest is that the code generator use some cache template

I disable all caches and upgrade again . it works Smiley Happy