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

NullpointerException with PATCH and nested collections [DATAREST-1113]

Open
#1,474 0 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

Andrew Markwell opened DATAREST-1113 and commented

When performing a PATCH request with a 3-level nested graph of objects, a NullPointerException is encountered. Only the top-level JPA entity has a repository, and data the second 2 levels is inlined.

I've reproduced the bug with a small example on github: https://github.com/Spacemanbenny/sdr-patch-with-nested-collection @RepositoryRestResource(exported = false). run the PatchTest junit to reproduce. The junit doesn't fail, but the console output does show the NullPointerException.

Data structure is as follows:
[Company] --> [Employee] --> [Pen] ,
where each relation is OneToMany and unidirectional.

Steps to reproduce (in the Github example):
(a) Insert some starting data with Spring-Data JPA (not spring data rest)
1 company, 2 employees linked to companyy, 4 pens, 2 linked to first employee, 2 linked to second employee.

(b) Perform PATCH request containing:
2 employees:
First employee is not in the request (expect employee to be deleted and 2 pens to be deleted)
Second employee is unchanged
Third employee has been added with 1 new pen (expect 1 row added to employee and 1 row to pen)

What actually happens in this error:

Caused by: java.lang.NullPointerException: null
	at org.hibernate.collection.internal.PersistentBag.toArray(PersistentBag.java:284) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
	at java.util.ArrayList.<init>(ArrayList.java:177) ~[na:1.8.0_72]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.handleArrayNode(DomainObjectReader.java:324) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.handleArray(DomainObjectReader.java:303) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:244) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.handleArrayNode(DomainObjectReader.java:345) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.handleArray(DomainObjectReader.java:303) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:244) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	at org.springframework.data.rest.webmvc.json.DomainObjectReader.read(DomainObjectReader.java:87) ~[spring-data-rest-webmvc-2.6.5.RELEASE.jar:na]
	... 54 common frames omitted

Affects: 2.6.5 (Ingalls SR5)

Reference URL: https://github.com/Spacemanbenny/sdr-patch-with-nested-collection

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.