Skip to content

Commit e4eaf6f

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 6e45386 commit e4eaf6f

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.
@@ -31,6 +31,7 @@
3131
import org.springframework.data.mapping.PersistentEntity;
3232
import org.springframework.data.mapping.PropertyPath;
3333
import org.springframework.data.mapping.PropertyReferenceException;
34+
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
3435
import org.springframework.data.mapping.context.MappingContext;
3536
import org.springframework.data.mapping.context.PersistentPropertyPath;
3637
import org.springframework.data.mapping.model.MappingException;
@@ -119,10 +120,20 @@ public DBObject getMappedObject(DBObject query, MongoPersistentEntity<?> entity)
119120
continue;
120121
}
121122

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

125-
result.put(entry.getKey(), entry.getValue());
130+
// in case the object has not already been mapped
131+
if (!(query.get(key) instanceof DBObject)) {
132+
throw invalidPathException;
133+
}
134+
135+
result.put(key, query.get(key));
136+
}
126137
}
127138

128139
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)