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

Non-linked association overwritten on PATCH update [DATAREST-571]

Open
#945 4 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Ryan Evans opened DATAREST-571 and commented

When processing a PATCH update (save) for a repository, any non-linked associations for the entity being updated aren't merged correctly, causing the entire associated entity to be replaced (PUT behavior) instead of merged.

This issue did not exist in 2.2.1.

It looks as if the problem is around line 185 in org.springframework.data.rest.webmvc.json.DomainObjectReader#doMerge(...):

			if (child.isObject()) {

				if (associationLinks.isLinkableAssociation(property)) {
					continue;
				}

				BeanWrapper<T> wrapper = BeanWrapper.create(target, null);
				Object nested = wrapper.getProperty(property);

				if (nested != null) {
                                        // *** i.remove() is not called here ***
					doMerge((ObjectNode) child, nested, mapper);
				}

			}
		}

                // *** the associated object is overwritten by a NEW, empty object here ***
		return mapper.readerForUpdating(target).readValue(root);

Contrast this with the code from 2.2.1:

			} else if (child.isObject()) {

				PersistentProperty<?> property = findProperty(existingObject, entry.getKey(), mapper);

				if (property == null || associationLinks.isLinkableAssociation(property)) {
					continue;
				}

				BeanWrapper<T> wrapper = BeanWrapper.create(existingObject, null);
				Object nested = wrapper.getProperty(property);

				if (nested != null) {

					// Only remove the JsonNode if the object already exists. Otherwise it will be instantiated when the parent
					// gets deserialized.

                                        // *** i.remove is called, removing the child node from so it isn't processed again below ***
					i.remove();
					merge((ObjectNode) child, nested, mapper);
				}
			}

Affects: 2.2.2 (Evans SR2), 2.3 GA (Fowler)

1 votes, 3 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.