Skip to content

can't update nested entities while doing patch #4061

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

Closed
VincentDugard opened this issue Feb 15, 2021 · 2 comments
Closed

can't update nested entities while doing patch #4061

VincentDugard opened this issue Feb 15, 2021 · 2 comments
Assignees

Comments

@VincentDugard
Copy link

API Platform version(s) affected: 2.6.2

Description
After upgrading from api platform 2.5.7 to 2.6.2, updating nested entites using patch request (application/merge-patch+json) result in inserting new nested entities even if id attribute is provided.

How to reproduce
https://github.com/Adrillex/api-platform-2.6-patch-bug

The above project is a simple project with only two entities "resource" and "subresource".
to reproduce the bug :

  • post a resource containing a subresource :
{
    "name": "test",
    "subResources": [
        {
            "name": "test1"
        }
    ]
}
  • try to PATCH this resource and modify the subresource
    {{baseUrl}}/resources/1
    Content-Type: application/merge-patch+json
{
    "name": "test",
    "subResources": [
        {
            "id": 1,
            "name": "test2"
        }
    ]
}

It result in inserting a new subresource instead of updating the old one (like if the id property wasn't provided) :

{
    "@context": "/contexts/Resource",
    "@id": "/resources/1",
    "@type": "Resource",
    "id": 1,
    "name": "test",
    "subResources": [
        {
            "@id": "/sub_resources/2",
            "@type": "SubResource",
            "id": 2,
            "name": "test2"
        }
    ]
}

I noticed that If I do the exact same request using PUT, it works. BTW actually the PUT behavior seems to be exactly the one PATCH should be since I can use PUT to modify a resource without replacing it entirely.
I can also do a POST resource while modifying the subresource, it also works.

It looks like the problem is only with PATCH

@Charles-Emmanuel-DEZANDEE

API Platform version(s) affected: 2.6.2

Description
After upgrading from api platform 2.5.7 to 2.6.2, updating nested entites using patch request (application/merge-patch+json) result in inserting new nested entities even if id attribute is provided.

How to reproduce
https://github.com/Adrillex/api-platform-2.6-patch-bug

The above project is a simple project with only two entities "resource" and "subresource".
to reproduce the bug :

  • post a resource containing a subresource :
{
    "name": "test",
    "subResources": [
        {
            "name": "test1"
        }
    ]
}
  • try to PATCH this resource and modify the subresource
    {{baseUrl}}/resources/1
    Content-Type: application/merge-patch+json
{
    "name": "test",
    "subResources": [
        {
            "id": 1,
            "name": "test2"
        }
    ]
}

It result in inserting a new subresource instead of updating the old one (like if the id property wasn't provided) :

{
    "@context": "/contexts/Resource",
    "@id": "/resources/1",
    "@type": "Resource",
    "id": 1,
    "name": "test",
    "subResources": [
        {
            "@id": "/sub_resources/2",
            "@type": "SubResource",
            "id": 2,
            "name": "test2"
        }
    ]
}

I noticed that If I do the exact same request using PUT, it works. BTW actually the PUT behavior seems to be exactly the one PATCH should be since I can use PUT to modify a resource without replacing it entirely.
I can also do a POST resource while modifying the subresource, it also works.

It looks like the problem is only with PATCH

+1
I have the same problem
it's work when I'm use : "id" : "/sub_resources/2" for the subressource in PATCH method.
but in PUT method "@id" : "/sub_resources/2" works.
it's magic.

@soyuka
Copy link
Member

soyuka commented Mar 2, 2021

The behavior explained at https://tools.ietf.org/html/rfc7386#section-3 shows that arrays must be replaced. Therefore this is the sole implementation of the merge-patch specification. The JSON PATCH specification (that is not implemented yet) may help solve that issue (https://tools.ietf.org/html/rfc6902#appendix-A.2). I'm closing this issue and we will follow that at #759

@soyuka soyuka closed this as completed Mar 2, 2021
@soyuka soyuka removed the bug label Mar 2, 2021
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

4 participants