Skip to content

Commit

Permalink
NODE-2253: Support Regex and DBRef as legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Dec 16, 2019
1 parent fd5ce66 commit 900550d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class DBRef {
* @ignore
*/
static fromExtendedJSON(doc) {
console.log('.fromExtendedJSON', doc);

This comment has been minimized.

Copy link
@mvasilkov

mvasilkov Dec 16, 2019

Contributor

🙂

This comment has been minimized.

Copy link
@durran

durran Dec 16, 2019

Author Member

Whoops!

var copy = Object.assign({}, doc);
['$ref', '$id', '$db'].forEach(k => delete copy[k]);
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
Expand Down
1 change: 1 addition & 0 deletions lib/extended_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const keysToCodecs = {
$numberLong: Long,
$minKey: MinKey,
$maxKey: MaxKey,
$regex: BSONRegExp,
$regularExpression: BSONRegExp,
$timestamp: Timestamp
};
Expand Down
21 changes: 17 additions & 4 deletions lib/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class BSONRegExp {
}
}

static parseOptions(options) {
return options
? options
.split('')
.sort()
.join('')
: '';
}

/**
* @ignore
*/
Expand All @@ -53,12 +62,16 @@ class BSONRegExp {
* @ignore
*/
static fromExtendedJSON(doc) {
if (doc.$regex) {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc;
}
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
return new BSONRegExp(
doc.$regularExpression.pattern,
doc.$regularExpression.options
.split('')
.sort()
.join('')
BSONRegExp.parseOptions(doc.$regularExpression.options)
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ describe('Extended JSON', function() {
});

context('when deserializing regex', function() {
it.skip('parses $regex and $options', function() {
it('parses $regex and $options', function() {
const regexp = new BSONRegExp('hello world', 'i');
const doc = { field: regexp };
const bson = EJSON.parse('{"field":{"$regex":"hello world","$options":"i"}}', {
Expand All @@ -626,8 +626,8 @@ describe('Extended JSON', function() {
});

context('when deserializing dbref', function() {
it.skip('parses $ref and $id', function() {
const dbRef = new DBRef('tests', new Int32(1));
it('parses $ref and $id', function() {
const dbRef = new DBRef('tests', 1);
const doc = { field: dbRef };
const bson = EJSON.parse('{"field":{"$ref":"tests","$id":1}}', {
legacy: true
Expand Down

0 comments on commit 900550d

Please sign in to comment.