Skip to content

Commit e5a41ad

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1534 - Fix bulk operations missing to write type info.
We now correctly convert entities into their MongoDB representation including type information via _class property. Original pull request: #415.
1 parent dd7d25c commit e5a41ad

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultBulkOperations.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,16 @@ public BulkOperations insert(Object document) {
123123

124124
Assert.notNull(document, "Document must not be null!");
125125

126-
models.add(new InsertOneModel<Document>((Document) mongoOperations.getConverter().convertToMongoType(document)));
126+
if (document instanceof Document) {
127+
128+
models.add(new InsertOneModel<>((Document) document));
129+
return this;
130+
}
131+
132+
Document sink = new Document();
133+
mongoOperations.getConverter().write(document, sink);
134+
135+
models.add(new InsertOneModel<>(sink));
127136
return this;
128137
}
129138

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*
4545
* @author Tobias Trelle
4646
* @author Oliver Gierke
47+
* @author Christoph Strobl
4748
*/
4849
@RunWith(SpringJUnit4ClassRunner.class)
4950
@ContextConfiguration("classpath:infrastructure.xml")
@@ -268,6 +269,25 @@ public void mixedBulkOrderedWithList() {
268269
assertThat(result.getDeletedCount(), is(1));
269270
}
270271

272+
/**
273+
* @see DATAMONGO-1534
274+
*/
275+
@Test
276+
public void insertShouldConsiderInheritance() {
277+
278+
SpecialDoc specialDoc = new SpecialDoc();
279+
specialDoc.id = "id-special";
280+
specialDoc.value = "normal-value";
281+
specialDoc.specialValue = "special-value";
282+
283+
createBulkOps(BulkMode.ORDERED).insert(Arrays.asList(specialDoc)).execute();
284+
285+
BaseDoc doc = operations.findOne(where("_id", specialDoc.id), BaseDoc.class, COLLECTION_NAME);
286+
287+
assertThat(doc, notNullValue());
288+
assertThat(doc, instanceOf(SpecialDoc.class));
289+
}
290+
271291
private void testUpdate(BulkMode mode, boolean multi, int expectedUpdates) {
272292

273293
BulkOperations bulkOps = createBulkOps(mode);

0 commit comments

Comments
 (0)