Skip to content

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

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

Closed
dinerotah opened this issue Jul 1, 2021 · 5 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@dinerotah
Copy link

dinerotah commented Jul 1, 2021

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 1, 2021
@christophstrobl
Copy link
Member

Thanks for reporting can you please add the domain types involved as well as the raw document representation. Ideally you have a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.
This would help us to better understand the issue.

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 1, 2021
@dinerotah
Copy link
Author

dinerotah commented Jul 1, 2021

I will try but in 3.1.x there was no cast to Bson and this the problem when using Object as value in a map.

if (typeHint.isMap()) { return (S) mapConverter.convert(this, (Bson) source, typeHint); }

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 1, 2021
@dinerotah
Copy link
Author

dinerotah commented Jul 5, 2021

minimal_sample.zip
Here is a minimal sample to reproduce the error. It works with 3.1.x but not with 3.2.2
Server port is 9999

In the following link, near the end of the article, you could find Postman collection with all crud ops.

@christophstrobl
Copy link
Member

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.

@mp911de
Copy link
Member

mp911de commented Jul 15, 2021

Closing as duplicate of #3702

@mp911de mp911de closed this as completed Jul 15, 2021
@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants