spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
919 stars 563 forks source link

PUT Request Issue with Composite Keys and OneToMany Relationship (Single Repo for Main Entity) #2398

Open t0bij opened 3 months ago

t0bij commented 3 months ago

Reproducer:

I've created a reproducer to demonstrate the issue. The repository is available here.

Summary

The main entity Movie has a OneToMany relation to Ratings. Ratings has a composite primary key with movieID and ratingPlatformId. Only the Movie entity has a REST repository, and IDs are exposed. A single movie API exists where GET works fine, but updating via PUT doesn't. Updating the ratings list gives unexpected results. For example:

Before:

{
   "id": 2,
   "name": "The Dark Knight",
   "ratings": [
      {
         "ratingPlatformId": 1,
         "score": 10
      }
   ]
}

PUT Request:

{
   "id": 2,
   "name": "The Dark Knight",
   "ratings": [
      {
         "ratingPlatformId": 2,
         "score": 2
      }
   ]
}

Expectation: The rating for ratingPlatformId 1 should be deleted, and a new one with ratingPlatformId 2 should be created.

Actual Result:

{
   "id": 2,
   "name": "The Dark Knight",
   "ratings": [
      {
         "ratingPlatformId": 1,
         "score": 2
      }
   ]
}

I debugged a bit and ratingPlatformId is not considered at all during the PUT operation and gets skipped here as it is an ID:

https://github.com/spring-projects/spring-data-rest/blob/aaadc344ab1bef4ed98cb0dbf6ca8ebd7c9262ff/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/DomainObjectReader.java#L688-L690

The only working setup is inspired by AresEkb from #2324 and involves:

Branches

t0bij commented 1 week ago

@mp911de, @odrotbohm - any chance you find the time to have a look on this issue? Thanks in advance. Just let me know if further information is required.