Browse Source

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
pull/640/head
Mark Paluch 7 years ago committed by Christoph Strobl
parent
commit
10107c7b81
  1. 17
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

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

@ -127,6 +127,11 @@ public class QueryMapper {
continue; continue;
} }
if (isTypeKey(key)) {
result.put(key, BsonUtils.get(query, key));
continue;
}
if (isKeyword(key)) { if (isKeyword(key)) {
result.putAll(getMappedKeyword(new Keyword(query, key), entity)); result.putAll(getMappedKeyword(new Keyword(query, key), entity));
continue; continue;
@ -605,6 +610,18 @@ public class QueryMapper {
return isKeyword(keys.iterator().next().toString()); 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 * 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()}. * set of registered keywords returned by {@link #getKeywords()}.

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

@ -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.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; 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 org.springframework.data.mongodb.test.util.BasicDbListBuilder;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
@ -803,6 +804,17 @@ public class QueryMapperUnitTests {
assertThat(document.get("sample._id"), instanceOf(String.class)); 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 @Document
public class Foo { public class Foo {
@Id private ObjectId id; @Id private ObjectId id;

Loading…
Cancel
Save