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

'add' on array attribute performs 'replace' when 'path' is absent #220

Closed
asutosh97 opened this issue Mar 25, 2022 · 0 comments · Fixed by #222
Closed

'add' on array attribute performs 'replace' when 'path' is absent #220

asutosh97 opened this issue Mar 25, 2022 · 0 comments · Fixed by #222

Comments

@asutosh97
Copy link
Contributor

asutosh97 commented Mar 25, 2022

Describe the bug

Let's look at the following 4 code snippets and their corresponding outputs

Case 1

Code

const scimResource = {
    displayName: "ramayan",
    members: [
        { name: "ram" },
        { name: "laxman" }
    ]
}

const patches = {
    Operations: [
        {
            op: "add",
            path: "members",
            value: { name: "hanuman" }
        },
    ]
}

const patchedResource = scimPatch(scimResource, patches.Operations, { mutateDocument: false });
console.log(patchedResource);

Output

{
  displayName: 'ramayan',
  members: [ { name: 'ram' }, { name: 'laxman' }, { name: 'hanuman' } ]
}

Case 2

Code

const scimResource = {
    displayName: "ramayan",
    members: [
        { name: "ram" },
        { name: "laxman" }
    ]
}

const patches = {
    Operations: [
        {
            op: "add",
            path: "members",
            value: [{ name: "hanuman" }]
        },
    ]
}

const patchedResource = scimPatch(scimResource, patches.Operations, { mutateDocument: false });
console.log(patchedResource);

Output

{
  displayName: 'ramayan',
  members: [ { name: 'ram' }, { name: 'laxman' }, { name: 'hanuman' } ]
}

Case 3

Code

const scimResource = {
    displayName: "ramayan",
    members: [
        { name: "ram" },
        { name: "laxman" }
    ]
}

const patches = {
    Operations: [
        {
            op: "add",
            value: {
                members: { name: "hanuman" }
            }
        },
    ]
}

const patchedResource = scimPatch(scimResource, patches.Operations, { mutateDocument: false });
console.log(patchedResource);

Output

{ displayName: 'ramayan', members: { name: 'hanuman' } }

Case 4

Code

const scimResource = {
    displayName: "ramayan",
    members: [
        { name: "ram" },
        { name: "laxman" }
    ]
}

const patches = {
    Operations: [
        {
            op: "add",
            value: {
                members: [{ name: "hanuman" }]
            }
        },
    ]
}

const patchedResource = scimPatch(scimResource, patches.Operations, { mutateDocument: false });
console.log(patchedResource);

Output

{ displayName: 'ramayan', members: [ { name: 'hanuman' } ] }

Case 1 & 2 give the correct output, whereas case 3 & 4 produce the wrong output.
For case 3 & 4, if the operation was "replace", then that output would have been correct.

I've figured out a fix, will be creating a PR soon

asutosh97 added a commit to asutosh97/scim-patch that referenced this issue Mar 25, 2022
- for add operations, append values to end incase of array attribute
thomaspoignant pushed a commit that referenced this issue Mar 29, 2022
…attribute) (#222)

* fixes #220
- for add operations, append values to end incase of array attribute

* made the code more readable

* added docstring for options argument

Co-authored-by: Asutosh Sahoo <asutosh.sahoo@postman.com>
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

Successfully merging a pull request may close this issue.

1 participant