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

rfc6902.createPatch() removing object from array. #27

Open
hafeefshaik opened this issue Apr 25, 2018 · 3 comments
Open

rfc6902.createPatch() removing object from array. #27

hafeefshaik opened this issue Apr 25, 2018 · 3 comments

Comments

@hafeefshaik
Copy link

hafeefshaik commented Apr 25, 2018

Actual object:

{
  "id": "06449690-3e21-43c9-0c13-08d5a8f9a4ff",
   "firstName": "Hafeef",
  "lastName": "Shaik",
  "roles": [
    "Developer",
    "Admin"
  ]
}

Later added Business Analyst and Delivery Manager roles. Modified Object:

{
  "id": "06449690-3e21-43c9-0c13-08d5a8f9a4ff",
   "firstName": "Hafeef",
  "lastName": "Shaik",
  "roles": [
    "Admin",
    "Business Analyst",
    "Delivery Manager",
    "Developer"
  ]
}

Result of rfc6902.createPatch()

[
  {
    "op": "replace",
    "path": "/roles/1",
    "value": "Business Analyst"
  },
  {
    "op": "replace",
    "path": "/roles/0",
    "value": "Admin"
  },
  {
    "op": "add",
    "path": "/roles/2",
    "value": "Delivery Manager"
  },
  {
    "op": "add",
    "path": "/roles/3",
    "value": "Developer"
  }
]

Please correct me if I am wrong, I am expecting two add operations for newly added roles, However I could see replacing new role and adding existing roles. Please guide me.

@chbrown
Copy link
Owner

chbrown commented Apr 25, 2018

Replace the original object's

  "roles": [
    "Developer",
    "Admin"
  ]

with

  "roles": [
    "Admin",
    "Developer"
  ]

and you'll get the result you expect.

JSON has no notion of sets, which might be part of the confusion.

The rfc6902.createPatch() does not currently (PRs welcome!) implement the "move" operation, which I should document in the README somewhere.

That said, I admit I would find it a little more intuitive if it produced operations like:

  1. remove "Developer"
  2. add "Business Analyst"
  3. add "Delivery Manager"
  4. add "Developer"

But since each operation has the same "cost", it simply runs with the first 4-operation sequence it found.

@hafeefshaik
Copy link
Author

@chbrown Thanks for the suggestion.

@raitucarp
Copy link

raitucarp commented Oct 17, 2021

@chbrown

which I should document in the README somewhere.

Can someone do a PR for that ASAP? Cause misleading and waste of time for developer who want "move" operation until discover this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants