spring-data-rest icon indicating copy to clipboard operation
spring-data-rest copied to clipboard

HandleBeforeLinkSave method returns old linked entity [DATAREST-1241]

Open spring-projects-issues opened this issue 7 years ago • 3 comments

Abhinav Sonkar opened DATAREST-1241 and commented

The method annotated with @HandleBeforeLinkSave takes 2 arguments - entity which is updated and the linked object being modified.

@HandleBeforeLinkSave
public void handleBeforeLinkSave(Object source, Object linked) {
   ...
}

In case of a PATCH or POST call, the linked object is a PersistentSet with both old and new values as these are augmenting HTTP methods.

But in case of a PUT call, the linked object is a PersistentSet with only old value. The new value passed in the request is not accessible. This makes it impossible to perform validations and/or business logic based on the new value of linked object.

Currently I am not aware of any workarounds to this issue.

After inspecting the Spring Data Rest  code I think the following line in RepositoryPropertyReferenceController.java:

publisher.publishEvent(new BeforeLinkSaveEvent(prop.accessor.getBean(), prop.propertyValue));

 must be changed to:

publisher.publishEvent(new BeforeLinkSaveEvent(prop.accessor.getBean(), prop.accessor.getProperty(prop.property)));

The same issue would affect the AfterLinkSaveEvent so below line:

publisher.publishEvent(new AfterLinkSaveEvent(result, prop.propertyValue));

should also be changed to:

publisher.publishEvent(new AfterLinkSaveEvent(result, prop.accessor.getProperty(prop.property)));

 

 


Affects: 2.6.11 (Ingalls SR11)

Referenced from: commits https://github.com/spring-projects/spring-data-rest/commit/79cbc1e6682adab73530a0d6102bf983af3cde30, https://github.com/spring-projects/spring-data-rest/commit/23b66fafdde3785a43d1880322db624a54949694

5 votes, 6 watchers

spring-projects-issues avatar May 03 '18 22:05 spring-projects-issues

mathomas commented

I'm being bitten by the same issue, which makes it impossible for me to handle the created application event and send a websocket message with all the needed info.  I had independently thought of the same solution presented in the main ticket, and it also applies to link delete events

spring-projects-issues avatar Sep 20 '18 15:09 spring-projects-issues

Doogiemuc commented

+1   I stumbled over the same problem.   Looks like the code has changed in current version of spring. Now it looks like that @HandleBeforeLinkSave annotated method receives the NEW value as second parameter.  And only the new value when POSTing

spring-projects-issues avatar Jun 20 '19 09:06 spring-projects-issues

Is there any update on this? We are affected as well?

amnonkhen avatar Dec 08 '21 11:12 amnonkhen