Skip to content

REST API endpoint throwing wrong exception #5191

@paulespino

Description

@paulespino

Steps to reproduce

  1. Install Magento from 2.1 branch.
  2. Set up API integration
  3. Send call to create Simple Product via /V1/products endpoint
  4. Once simple has been added, create Configurable Product via V1/products endpoint
  5. Add Simple Product created on Step 3 via /V1/configurable-products/{sku}/child to the Configurable Product created on Step 4.

Expected result

  1. 200 Response (return true) when adding the child product to the parent

Actual result

  1. [Example] Error message appears: "Product has been already attached".
    --- actual error ----
    {"message":"Product has been already attached","trace":"#0 [internal function]: Magento\ConfigurableProduct\Model\LinkManagement->addChild('576b124120360', '576b12351fb04')\n#1 /var/www/staging.streetwear.la/public_html/app/code/Magento/Webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)\n#2 /var/www/staging.streetwear.la/public_html/app/code/Magento/Webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()\n#3 /var/www/staging.streetwear.la/public_html/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))\n#4 /var/www/staging.streetwear.la/public_html/lib/internal/Magento/Framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))\n#5 /var/www/staging.streetwear.la/public_html/lib/internal/Magento/Framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()\n#6 /var/www/staging.streetwear.la/public_html/index.php(44): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))\n#7 {main}"}

In closing:

I tested the the endpoint method addChild() located on line 95 on file:
app/code/Magento/ConfigurableProduct/Model/LinkManagement.php

Here is the method with notes:

public function addChild($sku, $childSku)
{
    $product = $this->productRepository->get($sku);
    $child = $this->productRepository->get($childSku);

    $childrenIds = array_values($this->configurableType->getChildrenIds($product->getId())[0]);

    if (in_array($child->getId(), $childrenIds)) {
        throw new StateException(__('Product has been already attached'));
    }

    $childrenIds[] = $child->getId();
    $product->setAssociatedProductIds($childrenIds);
    $product->save();
    return true;
}

This IF statement (in_array($child->getId(), $childrenIds)) fires if the ID of $childSku is returned by this line:

$childrenIds = array_values($this->configurableType->getChildrenIds($product->getId())[0]);

The weird part is that the exception that is executed here:

throw new StateException(__('Product has been already attached'));

fires even after adding the FIRST Single Product to the Configurable and that is what the BUG is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CatalogEvent: WroCDContribution day in WroIssue: Cannot ReproduceCannot reproduce the issue on the latest `2.4-develop` branchIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is not validGate 1 Failed. Automatic verification of issue format is failedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releasebug report

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions