Skip to content

Product::getExtensionAttributes() returns orphaned empty instance if not initialized #5979

@schmengler

Description

@schmengler

Preconditions

  1. Magento CE 2.1.0

Steps to reproduce

  1. Load a product list from the product repository
  2. Load stock item from stock registry and attach it to the extension attributes
$product->getExtensionAttributes()->setStockItem($stockItem);
  1. Inspect stock item
var_dump($product->getExtensionAttributes()->getStockItem());

Expected result

  1. I see a dump of $stockItem

Actual result

NULL

Explanation

if the extension attributes were not loaded before, getExtensionAttributes() always returns an empty instance:

public function getExtensionAttributes()
{
    $extensionAttributes = $this->_getExtensionAttributes();
    if (!$extensionAttributes) {
        return $this->extensionAttributesFactory->create('Magento\Catalog\Api\Data\ProductInterface');
    }
    return $extensionAttributes;
}

Possible Solution

public function getExtensionAttributes()
{
    $extensionAttributes = $this->_getExtensionAttributes();
    if (!$extensionAttributes) {
        $extensionAttributes = $this->extensionAttributesFactory->create('Magento\Catalog\Api\Data\ProductInterface');
        $this->_setExtensionAttributes($extensionAttributes);
    }
    return $extensionAttributes;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releasebug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions