Skip to content

RepositoryEntityController.saveAndReturn calls AfterSaveEvent with wrong domain object [DATAREST-847] #1218

@spring-projects-issues

Description

@spring-projects-issues

Marc Zampetti opened DATAREST-847 and commented

The saveAndReturn method has the following code to handle the Repository Event logic.

publisher.publishEvent(new BeforeSaveEvent(domainObject));
Object obj = invoker.invokeSave(domainObject);
publisher.publishEvent(new AfterSaveEvent(domainObject));

but I think this is wrong. It is using the provided domainObject for the AfterSaveEvent, instead of the obj returned by the invoker. From what I can tell, the expectation is that the After events should be supplied the object with its current state (i.e. after the change has been applied). This is further born out by the behavior of the createAndReturn method, which uses:

publisher.publishEvent(new BeforeCreateEvent(domainObject));
Object savedObject = invoker.invokeSave(domainObject);
publisher.publishEvent(new AfterCreateEvent(savedObject));

Here it is using the returned object (savedObject in this case). This gets even more confusing in the case of a REST PUT call, since that method first determines if the request should create a new item or update an existing item. Granted, since the contract of PUT is to always supply a complete object, the impact is less then a PATCH case, if one is using Generated fields or has EntityListeners that affect the object, those changes would be hidden to the AfterSaveEvent.


Affects: 2.3.2 (Fowler SR2), 2.4.4 (Gosling SR4), 2.5.2 (Hopper SR2)

Backported to: 2.5.3 (Hopper SR3), 2.4.5 (Gosling SR5)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions