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:
@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.
Reproducer:
I've created a reproducer to demonstrate the issue. The repository is available here.
Summary
The main entity
Movie
has aOneToMany
relation toRatings
.Ratings
has a composite primary key withmovieID
andratingPlatformId
. Only theMovie
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:
PUT Request:
Expectation: The rating for ratingPlatformId 1 should be deleted, and a new one with ratingPlatformId 2 should be created.
Actual Result:
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:
Using a
HashMap
for theOneToMany
ratings and writinggetRatings
andsetRatings
methods manually.Adding some AOP workaround for
RepositoryEntityController.putItemResource
to set the movie in the ratings.Branches
HashMap
and AOP workarounds.HashSet
and AOP, but adding list elements in the middle fails.HashSet
and without any AOP or workarounds; many tests fail.