Skip to content

Fix regression in findAndReplace when using native mongodb types as domain value #4310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ public <T> TypedOperations<T> forType(@Nullable Class<T> entityClass) {
* @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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,26 @@ public void findAndReplaceShouldProjectReturnedObjectCorrectly() {
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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,17 @@ void templatePassesOnTimeSeriesOptionsWhenNoTypeGiven() {
.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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,32 @@ void findAndReplaceShouldProjectReturnedObjectCorrectly() {
}).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() {

Expand Down