spring-data-mongodb
spring-data-mongodb copied to clipboard
QueryMapper floods log with "Could not map xxx. Maybe a fragment in yyy is a simple type" INFO messages
This is a cross-post from: https://stackoverflow.com/q/73115112/1097451 with some minor modifications:
This message started popping up after upgrading Spring Data MongoDB a few months ago.
2022-07-25 22:27:33.313 INFO 236203 --- [ main] o.s.d.mongodb.core.convert.QueryMapper : Could not map 'Account.user.favorites'. Maybe a fragment in 'User -> String' is considered a simple type. Mapper continues with user.favorites.
The corresponding minimal working example is:
@Document
class Account(val user: User)
@Document
class User(_favorites: MutableList<String>)
{
@Field("favorites")
private val _favorites: MutableList<String> = _favorites
val favorites: List<String>
get() = _favorites
}
@SpringBootApplication
class DemoApplication(val mongoOperations: MongoOperations): CommandLineRunner
{
override fun run(vararg args: String?)
{
mongoOperations.save(Account(User(mutableListOf("a", "b", "c"))))
mongoOperations.find(
Query.query(where("user.favorites").isEqualTo(listOf("a", "b", "c"))),
Account::class.java)
}
}
fun main(args: Array<String>)
{
runApplication<DemoApplication>(*args)
}
Spring logs this at the INFO level, so apparently, it does not mean anything wrong, yet the tone indicates that Spring is struggling to understand my code.
Furthermore, the message is logged every time a query is executed — thousands of times.
Note that according to the log message, Spring thinks the types in Account.user.favorites are User -> String, but in reality they are User -> List.
What are the implications of this message? Isn't this supposed to be a WARNING?
Does my code contain an incorrect type mapping configuration? The reference documentation is not very clear on mapping Kotlin classes.
Thanks for bringing this up - does this only appear using Kotlin?
I will try to write equivalent Java code and post results. Thanks!
It took me some time since I forgot how to write Java. Thanks, Kotlin! :)
The Java example gives the same message:
@Document
class Account
{
public User user;
public Account(User user)
{
this.user = user;
}
}
@Document
class User
{
User(List<String> _favorites)
{
this._favorites = _favorites;
}
@Field("favorites")
private List<String> _favorites;
@Transient
List<String> favorites = _favorites;
}
@SpringBootApplication
class DemoApplication implements CommandLineRunner
{
@Autowired
MongoOperations mongoOperations;
@Override
public void run(String... args) throws Exception
{
List<String> l = Arrays.asList("a", "b", "c");
mongoOperations.save(new Account(new User(l)));
mongoOperations.find(Query.query(where("user.favorites").is(l)), Account.class);
}
}
public class Main
{
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Thank you @martin-drozdik. We'll have a look.
My bad, could have spotted this earlier. Criteria is intended to operate upon the java domain model which is using _favorites. Therefore where("user._favorites")... will allow property path resolution.
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.