summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db/json.cpp17
-rw-r--r--dbtests/jsobjtests.cpp14
2 files changed, 23 insertions, 8 deletions
diff --git a/db/json.cpp b/db/json.cpp
index 1ee992d5f2e..5986df41df1 100644
--- a/db/json.cpp
+++ b/db/json.cpp
@@ -178,10 +178,16 @@ struct chClear {
struct fieldNameEnd {
fieldNameEnd( ObjectBuilder &_b ) : b( _b ) {}
void operator() ( const char *start, const char *end ) const {
- b.fieldNames.back() = b.popString();
- massert( "Field name cannot start with '$'",
- b.fieldNames.back().length() == 0 ||
- b.fieldNames.back()[ 0 ] != '$' );
+ string name = b.popString();
+ massert( "Invalid use of reserved field name",
+ name != "$ns" &&
+ name != "$id" &&
+ name != "$binary" &&
+ name != "$type" &&
+ name != "$date" &&
+ name != "$regex" &&
+ name != "$options" );
+ b.fieldNames.back() = name;
}
ObjectBuilder &b;
};
@@ -384,9 +390,6 @@ struct regexEnd {
// worth noting here that this parser follows a short-circuit convention. So,
// in the original z example on line 3, if the input was "ab", foo() would only
// be called once.
-// 2009-01-08 I've disallowed field names beginning with '$' (except those matching
-// our special types). If we go with this convention long term, parser
-// backtracking is less of a concern.
struct JsonGrammar : public grammar< JsonGrammar > {
public:
JsonGrammar( ObjectBuilder &_b ) : b( _b ) {}
diff --git a/dbtests/jsobjtests.cpp b/dbtests/jsobjtests.cpp
index 852be400e03..84cc9ea73f3 100644
--- a/dbtests/jsobjtests.cpp
+++ b/dbtests/jsobjtests.cpp
@@ -462,10 +462,21 @@ namespace FromJsonTests {
class ReservedFieldName : public Bad {
virtual string json() const {
- return "{ \"$a\" : \"b\" }";
+ return "{ \"$ns\" : \"b\" }";
}
};
+ class OkDollarFieldName : public Base {
+ virtual BSONObj bson() const {
+ BSONObjBuilder b;
+ b.append( "$where", 1 );
+ return b.doneAndDecouple();
+ }
+ virtual string json() const {
+ return "{ \"$where\" : 1 }";
+ }
+ };
+
class SingleNumber : public Base {
virtual BSONObj bson() const {
BSONObjBuilder b;
@@ -869,6 +880,7 @@ public:
add< FromJsonTests::SingleString >();
add< FromJsonTests::EmptyStrings >();
add< FromJsonTests::ReservedFieldName >();
+ add< FromJsonTests::OkDollarFieldName >();
add< FromJsonTests::SingleNumber >();
add< FromJsonTests::FancyNumber >();
add< FromJsonTests::TwoElements >();