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

JSON Patch to add/replace child entity collection fails [DATAREST-996]

Open
#1,359 2 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

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

Description

Tommy Ludwig opened DATAREST-996 and commented

The sample project has an integration test to reproduce this. The AggregateChildUpdateSampleApplicationTests.canReplaceCommentsWithPatch test demonstrates the issue and results in the following exception.

Essentially, when trying to update a collection of child entities that belongs to an aggregate root (child entities do not have their own repositories) with JSON Patch, Spring Data REST since Hopper (it seems) gives an error. With Gosling (Spring Boot 1.3.x), this does work, though. Notably different is the upgrade from Hibernate 4 to 5, which may be related.

Caused by: org.springframework.data.rest.webmvc.json.patch.PatchException: Could not read [{"content":"Cool post!"}] into class org.hibernate.collection.internal.PersistentSet!
	at org.springframework.data.rest.webmvc.json.patch.JsonLateObjectEvaluator.evaluate(JsonLateObjectEvaluator.java:47) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.patch.PatchOperation.evaluateValueFromTarget(PatchOperation.java:177) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.patch.AddOperation.perform(AddOperation.java:43) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.patch.Patch.apply(Patch.java:64) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyPatch(JsonPatchHandler.java:91) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.config.JsonPatchHandler.apply(JsonPatchHandler.java:83) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:198) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	... 53 common frames omitted
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session
	at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
	at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
	at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
	at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:308) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:259) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:26) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3770) ~[jackson-databind-2.8.6.jar:2.8.6]
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2099) ~[jackson-databind-2.8.6.jar:2.8.6]
	at org.springframework.data.rest.webmvc.json.patch.JsonLateObjectEvaluator.evaluate(JsonLateObjectEvaluator.java:45) ~[spring-data-rest-webmvc-2.6.0.RELEASE.jar:na]
	... 59 common frames omitted

Affects: 2.6 GA (Ingalls), 2.5.7 (Hopper SR7)

Reference URL: https://github.com/shakuzen/aggregate-child-update-sample

4 votes, 7 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.