ClassCastException in MappingMongoConverter when having Map<String, Object> as a field type.
See original GitHub issueAfter 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The java driver uses
org.bson.Document
to representMap
structures. the_flexFields
is actually not present in the source when reading from MongoDB. However theFlexDocumentListener
puts in aLinkedHashMap
via theafterLoad
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:
For the time being please
org.bson.Document
when adding map like structures to an existing document.Closing as duplicate of #3702