Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryMapper with annotations and does not properly applies PropertyValueConverter #4510

Closed
judby opened this issue Sep 21, 2023 · 2 comments
Closed
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug

Comments

@judby
Copy link

judby commented Sep 21, 2023

  • 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.

@christophstrobl
Copy link
Member

@judby thanks for reporting and the reproducer - we'll have a look.

@christophstrobl christophstrobl added type: bug A general bug in: mapping Mapping and conversion infrastructure and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 28, 2023
@judby
Copy link
Author

judby commented Sep 28, 2023

Thank you very much - happy to be able to help improving the product

mp911de added a commit that referenced this issue Feb 12, 2024
Refine nullability declarations and NPE guards. Reformat code.

See #4510
Original pull request: #4517
@mp911de mp911de changed the title Query defect in spring-data-mongodb QueryMapper with annotations and PropertyValueConverter QueryMapper with annotations and does not properly applies PropertyValueConverter Feb 12, 2024
@mp911de mp911de added this to the 4.3 M1 (2024.0.0) milestone Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants