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

Trouble creating a patch from objects in array #49

Closed
jayme-github opened this issue May 2, 2016 · 4 comments
Closed

Trouble creating a patch from objects in array #49

jayme-github opened this issue May 2, 2016 · 4 comments

Comments

@jayme-github
Copy link

I'm using jsonpatch as part of pykube to generate patches for kubernetes object definitions. The following (reduced) example generates a wrong patch for me:

original.json:

{
    "containers": [
        {
            "name": "fpm",
            "image": "eu.gcr.io/foo/bar:v1"
        },
        {
            "name": "fluentd",
            "image": "eu.gcr.io/foo/bar:v1"
        }
    ]
}

modified.json:

{
    "containers": [
        {
            "name": "fpm",
            "image": "eu.gcr.io/foo/bar:v2"
        },
        {
            "name": "fluentd",
            "image": "eu.gcr.io/foo/bar:v0"
        }
    ]
}

jsonpatch.make_patch() output:

[
    {
        "path": "/containers/1/image",
        "value": "eu.gcr.io/foo/bar:v0",
        "op": "replace"
    },
    {
        "path": "/containers/0",
        "value": {
            "image": "eu.gcr.io/foo/bar:v2",
            "name": "fpm"
        },
        "op": "replace"
    }
]
@stefankoegl
Copy link
Owner

Could you please clarify what exactly the issue is? On first glance, the patch seems to produce the correct result.

@jayme-github
Copy link
Author

If I remember correctly the problem was/is that for /containers/0 name and image are replaced instead of just the image. Updating thename field is not allowed by the API receiving the patch.

@stefankoegl
Copy link
Owner

Can you please provide a self-contained test case with assertions to clarify your case? When I create a patch between your original and modified files, and apply that patch onto original, I get modified.

>>> import jsonpatch
>>> original = {
...     "containers": [
...         {
...             "name": "fpm",
...             "image": "eu.gcr.io/foo/bar:v1"
...         },
...         {
...             "name": "fluentd",
...             "image": "eu.gcr.io/foo/bar:v1"
...         }
...     ]
... }
>>> 
>>> modified = {
...     "containers": [
...         {
...             "name": "fpm",
...             "image": "eu.gcr.io/foo/bar:v2"
...         },
...         {
...             "name": "fluentd",
...             "image": "eu.gcr.io/foo/bar:v0"
...         }
...     ]
... }
>>> patch = jsonpatch.make_patch(original, modified)
>>> result = patch.apply(original)
>>> result == modified
True

@stefankoegl
Copy link
Owner

Closing due to inactivity. Please feel free to comment, and I will reopen again.

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

2 participants