spring-projects / spring-projects/spring-data-rest

PATCH request for updating nested collections does not work correctly when there is deleted items [DATAREST-1533]

Open
#1,889 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Rasha Elsayed opened DATAREST-1533 and commented

I have a scenario where a parent entity "parent" has a collection of "child" entities (OneToMany-relation with CASCADE = all). The repository of the child entity is not exported. I have to update the "parent" and "children" in the same call (same transaction). So i am confronted with the following scenario

P1: name = parent1

ch1: id=1, name = child1

ch2: id=2, name = child2

ch3: id=3, name = child3

 

Now delete +child2+ and add new child name = +child4.+ and Send a PATCH request to persist. This results in the following:

ch1: id=1, name = child1

ch2: id=2, name = child4

ch3: id=3, name = child3

So the new child (child4) took the same id as the deleted one (child2).

If there is any foreign keys on the child entity, this will lead to wrong references. 

 

I found that inside the class DomainObjectReader, method handleArrayNode, that both collections, the one from the payload (array) and the one from the DB (collection) are read and iterated on to merge the items. In case of deletion, items will not be at the same index. Therfore it is not guranteed that an item is merged with its correct counterpart. Would it be an idea to merge two elements if their ids match? Just any mechanism making sure that the correct pair is merged.

 

I think the problem was mentioned under the issue https://jira.spring.io/browse/DATAREST-1012 (see last comment)


Affects: 3.3.1 (Neumann SR1)

1 votes, 1 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.