Fix regression in findAndReplace when using native MongoDB types as domain value.
This commit fixes a regression that prevented native org.bson.Document to serve as source for a findAndReplaceOperation. Closes: #4300 Original Pull Request: #4310
This commit is contained in:
@@ -283,6 +283,10 @@ class EntityOperations {
|
||||
* @see EntityProjectionIntrospector#introspect(Class, Class)
|
||||
*/
|
||||
public <M, D> EntityProjection<M, D> introspectProjection(Class<M> resultType, Class<D> entityType) {
|
||||
|
||||
if (!queryMapper.getMappingContext().hasPersistentEntityFor(entityType)) {
|
||||
return (EntityProjection) EntityProjection.nonProjecting(resultType);
|
||||
}
|
||||
return introspector.introspect(resultType, entityType);
|
||||
}
|
||||
|
||||
|
||||
@@ -2526,6 +2526,26 @@ public class MongoTemplateTests {
|
||||
assertThat(projection.getName()).isEqualTo("Walter");
|
||||
}
|
||||
|
||||
@Test // GH-4300
|
||||
public void findAndReplaceShouldAllowNativeDomainTypesAndReturnAProjection() {
|
||||
|
||||
MyPerson person = new MyPerson("Walter");
|
||||
person.address = new Address("TX", "Austin");
|
||||
template.save(person);
|
||||
|
||||
MyPerson previous = template.findAndReplace(query(where("name").is("Walter")),
|
||||
new org.bson.Document("name", "Heisenberg"), FindAndReplaceOptions.options(), org.bson.Document.class,
|
||||
"myPerson", MyPerson.class);
|
||||
|
||||
assertThat(previous).isNotNull();
|
||||
assertThat(previous.getAddress()).isEqualTo(person.address);
|
||||
|
||||
org.bson.Document loaded = template.execute(MyPerson.class, collection -> {
|
||||
return collection.find(new org.bson.Document("name", "Heisenberg")).first();
|
||||
});
|
||||
assertThat(loaded.get("_id")).isEqualTo(new ObjectId(person.id));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-407
|
||||
public void updatesShouldRetainTypeInformationEvenForCollections() {
|
||||
|
||||
|
||||
@@ -2392,6 +2392,17 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
||||
.isEqualTo(new com.mongodb.client.model.TimeSeriesOptions("time_stamp").toString());
|
||||
}
|
||||
|
||||
@Test // GH-4300
|
||||
void findAndReplaceAllowsDocumentSourceType() {
|
||||
|
||||
template.findAndReplace(new Query(), new Document("spring", "data"), FindAndReplaceOptions.options().upsert(),
|
||||
Document.class, "coll-1", Person.class);
|
||||
|
||||
verify(db).getCollection(eq("coll-1"), eq(Document.class));
|
||||
verify(collection).findOneAndReplace((Bson) any(Bson.class), eq(new Document("spring", "data")),
|
||||
any(FindOneAndReplaceOptions.class));
|
||||
}
|
||||
|
||||
class AutogenerateableId {
|
||||
|
||||
@Id BigInteger id;
|
||||
|
||||
@@ -729,6 +729,32 @@ public class ReactiveMongoTemplateTests {
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // GH-4300
|
||||
public void findAndReplaceShouldAllowNativeDomainTypesAndReturnAProjection() {
|
||||
|
||||
MongoTemplateTests.MyPerson person = new MongoTemplateTests.MyPerson("Walter");
|
||||
person.address = new Address("TX", "Austin");
|
||||
template.save(person) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextCount(1) //
|
||||
.verifyComplete();
|
||||
|
||||
template
|
||||
.findAndReplace(query(where("name").is("Walter")), new org.bson.Document("name", "Heisenberg"),
|
||||
FindAndReplaceOptions.options(), org.bson.Document.class, "myPerson", MongoTemplateTests.MyPerson.class)
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(actual -> {
|
||||
assertThat(actual.getAddress()).isEqualTo(person.address);
|
||||
}).verifyComplete();
|
||||
|
||||
template.execute(MongoTemplateTests.MyPerson.class, collection -> {
|
||||
return collection.find(new org.bson.Document("name", "Heisenberg")).first();
|
||||
}).as(StepVerifier::create) //
|
||||
.consumeNextWith(loaded -> {
|
||||
assertThat(loaded.get("_id")).isEqualTo(new ObjectId(person.id));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1827
|
||||
void findAndReplaceShouldReplaceObjectReturingNew() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user