Magento2: Magento 2.1 API products extension_attributes returns null

Created on 28 Sep 2016  路  10Comments  路  Source: magento/magento2

The task at hand is to synchronize the Magento products qty with data from a POS. A simple code sample provided at http://devdocs.magento.com/guides/v2.1/howdoi/webapi/filter-response.html is not working as expected. The API does return product information, but the extension_attributes always returns null.

Preconditions

  1. Magento 2.1.0
  2. PHP 5.6

    Steps to reproduce

Performed a curl with "http://myhost.dev/rest/default/V1/products/12345/?fields=name,sku,extension_attributes[category_links,stock_item[item_id,qty]]"
Note: From ?fields onward this came directly from the documentation source listed above

Expected result

According to the documentation, the result should be similar to:

{
"sku": "12345"
"name": "Product Name"
"extension_attributes": {
"category_links": {
"position": 1
"category_id": "123"
}
"stock_item": {
"item_id": 321
"qty": 1
}
}
}

Actual result

A var_dump of the response I get from the API is as follows:

"{"sku":"12345","name":"Product Name","extension_attributes":null}"

I have altered the product sku and name in my example as they are irrelevant. The NULL response for the extension_attributes is concerning me, as I reproduced the example I saw in the documentation after many other attempts to receive this data. Always a NULL response. Either I'm doing something wrong, the documentation is inaccurate, or the software is faulty.

FrameworWebapi Fixed in 2.2.x Fixed in 2.3.x Confirmed Format is valid Ready for Work Reproduced on 2.1.x bug report

All 10 comments

Thank you @tcytra for your report, we are tracking internally under MAGETWO-59183.

@tcytra , I wasn't able to reproduce this issue on 2.1.2 V1/products/12345/?fields=name,sku,extension_attributes[category_links,stock_item[item_id,qty]]" returns information about "stock_item".
"category_links" information don't return on 2.1.2 version, but this has been already fixed for current develop branch
Looks like you product doesn't have stock data, so API call returns null in extension_attributes

I've got same problem on performing api call using searchCriteria

@tcytra We are not able to reproduce this issue on the _develop_ branch. It was corrected in MAGETWO-55038 (you can search commits for this ID). It has not yet been backported, the following tickets are created for that:

  • 2.0 MAGETWO-59385
  • 2.1 MAGETWO-59384

Any news for MAGETWO-59384?

@PascalBrouwers no update yet, still in queue to be implemented

For anyone needing a temporary fix until this is released. Add this to your module in etc/extension_attributes.xml and make sure to add catalog_inventory to the sequence in your module.xml file :

<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="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface">
            <resources>
                <resource ref="Magento_CatalogInventory::cataloginventory"/>
            </resources>
            <join reference_table="cataloginventory_stock_item"
                  join_on_field="entity_id"
                  reference_field="product_id"
            >
                <field>qty</field>
                <field>item_id</field>
            </join>
        </attribute>
        <attribute code="stock_item_qty" type="string">
            <resources>
                <resource ref="Magento_CatalogInventory::cataloginventory"/>
            </resources>
            <join reference_table="cataloginventory_stock_item"
                  join_on_field="entity_id"
                  reference_field="product_id"
            >
                <field>qty</field>
            </join>
        </attribute>
    </extension_attributes>
</config>

@misha-kotov seems like an easy fix to do for 2.1. Just fix all extension_attributes.xmls

@tcytra, thank you for your report.
The issue is already fixed in develop branch, 2.2.1

@tcytra, thank you for your report.
The issue is already fixed in develop branch, 2.2.1

Was this page helpful?
0 / 5 - 0 ratings