Skip to content

Commit

Permalink
fix: single readPreferenceTags should be parsed as an array
Browse files Browse the repository at this point in the history
ReadPreference tags must be an array in order for matching to
succeed during server selection. If a single value is entered for
the `readPreferenceTags` uri option, it should be parsed as an
array of size one.

NODE-2541
  • Loading branch information
mbroadst committed Apr 6, 2020
1 parent d368f12 commit a50611b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/core/uri_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ function applyConnectionStringOption(obj, key, value, options) {
}
}

if (key === 'readpreferencetags' && Array.isArray(value)) {
value = splitArrayOfMultipleReadPreferenceTags(value);
if (key === 'readpreferencetags') {
value = Array.isArray(value) ? splitArrayOfMultipleReadPreferenceTags(value) : [value];
}

// set the actual value
Expand Down
15 changes: 15 additions & 0 deletions test/spec/uri-options/read-preference-options.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
"maxStalenessSeconds": 120
}
},
{
"description": "Single readPreferenceTags is parsed as array of size one",
"uri": "mongodb://example.com/?readPreferenceTags=dc:ny",
"valid": true,
"warning": false,
"hosts": null,
"auth": null,
"options": {
"readPreferenceTags": [
{
"dc": "ny"
}
]
}
},
{
"description": "Invalid readPreferenceTags causes a warning",
"uri": "mongodb://example.com/?readPreferenceTags=invalid",
Expand Down
13 changes: 12 additions & 1 deletion test/spec/uri-options/read-preference-options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ tests:
options:
readPreference: "primaryPreferred"
readPreferenceTags:
-
-
dc: "ny"
rack: "1"
-
dc: "ny"
maxStalenessSeconds: 120
-
description: "Single readPreferenceTags is parsed as array of size one"
uri: "mongodb://example.com/?readPreferenceTags=dc:ny"
valid: true
warning: false
hosts: ~
auth: ~
options:
readPreferenceTags:
-
dc: "ny"
-
description: "Invalid readPreferenceTags causes a warning"
uri: "mongodb://example.com/?readPreferenceTags=invalid"
Expand Down

0 comments on commit a50611b

Please sign in to comment.