Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating a product through the REST API removes category associations #3354

Closed
AndreasSundstrom opened this issue Feb 10, 2016 · 10 comments
Assignees

Comments

@AndreasSundstrom
Copy link

@AndreasSundstrom AndreasSundstrom commented Feb 10, 2016

Let's say that I want to update the price on a product with SKU 12345. I do a PUT request to /rest/V1/products/12345 with the following JSON body:

{
    "product": {
        "price": 319.20
    }
}

This causes category associations to be removed for this product. I'm assuming that this is a bug (and not a feature) since categories are the only effected product property (except for the price, of course).

@AndreasSundstrom

This comment has been minimized.

Copy link
Author

@AndreasSundstrom AndreasSundstrom commented Feb 10, 2016

Having looked at this issue a bit more I noticed that the initialize method in Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper sets category_ids to an empty array if category_ids is not already set in the product data.

If I replace the foreach (['category_ids', 'website_ids'] as $field) { line with foreach (['website_ids'] as $field) { things seem to work as expected, but I'm far from certain that this is an acceptable solution since I don't know if this change causes problems elsewhere.

@ingmarnet

This comment has been minimized.

Copy link

@ingmarnet ingmarnet commented Feb 25, 2016

Same problem here

@PingusPepan

This comment has been minimized.

Copy link
Contributor

@PingusPepan PingusPepan commented Mar 21, 2016

@AndreasSundstrom I believe that this is not the correct final solution but it is a workaround for now. It seems to clear all assigned websites too. See #3792
I commented the whole foreach with category and website ids and edited $productData = $this->request->getPost('product'); to $productData = $this->request->getPost('product', []);
Seems to work for me for now. Let's wait for official update. In the meantime I will use this quick fix.

@kandy

This comment has been minimized.

Copy link
Contributor

@kandy kandy commented Apr 28, 2016

Updates of entity in Magento API implemented with "Replace" strategy, not "Patch". So you should send full entity.

@kandy kandy closed this Apr 28, 2016
@PingusPepan

This comment has been minimized.

Copy link
Contributor

@PingusPepan PingusPepan commented Apr 28, 2016

@kandy Do you know any reason why? My third party software is something which knows only SKUs, prices and stock amounts. How am I supposed to update the products then?
Should I first grab the product with API GET, then update affected fields and then send it back again? This feels way too much for just a simple product price/stock amount/attribute update.
Please, let me know, I am honestly interested.

@Widmayer

This comment has been minimized.

Copy link

@Widmayer Widmayer commented Apr 29, 2016

@kandy Why this has been changed from Magento 1? There it was possible to update only one attribute of a product without loosing other data. If the new behaviour is by design this would be a bad decicion in my opinion. The overhead in data sent to the shop would be multiplied when it is only neccessary to update one attribute.

@AndreasSundstrom

This comment has been minimized.

Copy link
Author

@AndreasSundstrom AndreasSundstrom commented Apr 29, 2016

@kandy No other product properties are emptied/nulled when the API request described in my initial comment is made. Why is the "categori_ids" property treated differently than other properties?

@vkorotun

This comment has been minimized.

Copy link
Contributor

@vkorotun vkorotun commented Apr 29, 2016

Hey @AndreasSundstrom, the bug is in place indeed.
We will fix it, so the behaviour will be as the following:
If you send PUT request and provide some partial data, then ALL OTHER entity attributes and associations (which belong to the current entity aggregation root) will be erased (not only category_ids as it is for the moment).

And I assume that you need something different here - PATCH method supported. We plan to add support for PATCH method for Web API in future, so then you'll be able to call to PATCH and provide partial data to be updated without affecting to other entity properties.

@PingusPepan

This comment has been minimized.

Copy link
Contributor

@PingusPepan PingusPepan commented Apr 29, 2016

@vkorotun Thanks for clarifying. What is the eta for PATCH method support? Without PATCH method support we can't migrate to Magento 2 because we use patching extensively on M1 and the API is kept very busy.

@thomasnordkvist

This comment has been minimized.

Copy link

@thomasnordkvist thomasnordkvist commented May 26, 2016

Hi @vkorotun i'm also interested in the eta for patch functionality.
Reading above it seems like i need to rewrite quite a few integrations i have, or just skip upgrading magento at some point untill PATCH is working or i have had time to change what i need.

The sensible thing in my opinion would be to keep the current functionality and make category_ids act "PATCH strategy" or at least move current functionality to PATCH, and instead add REPLACE strategy later. That way it won't crash things for current implementations. I guess there is quite a few implementations that relay on current functionality.

magento-team pushed a commit that referenced this issue Nov 28, 2018
[TSG] Backporting for 2.1 (pr58) (2.1.16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.