DATAMONGO-2101 - Fix DBObject to GeoJson conversion.

Querydsl still wraps MongoDB data in DBObject which causes trouble with the registered converters that deal with Document to entity conversion. Therefore we now try to extract the argument map from the DBObject transferring it to Document in order to have the converters kick in where applicable.

Original pull request: #614.
This commit is contained in:
Christoph Strobl
2018-10-02 10:39:32 +02:00
committed by Mark Paluch
parent 99a4661e81
commit 6720967e19
3 changed files with 32 additions and 42 deletions

View File

@@ -50,6 +50,7 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
import org.springframework.data.mongodb.util.BsonUtils;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
@@ -207,6 +208,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
if (conversions.hasCustomReadTarget(bson.getClass(), rawType)) {
return conversionService.convert(bson, rawType);
} else if (bson instanceof DBObject && conversions.hasCustomReadTarget(Document.class, rawType)) {
return conversionService.convert(new Document(BsonUtils.asMap(bson)), rawType);
}
if (DBObject.class.isAssignableFrom(rawType)) {

View File

@@ -15,18 +15,28 @@
*/
package org.springframework.data.mongodb.repository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.geo.GeoJson;
import com.querydsl.core.annotations.QueryEmbeddable;
/**
* @author Oliver Gierke
* @author Christoph Strobl
*/
@QueryEmbeddable
@Getter
@Setter
public class Address {
private String street;
private String zipCode;
private String city;
private GeoJson location;
protected Address() {
}
@@ -41,46 +51,4 @@ public class Address {
this.zipCode = zipcode;
this.city = city;
}
/**
* @return the street
*/
public String getStreet() {
return street;
}
/**
* @param street the street to set
*/
public void setStreet(String street) {
this.street = street;
}
/**
* @return the zipCode
*/
public String getZipCode() {
return zipCode;
}
/**
* @param zipCode the zipCode to set
*/
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
}

View File

@@ -28,6 +28,8 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.repository.Address;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.QPerson;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
@@ -99,4 +101,21 @@ public class QuerydslMongoPredicateExecutorIntegrationTests {
public void findOneWithPredicateThrowsExceptionForNonUniqueResults() {
repository.findOne(person.firstname.contains("e"));
}
@Test // DATAMONGO-2101
public void readEntityWithGeoJsonValue() {
Address adr1 = new Address("Hauptplatz", "4020", "Linz");
adr1.setLocation(new GeoJsonPoint(48.3063548, 14.2851337));
Person person1 = new Person("Max", "The Mighty");
person1.setAddress(adr1);
operations.save(person1);
List<Person> result = new SpringDataMongodbQuery<>(operations, Person.class).where(person.firstname.eq("Max"))
.fetch();
assertThat(result).containsExactly(person1);
}
}