Could not find valid 'id' or 'name' in object when using the ADD operation for REST API
Problem
When using REST API to update an issue, specifically the ADD operation, Could not find valid 'id' or 'name' error is thrown.
Example
Using the following data to update Fix Version
{"update":{"fixVersions":[{"add":[{"name":"versionone"}]}]}}
Error thrown
{"errorMessages":[],"errors":{"fixVersions":"Could not find valid 'id' or 'name' in version object."}}
OR
Using the following data to update Components
{"update":{"components":[{"add":[{"name":"componentone"}]}]}}
Error thrown
{"errorMessages":[],"errors":{"components":"Could not find valid 'id' or 'name' in component object."}}
Diagnosis
Using the exact same data, but replacing ADD with SET works with no errors
{"update":{"components":[{"set":[{"name":"versionone"}]}]}}
Cause
There is an extra square bracket in the data file
First, a little explanation between SET, ADD and REMOVE:
- they all only support existing values, so using ADD to create additional values is not possible.
ADD and REMOVE are opposites of each other, and they only support single values
e.g. if you already have 2 versions named version1 and version2, and an issue currently belongs to version1, the following json will add version2 to the issue and remove version1 from it i.e. moving the issue from version1 to version2:{"update":{"fixVersions":[{"add":{"name":"version2"}},{"remove":{"name":"version1"}}]}}
Because they only support "single" values, having square brackets [...] in front of ADD or REMOVE won't work
SET supports both single and multiple values, so there must be square brackers [...] in front of SET
e.g. adding an issue to 2 existing versions:{"update":{"fixVersions":[{"set":[{"name":"version1"},{"name":"version2"}]}]}}
Resolution
Remove the extra square brackets for the ADD operation
{"update":{"fixVersions":[{"add":{"name":"versionone"}}]}}