Browse Source

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.
pull/351/merge
Christoph Strobl 10 years ago committed by Mark Paluch
parent
commit
9930ec2d19
  1. 19
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 34
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 the original author or authors.
* Copyright 2011-2016 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.
@ -32,6 +32,7 @@ import org.springframework.data.mapping.Association; @@ -32,6 +32,7 @@ import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.context.PersistentPropertyPath;
import org.springframework.data.mapping.model.MappingException;
@ -122,10 +123,20 @@ public class QueryMapper { @@ -122,10 +123,20 @@ public class QueryMapper {
continue;
}
Field field = createPropertyField(entity, key, mappingContext);
Entry<String, Object> entry = getMappedObjectForField(field, query.get(key));
try {
Field field = createPropertyField(entity, key, mappingContext);
Entry<String, Object> entry = getMappedObjectForField(field, query.get(key));
result.put(entry.getKey(), entry.getValue());
} catch (InvalidPersistentPropertyPath invalidPathException) {
result.put(entry.getKey(), entry.getValue());
// in case the object has not already been mapped
if (!(query.get(key) instanceof DBObject)) {
throw invalidPathException;
}
result.put(key, query.get(key));
}
}
return result;

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 the original author or authors.
* Copyright 2011-2016 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.
@ -63,6 +63,7 @@ import org.springframework.data.mongodb.core.convert.DbRefResolver; @@ -63,6 +63,7 @@ import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.Index.Duplicates;
import org.springframework.data.mongodb.core.index.IndexField;
@ -197,6 +198,7 @@ public class MongoTemplateTests { @@ -197,6 +198,7 @@ public class MongoTemplateTests {
template.dropCollection(SomeTemplate.class);
template.dropCollection(Address.class);
template.dropCollection(DocumentWithCollectionOfSamples.class);
template.dropCollection(WithGeoJson.class);
}
@Test
@ -3143,11 +3145,30 @@ public class MongoTemplateTests { @@ -3143,11 +3145,30 @@ public class MongoTemplateTests {
assertThat(loaded.refToDocNotUsedInCtor, instanceOf(LazyLoadingProxy.class));
}
/**
* @see DATAMONGO-1401
*/
@Test
public void updateShouldWorkForTypesContainingGeoJsonTypes() {
WithGeoJson wgj = new WithGeoJson();
wgj.id = "1";
wgj.description = "datamongo-1401";
wgj.point = new GeoJsonPoint(1D, 2D);
template.save(wgj);
wgj.description = "datamongo-1401-update";
template.save(wgj);
assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point)));
}
static class DoucmentWithNamedIdField {
@Id String someIdKey;
@Field(value = "val")//
@Field(value = "val") //
String value;
@Override
@ -3484,4 +3505,13 @@ public class MongoTemplateTests { @@ -3484,4 +3505,13 @@ public class MongoTemplateTests {
}
static class WithGeoJson {
@Id String id;
@Version //
Integer version;
String description;
GeoJsonPoint point;
}
}

Loading…
Cancel
Save