question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException in MappingMongoConverter when having Map<String, Object> as a field type.

See original GitHub issue

After upgrading to Spring Boot from 2.4.4 to 2.5.2(spring-data-mongodb 3.1.6 --> 3.2.2) I am having the following exception:

Caused by: java.lang.ClassCastException: java.util.LinkedHashMap incompatible with org.bson.conversions.Bson
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:1905)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1636)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:477)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:392)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:371)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument(MappingMongoConverter.java:341)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:277)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:273)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:102)
	at org.springframework.data.mongodb.core.MongoTemplate$ReadDocumentCallback.doWith(MongoTemplate.java:3178)
	at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2813)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2543)
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2525)
	at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:847)
	at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:428)
	at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAllById(SimpleMongoRepository.java:162)

In the domain object there is a the following property: protected Map<String, Object> _flexFields = new HashMap<>()

When MappingMongoConverter try to convert it in line 1905, the exception is thrown even the map is empty. The 3.1.x MappingMongoConverter implementation works fine. Please some fix.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christophstroblcommented, Jul 6, 2021

The java driver uses org.bson.Document to represent Map structures. the _flexFields is actually not present in the source when reading from MongoDB. However the FlexDocumentListener puts in a LinkedHashMap via the afterLoad event which then in turn leads to the error you see.

I admit the restriction in place is a kinda hard one and I’ll take this to the team to see if we want it to be softened to something like:

if (typeHint.isMap()) {
	if(source instanceof Bson) {
		return (S) mapConverter.convert(this, (Bson) source, typeHint);
	}
	if(source instanceof Map) {
		return (S) mapConverter.convert(this, new Document((Map<String,Object>)source), typeHint);
	}
	throw new IllegalArgumentException(String.format("Expected map like structure but found %s", source.getClass()));
}

For the time being please org.bson.Document when adding map like structures to an existing document.

0reactions
mp911decommented, Jul 15, 2021

Closing as duplicate of #3702

Read more comments on GitHub >

github_iconTop Results From Across the Web

`MappingMongoConverter` incorrectly processes an object ...
During that process, the property value of type org.bson.Document is incorrectly handled as a Map and all properties (fields) with an object ......
Read more >
java - Cast exception issue in map with values of type Object
My question is personDetailMap will return a object of Class Object then why its giving a cast exception of Boolean to String. java...
Read more >
changelog.txt - Spring
[DATAMONGO-602] Querying with $in operator on the id field of type ... 'key' in the MongoDB * [DATAMONGO-489] - ClassCastException when loading Map<String, ......
Read more >
Document (bson 4.5.0 API)
public class Document extends Object implements Map<String,Object>, Serializable, Bson ... Fields inherited from interface org.bson.conversions.
Read more >
[JAVA-2695] Add support for Map<String, Object> in PojoCodec
If I forget about Pojo and save new Document(map) or Document.parse(jsonStringRepresentationOfMap) - java mongo driver can recognize the types ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found