diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 3bd2e52f0..c5ff205a4 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ Spring Data MongoDB - 2.8.0 + 2.8.2 1.0 1.0.0.GA 1.1.3 diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java index 43677a238..023603e6f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java @@ -1,11 +1,11 @@ /* - * Copyright (c) 2011 by the original author(s). + * Copyright 2011-2013 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. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.data.mongodb.core.convert; import java.math.BigInteger; @@ -33,8 +32,8 @@ import org.springframework.data.mongodb.core.convert.MongoConverters.StringToObj * Base class for {@link MongoConverter} implementations. Sets up a {@link GenericConversionService} and populates basic * converters. Allows registering {@link CustomConversions}. * - * @author Jon Brisbin - * @author Oliver Gierke ogierke@vmware.com + * @author Jon Brisbin + * @author Oliver Gierke */ public abstract class AbstractMongoConverter implements MongoConverter, InitializingBean { @@ -94,6 +93,14 @@ public abstract class AbstractMongoConverter implements MongoConverter, Initiali conversions.registerConvertersIn(conversionService); } + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.convert.MongoWriter#convertToMongoType(java.lang.Object) + */ + public Object convertToMongoType(Object obj) { + return convertToMongoType(obj, null); + } + /* * (non-Javadoc) * @see org.springframework.data.mongodb.core.core.convert.MongoConverter#getConversionService() diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index a7e1e3605..2151e227c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 by the original author(s). + * Copyright 2011-2013 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -813,8 +813,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App return rootList; } + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.convert.MongoWriter#convertToMongoType(java.lang.Object, org.springframework.data.util.TypeInformation) + */ @SuppressWarnings("unchecked") - public Object convertToMongoType(Object obj) { + public Object convertToMongoType(Object obj, TypeInformation typeInformation) { if (obj == null) { return null; @@ -861,7 +865,12 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App DBObject newDbo = new BasicDBObject(); this.write(obj, newDbo); - return removeTypeInfoRecursively(newDbo); + + if (typeInformation == null) { + return removeTypeInfoRecursively(newDbo); + } + + return !obj.getClass().equals(typeInformation.getType()) ? newDbo : removeTypeInfoRecursively(newDbo); } public BasicDBList maybeConvertList(Iterable source) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java index 4c04452bb..6d25f84a2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2013 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,6 +17,7 @@ package org.springframework.data.mongodb.core.convert; import org.springframework.data.convert.EntityWriter; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; +import org.springframework.data.util.TypeInformation; import com.mongodb.DBObject; import com.mongodb.DBRef; @@ -35,11 +36,21 @@ public interface MongoWriter extends EntityWriter { * Converts the given object into one Mongo will be able to store natively. If the given object can already be stored * as is, no conversion will happen. * - * @param obj + * @param obj can be {@literal null}. * @return */ Object convertToMongoType(Object obj); + /** + * Converts the given object into one Mongo will be able to store natively but retains the type information in case + * the given {@link TypeInformation} differs from the given object type. + * + * @param obj can be {@literal null}. + * @param typeInformation can be {@literal null}. + * @return + */ + Object convertToMongoType(Object obj, TypeInformation typeInformation); + /** * Creates a {@link DBRef} to refer to the given object. * diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java index 98a06f329..7c0a713fb 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 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. @@ -28,6 +28,7 @@ import org.springframework.data.mongodb.core.geo.Distance; import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.repository.query.ParameterAccessor; +import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -85,12 +86,12 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { return delegate.getSort(); } - /* (non-Javadoc) - * @see org.springframework.data.repository.query.ParameterAccessor#getBindableParameter(int) - */ + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.ParameterAccessor#getBindableValue(int) + */ public Object getBindableValue(int index) { - - return getConvertedValue(delegate.getBindableValue(index)); + return getConvertedValue(delegate.getBindableValue(index), null); } /* @@ -101,7 +102,8 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { return delegate.getMaxDistance(); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.data.mongodb.repository.MongoParameterAccessor#getGeoNearLocation() */ public Point getGeoNearLocation() { @@ -111,11 +113,12 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { /** * Converts the given value with the underlying {@link MongoWriter}. * - * @param value + * @param value can be {@literal null}. + * @param typeInformation can be {@literal null}. * @return */ - private Object getConvertedValue(Object value) { - return writer.convertToMongoType(value); + private Object getConvertedValue(Object value, TypeInformation typeInformation) { + return writer.convertToMongoType(value, typeInformation == null ? null : typeInformation.getActualType()); } /* @@ -186,7 +189,7 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor { } } - return getConvertedValue(next); + return getConvertedValue(next, property.getTypeInformation()); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java index 98f758f37..81e9bdaff 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoOperationsUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2013 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. @@ -34,6 +34,7 @@ import org.springframework.data.mongodb.core.geo.Point; import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.mongodb.core.query.NearQuery; +import org.springframework.data.util.TypeInformation; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; @@ -78,7 +79,7 @@ public abstract class MongoOperationsUnitTests { return null; } - public Object convertToMongoType(Object obj) { + public Object convertToMongoType(Object obj, TypeInformation typeInformation) { return null; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java index 99cd7bc6d..0ae7645fb 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-2013 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. @@ -523,4 +523,23 @@ public abstract class AbstractPersonRepositoryIntegrationTests { assertThat(result, hasSize(1)); assertThat(result, hasItem(oliver)); } + + /** + * @see DATAMONGO-600 + */ + @Test + public void readsDocumentsWithNestedPolymorphismCorrectly() { + + UsernameAndPassword usernameAndPassword = new UsernameAndPassword(); + usernameAndPassword.username = "dave"; + usernameAndPassword.password = "btcs"; + + dave.credentials = usernameAndPassword; + + repository.save(dave); + + List result = repository.findByCredentials(usernameAndPassword); + assertThat(result, hasSize(1)); + assertThat(result, hasItem(dave)); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Credentials.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Credentials.java new file mode 100644 index 000000000..2f58f1027 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Credentials.java @@ -0,0 +1,24 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +/** + * + * @author Oliver Gierke + */ +public interface Credentials { + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java index 34d6f2366..2a86e1c4f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2013 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. @@ -54,6 +54,8 @@ public class Person extends Contact { @DBRef User creator; + Credentials credentials; + public Person() { this(null, null); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java index bf35a99b6..7b7424055 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2013 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. @@ -192,4 +192,10 @@ public interface PersonRepository extends MongoRepository, Query */ List findByLastnameNot(String lastname); + /** + * @see DATAMONGO-600 + * @param credentials + * @return + */ + List findByCredentials(Credentials credentials); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UsernameAndPassword.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UsernameAndPassword.java new file mode 100644 index 000000000..205c8120f --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/UsernameAndPassword.java @@ -0,0 +1,25 @@ +/* + * Copyright 2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.repository; + +/** + * @author Oliver Gierke + */ +public class UsernameAndPassword implements Credentials { + + String username; + String password; +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java index 8110eb7be..0b1ffabe4 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-2013 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. @@ -49,6 +49,7 @@ import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; import org.springframework.data.repository.query.parser.PartTree; +import org.springframework.data.util.TypeInformation; /** * Unit test for {@link MongoQueryCreator}. @@ -74,7 +75,7 @@ public class MongoQueryCreatorUnitTests { public Object answer(InvocationOnMock invocation) throws Throwable { return invocation.getArguments()[0]; } - }).when(converter).convertToMongoType(any()); + }).when(converter).convertToMongoType(any(), Mockito.any(TypeInformation.class)); } @Test