magento2: REST API multiselect attribute values are sent in an incompatible format and cannot be cleared

Preconditions

  1. Magento 2.1.0

Steps to reproduce

  1. Add a product attribute with multiple select input type and define several options (values).
  2. Choose a sku set some values for the attribute.
  3. GET the sku with /V1/products/{sku}
  4. Wrap the returned JSON body with { “product”: <JSON GET result> }.
  5. PUT the sku with /V1/products/{sku}
  6. Edit the JSON body and set the attribute value to [] (empty array).
  7. PUT the sku again.

Expected result

  1. The unchanged results of a GET should PUT without errors.
  2. A multi select attribute with a single option with id “123” selected should be sent as [“123”].
  3. Multiple options selected should look like [“123”,“456”].
  4. Putting a an empty array should clear all the selected options for the attribute.

Actual result

  1. Putting an unchanged GET returns errors on the multiselect attribute: { "message": "Error occurred during \"custom_attributes\" processing. Attribute \"occasion\" has invalid value. Invalid type for value: \"string\". Expected Type: \"string[]\"." }
  2. If the attribute has a single option, it is sent as “123”, not [“123”].
  3. If the attribute has multiple options, it is sent as “123,456”, not [“123”,“456”].
  4. Putting an empty array does not remove whatever options are selected.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 3
  • Comments: 19 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Thank you for your submission.

We recently made some changes to the way we process GitHub submissions to more quickly identify and respond to core code issues.

Feature Requests and Improvements should now be submitted to the new Magento 2 Feature Requests and Improvements forum (see details here).

We are closing this GitHub ticket and have moved your request to the new forum.

The problem with not fixing this bug ASAP is that the only way to clear a multi-select field of all selections is to delete the product and re-create it, which breaks all links to that product in customers’ order history, re-ordering, etc. I don’t think you should wait until it’s a convenient time from an API versioning perspective. There are other pragmatic ways to fix this bug.

Instead of waiting to version the whole API, why not just send two versions of any multi-select field and accept either version in POST? For example, send: attribute: "123,456", attribute-v2: ["123", "456"].

@dnadle, sorry for the confusion. This was picked up by an automated script. I’ve recreated it as a bug and we are tracking it internally as MAGETWO-58652.

After 7th step I get updated product, if I send values:[] I get “Invalid option value” message. Only 4th point of expected is reproduced and it looks like improvement.