Third-party developers cannot change the API Data interfaces defined in the Magento Core module. Check the interface for the methods getExtensionAttributes()
and setExtensionAttributes()
to determine if they are available for the entity.
To add the getter and setter method dynamically to the existing interface without modifying the core code Magento provide the facility to add extension attribute so that your data can expose with the API.
You have to create extension_attributes.xml inside your module etc folder like below
<config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:Api/etc/extension_attributes.xsd”> <extension_attributes for=“{interface class name where you want to add getter and setter”> <attribute code=“{attribute code}” type=“{data type}” /> </extension_attributes> </config>
Example:
<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="some_custom_data" type="string[]" /> </extension_attributes> </config>
<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="our_custom_data" type="Magento\SomeModule\Api\Data\CustomDataInterface" /> </extension_attributes> </config>