Skip to content

Commit 10107c7

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-2168 - Do not map type key in QueryMapper.
QueryMapper now excludes the type key in during mapping and retains the value as-is. This change fixes an issue in which type keys were attempted to be mapped using the entity model. Type key resolution for _class failed silently while other type keys such as className failed in property path resolution and the failure became visible. Original Pull Request: #631
1 parent abe7876 commit 10107c7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public Document getMappedObject(Bson query, @Nullable MongoPersistentEntity<?> e
127127
continue;
128128
}
129129

130+
if (isTypeKey(key)) {
131+
result.put(key, BsonUtils.get(query, key));
132+
continue;
133+
}
134+
130135
if (isKeyword(key)) {
131136
result.putAll(getMappedKeyword(new Keyword(query, key), entity));
132137
continue;
@@ -605,6 +610,18 @@ protected boolean isNestedKeyword(Object candidate) {
605610
return isKeyword(keys.iterator().next().toString());
606611
}
607612

613+
/**
614+
* Returns whether the given {@link String} is the type key.
615+
*
616+
* @param key
617+
* @return
618+
* @see MongoTypeMapper#isTypeKey(String)
619+
* @since 2.2
620+
*/
621+
protected boolean isTypeKey(String key) {
622+
return converter.getTypeMapper().isTypeKey(key);
623+
}
624+
608625
/**
609626
* Returns whether the given {@link String} is a MongoDB keyword. The default implementation will check against the
610627
* set of registered keywords returned by {@link #getKeywords()}.

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.mongodb.core.query.BasicQuery;
5656
import org.springframework.data.mongodb.core.query.Criteria;
5757
import org.springframework.data.mongodb.core.query.Query;
58+
import org.springframework.data.mongodb.test.util.Assertions;
5859
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
5960

6061
import com.mongodb.BasicDBObject;
@@ -803,6 +804,17 @@ public void leavesNonObjectIdStringIdRepresentationUntouchedWhenReferencingIdPro
803804
assertThat(document.get("sample._id"), instanceOf(String.class));
804805
}
805806

807+
@Test // DATAMONGO-2168
808+
public void getMappedObjectShouldNotMapTypeHint() {
809+
810+
converter.setTypeMapper(new DefaultMongoTypeMapper("className"));
811+
812+
org.bson.Document update = new org.bson.Document("className", "foo");
813+
org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class));
814+
815+
Assertions.assertThat(mappedObject).containsEntry("className", "foo");
816+
}
817+
806818
@Document
807819
public class Foo {
808820
@Id private ObjectId id;

0 commit comments

Comments
 (0)