Skip to content

Adding product attribute options through api can fail because of incorrect string comparison #13151

@koenner01

Description

@koenner01

Trying to insert a new product attribute option through the api that contains an existing value but starting with a character '0' will fail. This also happens when directly using the Magento\Catalog\Api\ProductAttributeOptionManagementInterface add function

Preconditions

  1. PHP7
  2. CE 2.2.2 (looking at the codebase on github this issue is present since the start of Magento2), also tested on EE 2.2.2

Steps to reproduce

  1. Create a product attribute option '95' manually for a random attribute
  2. Try to insert a new option for the same random attribute through api '../rest/all/V1/products/attributes/{randomAttributeCode}/options' with body:
{
    "option": {
        "label": "095"
    }
}
  1. Watch Magento fail time and time again in the add function of Magento\Catalog\Model\Product\Attribute\OptionManagement where an in_array check is done that causes false to be returned.

Expected result

  1. A new option '095' should be created

Actual result

  1. Api returns status 200 with response 'false'

Code snippet from Magento\Catalog\Model\Product\Attribute\OptionManagement add function:

public function add($attributeCode, $option)
{
    /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $currentOptions */
    $currentOptions = $this->getItems($attributeCode);
    if (is_array($currentOptions)) {
        array_walk($currentOptions, function (&$attributeOption) {
            /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $attributeOption */
                $attributeOption = $attributeOption->getLabel();
        });
        if (in_array($option->getLabel(), $currentOptions)) {
            return false;
        }
    }
    return $this->eavOptionManagement->add(
        \Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
        $attributeCode,
        $option
    );
}

The in_array function is not working correctly; PHP documentation states that there is a third parameter that can be passed in the in_array function for strict validation. Adding this solves the problem

#magentomagic

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 passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions