DATAMONGO-1912 - Propagate autogenerated Id to persistent top-level Maps.
We now set autogenerated Ids in Maps that are used as top-level entities. This allows transparent and persistent Map usage without requiring to use Document in application code. Previously, we only set autogenerated Ids in Document and persistent entity types. Original Pull Request: #545
This commit is contained in:
committed by
Christoph Strobl
parent
25dc56a840
commit
18c5ecd36f
@@ -2536,15 +2536,18 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
* @param savedObject
|
||||
* @param id
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void populateIdIfNecessary(Object savedObject, Object id) {
|
||||
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (savedObject instanceof Document) {
|
||||
Document document = (Document) savedObject;
|
||||
document.put(ID_FIELD, id);
|
||||
if (savedObject instanceof Map) {
|
||||
|
||||
Map<String, Object> map = (Map<String, Object>) savedObject;
|
||||
map.put(ID_FIELD, id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2224,15 +2224,18 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
* @param savedObject
|
||||
* @param id
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void populateIdIfNecessary(Object savedObject, @Nullable Object id) {
|
||||
|
||||
if (id == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (savedObject instanceof Document) {
|
||||
Document Document = (Document) savedObject;
|
||||
Document.put(ID_FIELD, id);
|
||||
if (savedObject instanceof Map) {
|
||||
|
||||
Map<String, Object> map = (Map<String, Object>) savedObject;
|
||||
map.put(ID_FIELD, id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,9 @@ import lombok.Data;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -218,6 +220,19 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
|
||||
assertThat(entity.id, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1912
|
||||
public void autogeneratesIdForMap() {
|
||||
|
||||
MongoTemplate template = spy(this.template);
|
||||
doReturn(new ObjectId()).when(template).saveDocument(Mockito.any(String.class), Mockito.any(Document.class),
|
||||
Mockito.any(Class.class));
|
||||
|
||||
Map<String, String> entity = new LinkedHashMap<>();
|
||||
template.save(entity, "foo");
|
||||
|
||||
assertThat(entity, hasKey("_id"));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-374
|
||||
public void convertsUpdateConstraintsUsingConverters() {
|
||||
|
||||
|
||||
@@ -21,7 +21,11 @@ import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.Data;
|
||||
import org.bson.types.ObjectId;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.bson.Document;
|
||||
@@ -57,6 +61,7 @@ import com.mongodb.reactivestreams.client.FindPublisher;
|
||||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import com.mongodb.reactivestreams.client.MongoCollection;
|
||||
import com.mongodb.reactivestreams.client.MongoDatabase;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReactiveMongoTemplate}.
|
||||
@@ -117,6 +122,20 @@ public class ReactiveMongoTemplateUnitTests {
|
||||
assertTrue(ReflectionTestUtils.getField(template, "mongoConverter") instanceof MappingMongoConverter);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1912
|
||||
public void autogeneratesIdForMap() {
|
||||
|
||||
ReactiveMongoTemplate template = spy(this.template);
|
||||
doReturn(Mono.just(new ObjectId())).when(template).saveDocument(Mockito.any(String.class), Mockito.any(Document.class),
|
||||
Mockito.any(Class.class));
|
||||
|
||||
Map<String, String> entity = new LinkedHashMap<>();
|
||||
StepVerifier.create(template.save(entity, "foo")).consumeNextWith(actual -> {
|
||||
|
||||
assertThat(entity, hasKey("_id"));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1518
|
||||
public void findShouldUseCollationWhenPresent() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user