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
This commit is contained in:
Mark Paluch
2019-01-03 10:55:36 +01:00
committed by Christoph Strobl
parent 8ad16c1f23
commit 2da0ecbe72
2 changed files with 29 additions and 0 deletions

View File

@@ -127,6 +127,11 @@ public class QueryMapper {
continue;
}
if (isTypeKey(key)) {
result.put(key, BsonUtils.get(query, key));
continue;
}
if (isKeyword(key)) {
result.putAll(getMappedKeyword(new Keyword(query, key), entity));
continue;
@@ -629,6 +634,18 @@ public class QueryMapper {
return isKeyword(keys.iterator().next().toString());
}
/**
* Returns whether the given {@link String} is the type key.
*
* @param key
* @return
* @see MongoTypeMapper#isTypeKey(String)
* @since 2.2
*/
protected boolean isTypeKey(String key) {
return converter.getTypeMapper().isTypeKey(key);
}
/**
* Returns whether the given {@link String} is a MongoDB keyword. The default implementation will check against the
* set of registered keywords returned by {@link #getKeywords()}.

View File

@@ -55,6 +55,7 @@ import org.springframework.data.mongodb.core.mapping.TextScore;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.test.util.Assertions;
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import com.mongodb.BasicDBObject;
@@ -803,6 +804,17 @@ public class QueryMapperUnitTests {
assertThat(document.get("sample._id"), instanceOf(String.class));
}
@Test // DATAMONGO-2168
public void getMappedObjectShouldNotMapTypeHint() {
converter.setTypeMapper(new DefaultMongoTypeMapper("className"));
org.bson.Document update = new org.bson.Document("className", "foo");
org.bson.Document mappedObject = mapper.getMappedObject(update, context.getPersistentEntity(UserEntity.class));
Assertions.assertThat(mappedObject).containsEntry("className", "foo");
}
@Document
public class Foo {
@Id private ObjectId id;