Skip to content

Instantly share code, notes, and snippets.

@leflings
Last active August 23, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leflings/11b9b3d6e871810472c52abb4af5fe6f to your computer and use it in GitHub Desktop.
Save leflings/11b9b3d6e871810472c52abb4af5fe6f to your computer and use it in GitHub Desktop.
Sylius: /api/v1/product-options/XXX weirdness (question at the end of gist)

Create the option

POST http://127.0.0.1:8000/api/v1/product-options/ HTTP/1.1
Authorization: Bearer VALID_TOKEN
Content-Type: application/json

{
  "code": "my_test_option_for_testing",
  "position": 10,
  "translations": {
    "en_US": {
      "name": "Is this weird?"
    }
  },
  "values": [
    {
      "code": "my_test_option_value_yes",
      "translations": {
        "en_US": {
          "value": "Yes"
        }
      }
    },
    {
      "code": "my_test_option_value_no",
      "translations": {
        "en_US": {
          "value": "No"
        }
      }
    }
  ]
}

Update the option (with a new optionvalue at index 1, existing optionvalue "pushed" to index 2)

PUT http://127.0.0.1:8000/api/v1/product-options/my_test_option_for_testing HTTP/1.1
Authorization: Bearer VALID_TOKEN
Content-Type: application/json

{
  "code": "my_test_option_for_testing",
  "position": 10,
  "translations": {
    "en_US": {
      "name": "Is this weird?"
    }
  },
  "values": [
    {
      "code": "my_test_option_value_yes",
      "translations": {
        "en_US": {
          "value": "Yes"
        }
      }
    },
    {
      "code": "my_test_option_value_somewhat",
      "translations": {
        "en_US": {
          "value": "Yeah, sort of"
        }
      }
    },
    {
      "code": "my_test_option_value_no",
      "translations": {
        "en_US": {
          "value": "No"
        }
      }
    }
  ]
}

Observe the validation error

{
    "code": 400,
    "message": "Validation Failed",
    "errors": {
        "children": {
            "position": {},
            "translations": {
                "children": {
                    "en_US": {
                        "children": {
                            "name": {}
                        }
                    }
                }
            },
            "values": {
                "children": [
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {}
                        }
                    },
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {}
                        }
                    },
                    {
                        "children": {
                            "translations": {
                                "children": {
                                    "en_US": {
                                        "children": {
                                            "value": {}
                                        }
                                    }
                                }
                            },
                            "code": {
                                "errors": [
                                    "The option value with given code already exists."
                                ]
                            }
                        }
                    }
                ]
            },
            "code": {}
        }
    }
}

My question is this

Is this really the expected behaviour?

Say I actually cared about changing the position of two values, it seems to me like I would have to first update the option to not include the two values I want to change, and then later update it again with the values in the order I want them. But what if said values are in use on a product variant, I'm assuming it would be illegal to remove them from the option.

Aside from aforementioned particular problem, there's issue of having to have local bookkeeping data of value positions, in order to formulate a valid PUT/PATCH query. If not having local bookkeeping, I'd need to first GET option details, synchronize the order of my values with the order of the values on the server.

I'm not saying this is a bug, but I'm just trying to understand.

After creating the product-option as described in the first file, I attempt a patch call:

PATCH http://127.0.0.1:8000/api/v1/product-options/my_test_option_for_testing HTTP/1.1
Authorization: Bearer VALID_TOKEN
Content-Type: application/json

{
  "code": "my_test_option_for_testing",
  "position": 10,
  "translations": {
    "en_US": {
      "name": "Is this weird?"
    }
  },
  "values": [
    {
      "code": "my_test_option_value_somewhat",
      "translations": {
        "en_US": {
          "value": "Yeah, sort of"
        }
      }
    }
  ]
}

things get very weird now!

Result of GET afterwards

{
    "id": 16,
    "code": "my_test_option_for_testing",
    "position": 10,
    "values": [
        {
            "code": "my_test_option_value_yes",
            "translations": {
                "en_US": {
                    "locale": "en_US",
                    "id": 39,
                    "value": "Yeah, sort of"
                }
            }
        },
        {
            "code": "my_test_option_value_no",
            "translations": {
                "en_US": {
                    "locale": "en_US",
                    "id": 40,
                    "value": "No"
                }
            }
        }
    ],
    "translations": {
        "en_US": {
            "locale": "en_US",
            "id": 16,
            "name": "Is this weird?"
        }
    },
    "_links": {
        "self": {
            "href": "/api/v1/product-options/my_test_option_for_testing"
        }
    }
}

It updates my_test_option_value_yes (which doesn't even match my specified code!!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment