DATAMONGO-969 - Fixed nested id handling in SpringDataMongodbSerializer.
SpringDataMongodbSerializer now defensively triggers mapping of the DBObject created by the default serializer. This asserts that ids buried in nested structures like { "_id" : { "$in" : ["x", "y"] } } are converted correctly.
Original pull request: #202.
This commit is contained in:
committed by
Oliver Gierke
parent
0f487c10ba
commit
322a7cf033
@@ -41,6 +41,8 @@ import com.mysema.query.types.PathType;
|
||||
*/
|
||||
class SpringDataMongodbSerializer extends MongodbSerializer {
|
||||
|
||||
private final String ID_KEY = "_id";
|
||||
|
||||
private final MongoConverter converter;
|
||||
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
|
||||
private final QueryMapper mapper;
|
||||
@@ -84,8 +86,8 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
|
||||
@Override
|
||||
protected DBObject asDBObject(String key, Object value) {
|
||||
|
||||
if ("_id".equals(key)) {
|
||||
return super.asDBObject(key, mapper.convertId(value));
|
||||
if (ID_KEY.equals(key)) {
|
||||
return mapper.getMappedObject(super.asDBObject(key, value), null);
|
||||
}
|
||||
|
||||
return super.asDBObject(key, value instanceof Pattern ? value : converter.convertToMongoType(value));
|
||||
|
||||
@@ -990,4 +990,12 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
|
||||
assertThat(repository.findOne(QPerson.person.creator.eq(user)), is(dave));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-969
|
||||
*/
|
||||
@Test
|
||||
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
|
||||
assertThat(repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))), containsInAnyOrder(dave, carter));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2013 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,8 +17,10 @@ package org.springframework.data.mongodb.repository.support;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.repository.QAddress;
|
||||
import org.springframework.data.mongodb.repository.QPerson;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mysema.query.types.expr.BooleanOperation;
|
||||
@@ -43,6 +46,7 @@ import com.mysema.query.types.path.StringPath;
|
||||
* Unit tests for {@link SpringDataMongodbSerializer}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SpringDataMongodbSerializerUnitTests {
|
||||
@@ -132,6 +136,41 @@ public class SpringDataMongodbSerializerUnitTests {
|
||||
assertThat(path, is("0"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-969
|
||||
*/
|
||||
@Test
|
||||
public void shouldConvertObjectIdEvenWhenNestedInOperatorDbObject() {
|
||||
|
||||
ObjectId value = new ObjectId("53bb9fd14438765b29c2d56e");
|
||||
DBObject serialized = serializer.asDBObject("_id", new BasicDBObject("$ne", value.toString()));
|
||||
|
||||
DBObject _id = getAsDBObject(serialized, "_id");
|
||||
ObjectId $ne = getTypedValue(_id, "$ne", ObjectId.class);
|
||||
assertThat($ne, is(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-969
|
||||
*/
|
||||
@Test
|
||||
public void shouldConvertCollectionOfObjectIdEvenWhenNestedInOperatorDbObject() {
|
||||
|
||||
ObjectId firstId = new ObjectId("53bb9fd14438765b29c2d56e");
|
||||
ObjectId secondId = new ObjectId("53bb9fda4438765b29c2d56f");
|
||||
|
||||
BasicDBList objectIds = new BasicDBList();
|
||||
objectIds.add(firstId.toString());
|
||||
objectIds.add(secondId.toString());
|
||||
|
||||
DBObject serialized = serializer.asDBObject("_id", new BasicDBObject("$in", objectIds));
|
||||
|
||||
DBObject _id = getAsDBObject(serialized, "_id");
|
||||
Object[] $in = getTypedValue(_id, "$in", Object[].class);
|
||||
|
||||
assertThat($in, Matchers.<Object> arrayContaining(firstId, secondId));
|
||||
}
|
||||
|
||||
class Address {
|
||||
String id;
|
||||
String street;
|
||||
|
||||
Reference in New Issue
Block a user