spring-projects / spring-projects/spring-data-rest
RepositoryEntityController.deleteItemResource does not work with @IdClass composite key
@odrotbohm is already working on this.
Since Apr 17, 2023.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Defined a repository interface for CrudRepository<MyEntity, MyPkClass> with @RepositoryRestResource.
Relevant methods are exposed as endpoints.
Implemented a CustomIdConverter (implementing BackendIdConverter) in order to map a delimited string to MyPkClass.
POST and PATCH requests are working fine, but not DELETE.
Problem tracked down to the following RepositoryEntityController method.
@RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteItemResource(RootResourceInformation resourceInformation, @BackendId Serializable id,
ETag eTag) throws ResourceNotFoundException, HttpRequestMethodNotSupportedException {
resourceInformation.verifySupportedMethod(HttpMethod.DELETE, ResourceType.ITEM);
RepositoryInvoker invoker = resourceInformation.getInvoker();
Optional<Object> domainObj = invoker.invokeFindById(id);
return domainObj.map(it -> {
PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();
eTag.verify(entity, it);
publisher.publishEvent(new BeforeDeleteEvent(it));
invoker.invokeDeleteById(entity.getIdentifierAccessor(it).getIdentifier());
publisher.publishEvent(new AfterDeleteEvent(it));
return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}).orElseThrow(() -> new ResourceNotFoundException());
}
invoker.invokeFindById(id) retrieves the correct domainobject (line 417)
invoker.invokeDeleteById(entity.getIdentifierAccessor(it).getIdentifier()); (line 426) leads to an error, because "entity.getIdentifierAccessor(it).getIdentifier()" returns only the last "@Id"-annotated entity attribute. I verified this behaviour by changing the attribute order in my entity class. This is a primitive datatype and therefore can not be mapped to MyPkClass.
Maybe "invokeDeleteById()" should be called with "id" like "invokeFindById(id)" ?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.