Skip to content

Commit a50611b

Browse files
committed
fix: single readPreferenceTags should be parsed as an array
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
1 parent d368f12 commit a50611b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/core/uri_parser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ function applyConnectionStringOption(obj, key, value, options) {
285285
}
286286
}
287287

288-
if (key === 'readpreferencetags' && Array.isArray(value)) {
289-
value = splitArrayOfMultipleReadPreferenceTags(value);
288+
if (key === 'readpreferencetags') {
289+
value = Array.isArray(value) ? splitArrayOfMultipleReadPreferenceTags(value) : [value];
290290
}
291291

292292
// set the actual value

test/spec/uri-options/read-preference-options.json

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
"maxStalenessSeconds": 120
2222
}
2323
},
24+
{
25+
"description": "Single readPreferenceTags is parsed as array of size one",
26+
"uri": "mongodb://example.com/?readPreferenceTags=dc:ny",
27+
"valid": true,
28+
"warning": false,
29+
"hosts": null,
30+
"auth": null,
31+
"options": {
32+
"readPreferenceTags": [
33+
{
34+
"dc": "ny"
35+
}
36+
]
37+
}
38+
},
2439
{
2540
"description": "Invalid readPreferenceTags causes a warning",
2641
"uri": "mongodb://example.com/?readPreferenceTags=invalid",

test/spec/uri-options/read-preference-options.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ tests:
99
options:
1010
readPreference: "primaryPreferred"
1111
readPreferenceTags:
12-
-
12+
-
1313
dc: "ny"
1414
rack: "1"
1515
-
1616
dc: "ny"
1717
maxStalenessSeconds: 120
18+
-
19+
description: "Single readPreferenceTags is parsed as array of size one"
20+
uri: "mongodb://example.com/?readPreferenceTags=dc:ny"
21+
valid: true
22+
warning: false
23+
hosts: ~
24+
auth: ~
25+
options:
26+
readPreferenceTags:
27+
-
28+
dc: "ny"
1829
-
1930
description: "Invalid readPreferenceTags causes a warning"
2031
uri: "mongodb://example.com/?readPreferenceTags=invalid"

0 commit comments

Comments
 (0)