Skip to content

Commit

Permalink
Remove document key checks.
Browse files Browse the repository at this point in the history
The reason for this is that MongoDB uses keys that starts with '$' or
contains '.' internally  which have two consequences: (a) users aren't *normally*
permitted to create documents using such keys, (b) documents sent by the
database need to handled *differently* upon deserialization as they could contain such
keys quite often. The whole *checking-for-invalid-keys* should be handled at
MongoDB driver level (morelike at database level, but whatever ...).

 - Remove document key checks from DefaultBsonDocument, DefaultBsonDocumentBuilder
   and BsonDocuments.
 - Update BsonDocument, BsonDocument.Builder and BsonDocuments documentation
   accordingly.
 - Delete BsonPreconditions as the class was used only at one place of the code.
  • Loading branch information
kohanyirobert committed Feb 6, 2012
1 parent 8c6a935 commit 2638eb1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 118 deletions.
22 changes: 9 additions & 13 deletions src/main/java/com/github/kohanyirobert/ebson/BsonDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public interface BsonDocument extends Map<String, Object> {
* @param <T> the expected type of the value associated with {@code key}
* @return the value associated with {@code key} in a type-safe manner
* @throws NullPointerException if {@code key} is null
* @throws IllegalArgumentException if this document does not contain it or it
* is illegal (starts with {@code '$'} or contains {@code '.'}) or if the
* value to be returned is not an instance of with {@code type}
* @throws IllegalArgumentException if this document does not contain
* {@code key} or if the value associated with it is not assignment-compatible
* with {@code type}
* @throws ClassCastException if {@code key} is not a string
*/
@Nullable
Expand All @@ -90,8 +90,8 @@ public interface BsonDocument extends Map<String, Object> {
* @param key the key whose associated value is to be returned
* @return the value associated with {@code key}
* @throws NullPointerException if {@code key} is null
* @throws IllegalArgumentException if this document does not contain it or it
* is illegal (starts with {@code '$'} or contains {@code '.'})
* @throws IllegalArgumentException if this document does not contain
* {@code key}
* @throws ClassCastException if {@code key} is not a string
*/
@Override
Expand All @@ -102,12 +102,10 @@ public interface BsonDocument extends Map<String, Object> {
* Returns <em>true</em> if {@code key} is contained by this document;
* <em>false</em> otherwise.
*
* @param key the key to be tested if it's contained by this document
* @param key the key to be tested if it is contained by this document
* @return <em>true</em> if {@code key} is contained by this document;
* <em>false</em> otherwise
* @throws NullPointerException if {@code key} is null
* @throws IllegalArgumentException if {@code key} is illegal (starts with
* {@code '$'} or contains {@code '.'})
* @throws ClassCastException if {@code key} is not a string
*/
@Override
Expand All @@ -117,7 +115,7 @@ public interface BsonDocument extends Map<String, Object> {
* Returns <em>true</em> if {@code value} is contained by this document;
* <em>false</em> otherwise.
*
* @param value the value to be tested if it's contained by this document
* @param value the value to be tested if it is contained by this document
* @return <em>true</em> if {@code value} is contained by this document;
* <em>false</em> otherwise
*/
Expand Down Expand Up @@ -222,8 +220,7 @@ public interface Builder {
* @param value the value associated with {@code key}
* @return this builder
* @throws NullPointerException if {@code key} is null
* @throws IllegalArgumentException if {@code key} is illegal (starts with
* {@code '$'} or contains {@code '.'}) or if it is already present
* @throws IllegalArgumentException if {@code key} is already present
*/
Builder put(String key, @Nullable Object value);

Expand All @@ -235,8 +232,7 @@ public interface Builder {
* @return this builder
* @throws NullPointerException if {@code map} or any of its keys are null
* @throws IllegalArgumentException if {@code map} contains keys that are
* illegal (starts with {@code '$'} or contains {@code '.'}) or already
* present
* already present
*/
Builder putAll(Map<String, Object> map);

Expand Down
40 changes: 9 additions & 31 deletions src/main/java/com/github/kohanyirobert/ebson/BsonDocuments.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public static void writeTo(ByteBuffer buffer, BsonDocument document) {
* @return a new document containing {@code map}'s key-value pairs
* @throws NullPointerException if {@code map} or any of its keys are
* null
* @throws IllegalArgumentException if {@code map} contains keys that are
* illegal (starts with {@code '$'} or contains {@code '.'})
*/
public static BsonDocument copyOf(Map<String, Object> map) {
return builder().putAll(map).build();
Expand All @@ -62,9 +60,7 @@ public static BsonDocument copyOf(Map<String, Object> map) {
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -83,9 +79,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -103,9 +97,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -123,9 +115,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -142,9 +132,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -161,9 +149,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -178,9 +164,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
Expand All @@ -195,9 +179,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3) {
Expand All @@ -211,9 +193,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2</em>
* @throws NullPointerException if any key-value pair's key is null
* @throws IllegalArgumentException if any key-value pair's key is illegal
* (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
* keys
* @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2) {
Expand All @@ -231,8 +211,6 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing {@code key} and its associated
* {@code value}
* @throws NullPointerException if {@code key} is null
* @throws IllegalArgumentException if {@code key} is illegal (starts with
* {@code '$'} or contains {@code '.'})
*/
public static BsonDocument of(String key, @Nullable Object value) {
return builder().put(key, value).build();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public Object get(Object key) {
@Override
public boolean containsKey(Object key) {
Preconditions.checkNotNull(key, "null key");
BsonPreconditions.checkIsInstance(key, String.class, "key: '%s' is not a string", key);
Preconditions.checkArgument(!((String) key).startsWith("$"), "key: '%s' starts with '$'", key);
Preconditions.checkArgument(!((String) key).contains("."), "key: '%s' contains '.'", key);
if (!String.class.isInstance(key))
throw new ClassCastException(String.format("key: '%s' is not a string", key));
return super.containsKey(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public BsonDocument.Builder putAll(Map<String, Object> map) {
@Override
public BsonDocument.Builder put(String key, @Nullable Object value) {
Preconditions.checkNotNull(key, "null key");
Preconditions.checkArgument(!key.startsWith("$"), "key: '%s' starts with '$'", key);
Preconditions.checkArgument(!key.contains("."), "key: '%s' contains '.'", key);
Preconditions.checkArgument(!builder.containsKey(key), "key: '%s' is already present", key);
builder.put(key, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public final class BsonDocumentsTest extends AbstractBsonTest {
private static final String KEY2 = "key2";
private static final String KEY3 = "key3";

private static final String DOLLAR_KEY = "$key";
private static final String DOT_KEY = "k.ey";

public BsonDocumentsTest() {}

@Test
Expand Down Expand Up @@ -68,24 +65,6 @@ public void copyOf_map_withNullKey() {
BsonDocuments.copyOf(map);
}

@Test(expected = IllegalArgumentException.class)
public void copyOf_map_withDollarKey() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(KEY1, null);
map.put(DOLLAR_KEY, null);
map.put(KEY3, null);
BsonDocuments.copyOf(map);
}

@Test(expected = IllegalArgumentException.class)
public void copyOf_map_withDotKey() {
Map<String, Object> map = new HashMap<String, Object>();
map.put(DOT_KEY, null);
map.put(KEY2, null);
map.put(KEY3, null);
BsonDocuments.copyOf(map);
}

@Test
public void of_singleKeyValuePair_withLegalKey() {
BsonDocument document = BsonDocuments.of(KEY1, null);
Expand All @@ -97,16 +76,6 @@ public void of_singleKeyValuePair_withNullKey() {
BsonDocuments.of(null, null);
}

@Test(expected = IllegalArgumentException.class)
public void of_singleKeyValuePair_withDollarKey() {
BsonDocuments.of(DOLLAR_KEY, null);
}

@Test(expected = IllegalArgumentException.class)
public void of_singleKeyValuePair_withDotKey() {
BsonDocuments.of(DOT_KEY, null);
}

@Test
public void of_multipleKeyValuePairs_withLegalKeys() {
BsonDocument document = BsonDocuments.of(KEY1, null, KEY2, null, KEY3, null);
Expand All @@ -122,14 +91,4 @@ public void of_multipleKeyValuePairs_withNullKey() {
public void of_multipleKeyValuePairs_withDuplicateKeys() {
BsonDocuments.of(KEY1, null, KEY1, null, KEY3, null);
}

@Test(expected = IllegalArgumentException.class)
public void of_multipleKeyValuePairs_withDollarKey() {
BsonDocuments.of(KEY1, null, KEY2, null, DOLLAR_KEY, null);
}

@Test(expected = IllegalArgumentException.class)
public void of_multipleKeyValuePairs_withDotKey() {
BsonDocuments.of(DOT_KEY, null, KEY2, null, KEY3, null);
}
}

0 comments on commit 2638eb1

Please sign in to comment.