Skip to content

Commit 6720967

Browse files
christophstroblmp911de
authored andcommitted
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.
1 parent 99a4661 commit 6720967

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent;
5151
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
5252
import org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent;
53+
import org.springframework.data.mongodb.util.BsonUtils;
5354
import org.springframework.data.util.ClassTypeInformation;
5455
import org.springframework.data.util.TypeInformation;
5556
import org.springframework.lang.Nullable;
@@ -207,6 +208,8 @@ private <S extends Object> S read(TypeInformation<S> type, @Nullable Bson bson,
207208

208209
if (conversions.hasCustomReadTarget(bson.getClass(), rawType)) {
209210
return conversionService.convert(bson, rawType);
211+
} else if (bson instanceof DBObject && conversions.hasCustomReadTarget(Document.class, rawType)) {
212+
return conversionService.convert(new Document(BsonUtils.asMap(bson)), rawType);
210213
}
211214

212215
if (DBObject.class.isAssignableFrom(rawType)) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Address.java

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18+
import lombok.Getter;
19+
import lombok.Setter;
20+
21+
import org.springframework.data.mongodb.core.geo.GeoJson;
22+
1823
import com.querydsl.core.annotations.QueryEmbeddable;
1924

2025
/**
2126
* @author Oliver Gierke
27+
* @author Christoph Strobl
2228
*/
2329
@QueryEmbeddable
30+
@Getter
31+
@Setter
2432
public class Address {
2533

2634
private String street;
2735
private String zipCode;
2836
private String city;
2937

38+
private GeoJson location;
39+
3040
protected Address() {
3141

3242
}
@@ -41,46 +51,4 @@ public Address(String street, String zipcode, String city) {
4151
this.zipCode = zipcode;
4252
this.city = city;
4353
}
44-
45-
/**
46-
* @return the street
47-
*/
48-
public String getStreet() {
49-
return street;
50-
}
51-
52-
/**
53-
* @param street the street to set
54-
*/
55-
public void setStreet(String street) {
56-
this.street = street;
57-
}
58-
59-
/**
60-
* @return the zipCode
61-
*/
62-
public String getZipCode() {
63-
return zipCode;
64-
}
65-
66-
/**
67-
* @param zipCode the zipCode to set
68-
*/
69-
public void setZipCode(String zipCode) {
70-
this.zipCode = zipCode;
71-
}
72-
73-
/**
74-
* @return the city
75-
*/
76-
public String getCity() {
77-
return city;
78-
}
79-
80-
/**
81-
* @param city the city to set
82-
*/
83-
public void setCity(String city) {
84-
this.city = city;
85-
}
8654
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QuerydslMongoPredicateExecutorIntegrationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.springframework.data.domain.Sort;
2929
import org.springframework.data.domain.Sort.Direction;
3030
import org.springframework.data.mongodb.core.MongoOperations;
31+
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
32+
import org.springframework.data.mongodb.repository.Address;
3133
import org.springframework.data.mongodb.repository.Person;
3234
import org.springframework.data.mongodb.repository.QPerson;
3335
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
@@ -99,4 +101,21 @@ public void findOneWithPredicateReturnsOptionalEmptyWhenNoDataFound() {
99101
public void findOneWithPredicateThrowsExceptionForNonUniqueResults() {
100102
repository.findOne(person.firstname.contains("e"));
101103
}
104+
105+
@Test // DATAMONGO-2101
106+
public void readEntityWithGeoJsonValue() {
107+
108+
Address adr1 = new Address("Hauptplatz", "4020", "Linz");
109+
adr1.setLocation(new GeoJsonPoint(48.3063548, 14.2851337));
110+
111+
Person person1 = new Person("Max", "The Mighty");
112+
person1.setAddress(adr1);
113+
114+
operations.save(person1);
115+
116+
List<Person> result = new SpringDataMongodbQuery<>(operations, Person.class).where(person.firstname.eq("Max"))
117+
.fetch();
118+
119+
assertThat(result).containsExactly(person1);
120+
}
102121
}

0 commit comments

Comments
 (0)