Skip to content

Commit 9d12901

Browse files
committed
To fix a regression from 2.x, DBCollection now handles a null Class argument to setObjectClass and setInternalClass
JAVA-1901
1 parent 14e468a commit 9d12901

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

driver/src/main/com/mongodb/DBCollectionObjectFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2014 MongoDB, Inc.
2+
* Copyright 2008-2015 MongoDB, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,9 +67,12 @@ public DBCollectionObjectFactory update(final Class<? extends DBObject> aClass,
6767

6868
private Map<List<String>, Class<? extends DBObject>> updatePathToClassMap(final Class<? extends DBObject> aClass,
6969
final List<String> path) {
70-
Map<List<String>, Class<? extends DBObject>> map
71-
= new HashMap<List<String>, Class<? extends DBObject>>(pathToClassMap);
72-
map.put(path, aClass);
70+
Map<List<String>, Class<? extends DBObject>> map = new HashMap<List<String>, Class<? extends DBObject>>(pathToClassMap);
71+
if (aClass != null) {
72+
map.put(path, aClass);
73+
} else {
74+
map.remove(path);
75+
}
7376
return map;
7477
}
7578

@@ -83,7 +86,7 @@ Class<? extends DBObject> getClassForPath(final List<String> path) {
8386
}
8487

8588
private boolean isReflectionDBObject(final Class<? extends DBObject> aClass) {
86-
return ReflectionDBObject.class.isAssignableFrom(aClass);
89+
return aClass != null && ReflectionDBObject.class.isAssignableFrom(aClass);
8790
}
8891

8992
private MongoInternalException createInternalException(final Class<? extends DBObject> aClass, final Exception e) {

driver/src/test/unit/com/mongodb/DBCollectionObjectFactoryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb;
1818

19+
import org.hamcrest.Matchers;
1920
import org.junit.Before;
2021
import org.junit.Test;
2122

@@ -84,6 +85,16 @@ public void testReflectionNestedObject() {
8485
assertThat(factory.getInstance(singletonList("Next")), instanceOf(Tweet.class));
8586
}
8687

88+
@Test
89+
public void testThatNullObjectClassRevertsToDefault() {
90+
factory = factory.update(Tweet.class, singletonList("a")).update(null);
91+
assertThat(factory.getInstance(), Matchers.instanceOf(BasicDBObject.class));
92+
assertThat(factory.getInstance(singletonList("a")), instanceOf(Tweet.class));
93+
94+
factory = factory.update(null, singletonList("a"));
95+
assertThat(factory.getInstance(), Matchers.instanceOf(BasicDBObject.class));
96+
assertThat(factory.getInstance(singletonList("a")), instanceOf(BasicDBObject.class));
97+
}
8798

8899
public static class TopLevelDBObject extends BasicDBObject {
89100
private static final long serialVersionUID = 7029929727222305692L;

0 commit comments

Comments
 (0)