Skip to content

Commit 9930ec2

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1401 - Fix error when updating entity with both GeoJsonPoint and Version property.
We now ignore property reference exceptions when resolving field values that have already been mapped. Eg. in case of an already mapped update extracted from an actual domain type instance. Original pull request: #351.
1 parent 83d7f44 commit 9930ec2

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@
3232
import org.springframework.data.mapping.PersistentEntity;
3333
import org.springframework.data.mapping.PropertyPath;
3434
import org.springframework.data.mapping.PropertyReferenceException;
35+
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
3536
import org.springframework.data.mapping.context.MappingContext;
3637
import org.springframework.data.mapping.context.PersistentPropertyPath;
3738
import org.springframework.data.mapping.model.MappingException;
@@ -122,10 +123,20 @@ public DBObject getMappedObject(DBObject query, MongoPersistentEntity<?> entity)
122123
continue;
123124
}
124125

125-
Field field = createPropertyField(entity, key, mappingContext);
126-
Entry<String, Object> entry = getMappedObjectForField(field, query.get(key));
126+
try {
127+
128+
Field field = createPropertyField(entity, key, mappingContext);
129+
Entry<String, Object> entry = getMappedObjectForField(field, query.get(key));
130+
result.put(entry.getKey(), entry.getValue());
131+
} catch (InvalidPersistentPropertyPath invalidPathException) {
127132

128-
result.put(entry.getKey(), entry.getValue());
133+
// in case the object has not already been mapped
134+
if (!(query.get(key) instanceof DBObject)) {
135+
throw invalidPathException;
136+
}
137+
138+
result.put(key, query.get(key));
139+
}
129140
}
130141

131142
return result;

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,6 +63,7 @@
6363
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
6464
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
6565
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
66+
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
6667
import org.springframework.data.mongodb.core.index.Index;
6768
import org.springframework.data.mongodb.core.index.Index.Duplicates;
6869
import org.springframework.data.mongodb.core.index.IndexField;
@@ -197,6 +198,7 @@ protected void cleanDb() {
197198
template.dropCollection(SomeTemplate.class);
198199
template.dropCollection(Address.class);
199200
template.dropCollection(DocumentWithCollectionOfSamples.class);
201+
template.dropCollection(WithGeoJson.class);
200202
}
201203

202204
@Test
@@ -3143,11 +3145,30 @@ public void shouldRespectParamterValueWhenAttemptingToReuseLazyLoadedDBRefUsedIn
31433145
assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class));
31443146
}
31453147

3148+
/**
3149+
* @see DATAMONGO-1401
3150+
*/
3151+
@Test
3152+
public void updateShouldWorkForTypesContainingGeoJsonTypes() {
3153+
3154+
WithGeoJson wgj = new WithGeoJson();
3155+
wgj.id = "1";
3156+
wgj.description = "datamongo-1401";
3157+
wgj.point = new GeoJsonPoint(1D, 2D);
3158+
3159+
template.save(wgj);
3160+
3161+
wgj.description = "datamongo-1401-update";
3162+
template.save(wgj);
3163+
3164+
assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point)));
3165+
}
3166+
31463167
static class DoucmentWithNamedIdField {
31473168

31483169
@Id String someIdKey;
31493170

3150-
@Field(value = "val")//
3171+
@Field(value = "val") //
31513172
String value;
31523173

31533174
@Override
@@ -3484,4 +3505,13 @@ public DocumentWithLazyDBrefUsedInPresistenceConstructor(Document refToDocUsedIn
34843505

34853506
}
34863507

3508+
static class WithGeoJson {
3509+
3510+
@Id String id;
3511+
@Version //
3512+
Integer version;
3513+
String description;
3514+
GeoJsonPoint point;
3515+
}
3516+
34873517
}

0 commit comments

Comments
 (0)