Skip to content

QueryMapper with annotations and does not properly applies PropertyValueConverter #4510

Closed
@judby

Description

@judby
  • JAVA 17
  • spring-boot 3.0.11
  • spring-data-mongodb 4.0.10

Introduced a @LocalDateAsString annotation used on selected LocalDate fields to have them mapped to string in mongo.

And a LocalDateToStringPropertyConverterwhich simply converts to/from String/LocalDate.

Everything is fine with mapping and simple queries but with eg "range" queries the QueryMapper calls the converter with a bson Document which obviously fails with a ClassCastException.

The annotation/converter implementations are trivial, code can be found on GitHub.

When a query like below is executed targeting a field that has aforementioned annotation:

final var query = Query.query(where("date").gte(LocalDate.parse("2023-09-20")).lte(LocalDate.parse("2023-09-22")));

final var testEntities = mongoTemplate.find(query, TestEntity.class);

an exception is thrown in QueryMapper since it tries to invoke the converter with the bson Document representing the gte and lte expressions:

java.lang.ClassCastException: class org.bson.Document cannot be cast to class java.time.LocalDate (org.bson.Document is in unnamed module of loader 'app'; java.time.LocalDate is in module java.base of loader 'bootstrap')

	at com.udby.defect.spring.data.mongodb.converter.query.simple.LocalDateToStringPropertyConverter.write(LocalDateToStringPropertyConverter.java:10)
	at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedValue(QueryMapper.java:450)
	at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:339)
	at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:166)
	at org.springframework.data.mongodb.core.QueryOperations$QueryContext.getMappedQuery(QueryOperations.java:350)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2422)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2411)
	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:815)
	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:805)
	at com.udby.defect.spring.data.mongodb.converter.query.simple.SpringDefectInQueryMapperTest.fail_LocalDate_between(SpringDefectInQueryMapperTest.java:36)
...

Complete simple setup can be found at GitHub.

Metadata

Metadata

Labels

in: mappingMapping and conversion infrastructuretype: bugA general bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions