Skip to content

AfterConvertEvent is not published when using custom methods in repository interface [DATAMONGO-1823] #2726

@spring-projects-issues

Description

@spring-projects-issues

Matúš Sekáč opened DATAMONGO-1823 and commented

Suppose basic Spring Data Mongo Example like https://spring.io/guides/gs/accessing-data-mongodb/ and simple MongoEventListener

package hello;

import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
import org.springframework.stereotype.Component;

@Component
public class CustomerAfterConvertListener extends AbstractMongoEventListener<Customer> {

	@Override
	public void onAfterConvert(AfterConvertEvent<Customer> event) {
		super.onAfterConvert(event);
		System.out.println("Converted " + event.getSource());
	}
}

When using Spring Data MongoDb up to version 2.0.0.M4, the output is:

Customers found with findAll():
-------------------------------
Converted Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Converted Customer[id=5a09b785290ee41a50b0dd61, firstName='Bob', lastName='Smith']
Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Customer[id=5a09b785290ee41a50b0dd61, firstName='Bob', lastName='Smith']

Customer found with findByFirstName('Alice'):
--------------------------------
Converted Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Customers found with findByLastName('Smith'):
--------------------------------
Converted Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Converted Customer[id=5a09b785290ee41a50b0dd61, firstName='Bob', lastName='Smith']
Customer[id=5a09b785290ee41a50b0dd60, firstName='Alice', lastName='Smith']
Customer[id=5a09b785290ee41a50b0dd61, firstName='Bob', lastName='Smith']

From version 2.0.0.RC1 higher, the output is:

Customers found with findAll():
-------------------------------
Converted Customer[id=5a09b847290ee4281ce2dd0c, firstName='Alice', lastName='Smith']
Converted Customer[id=5a09b847290ee4281ce2dd0d, firstName='Bob', lastName='Smith']
Customer[id=5a09b847290ee4281ce2dd0c, firstName='Alice', lastName='Smith']
Customer[id=5a09b847290ee4281ce2dd0d, firstName='Bob', lastName='Smith']

Customer found with findByFirstName('Alice'):
--------------------------------
Customer[id=5a09b847290ee4281ce2dd0c, firstName='Alice', lastName='Smith']
Customers found with findByLastName('Smith'):
--------------------------------
Customer[id=5a09b847290ee4281ce2dd0c, firstName='Alice', lastName='Smith']
Customer[id=5a09b847290ee4281ce2dd0d, firstName='Bob', lastName='Smith']

The event does not get published when calling any method created in repository interface. It does get published when using findAll(), findById(), etc...

This is probably caused by inverted nullcheck on emitting event, in

org.springframework.data.mongodb.core.MongoTemplate.ProjectingReadCallback.doWith(...)
Object result = targetType.isInterface() ? projectionFactory.createProjection(targetType, source) : source;

if (result == null) {
	maybeEmitEvent(new AfterConvertEvent<>(object, result, collectionName));
}

return (T) result;

Affects: 2.0.1 (Kay SR1)

Referenced from: pull request #517

Backported to: 2.0.2 (Kay SR2)

Metadata

Metadata

Labels

in: coreIssues in core supporttype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions