Skip to content

Commit 7186dc9

Browse files
committed
JAVA-820. Added support for checking of bad keys within documents contained in a List.
1 parent e79c9da commit 7186dc9

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,28 +1407,34 @@ private void _checkKeys( DBObject o ) {
14071407
return;
14081408

14091409
for ( String s : o.keySet() ){
1410-
validateKey ( s );
1411-
Object inner = o.get( s );
1412-
if ( inner instanceof DBObject ) {
1413-
_checkKeys( (DBObject)inner );
1414-
} else if ( inner instanceof Map ) {
1415-
_checkKeys( (Map<String, Object>)inner );
1416-
}
1410+
validateKey( s );
1411+
_checkValue( o.get( s ) );
14171412
}
14181413
}
14191414

14201415
/**
14211416
* Checks key strings for invalid characters.
14221417
*/
14231418
private void _checkKeys( Map<String, Object> o ) {
1424-
for ( String s : o.keySet() ){
1425-
validateKey ( s );
1426-
Object inner = o.get( s );
1427-
if ( inner instanceof DBObject ) {
1428-
_checkKeys( (DBObject)inner );
1429-
} else if ( inner instanceof Map ) {
1430-
_checkKeys( (Map<String, Object>)inner );
1431-
}
1419+
for ( Map.Entry<String, Object> cur : o.entrySet() ){
1420+
validateKey( cur.getKey() );
1421+
_checkValue( cur.getValue() );
1422+
}
1423+
}
1424+
1425+
private void _checkValues( final List list ) {
1426+
for ( Object cur : list ) {
1427+
_checkValue( cur );
1428+
}
1429+
}
1430+
1431+
private void _checkValue(final Object value) {
1432+
if ( value instanceof DBObject ) {
1433+
_checkKeys( (DBObject)value );
1434+
} else if ( value instanceof Map ) {
1435+
_checkKeys( (Map<String, Object>)value );
1436+
} else if ( value instanceof List ) {
1437+
_checkValues((List) value);
14321438
}
14331439
}
14341440

src/test/com/mongodb/JavaClientTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,12 @@ public void testBadKey(){
11331133
fail("Bad key was accepted");
11341134
} catch (IllegalArgumentException e) {}
11351135

1136+
try {
1137+
final List<BasicDBObject> list = Arrays.asList(new BasicDBObject("$a", 1));
1138+
c.save(new BasicDBObject("a", list));
1139+
fail("Bad key was accepted");
1140+
} catch (IllegalArgumentException e) {}
1141+
11361142
// try {
11371143
// c.save(new BasicDBObject("a", Arrays.asList(new BasicDBObject("$a", 1))));
11381144
// fail("Bad key was accepted");

0 commit comments

Comments
 (0)