Skip to content

Commit e3062f7

Browse files
deepview-autofixclaudeChALkeRljharb
committed
[Fix] stringify: apply formatter to encoded key under strictNullHandling
Previously the null branch returned the raw `encoder(prefix, ...)` output (or raw `prefix`) without running it through `formatter`. With `format: 'RFC1738'` this caused keys containing spaces to serialize as `%20` in strict-null mode while the normal primitive path produced `+`, yielding inconsistent output for the same key. Co-Authored-By: Claude <[email protected]> Co-Authored-By: DeepView Autofix <[email protected]> Co-Authored-By: Nikita Skovoroda <[email protected]> Co-Authored-By: Jordan Harband <[email protected]> Signed-off-by: Nikita Skovoroda <[email protected]>
1 parent 0c180a4 commit e3062f7

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎lib/stringify.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ var stringify = function stringify(
118118

119119
if (obj === null) {
120120
if (strictNullHandling) {
121-
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix;
121+
return formatter(encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix);
122122
}
123123

124124
obj = '';

‎test/stringify.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,28 @@ test('stringify()', function (t) {
11401140
st.end();
11411141
});
11421142

1143+
t.test('strictNullHandling: applies the formatter to the encoded key (RFC1738)', function (st) {
1144+
st.equal(
1145+
qs.stringify(
1146+
{ 'a b': null, 'c d': 'e f' },
1147+
{ strictNullHandling: false, format: 'RFC1738' }
1148+
),
1149+
'a+b=&c+d=e+f',
1150+
'without: as expected'
1151+
);
1152+
1153+
st.equal(
1154+
qs.stringify(
1155+
{ 'a b': null, 'c d': 'e f' },
1156+
{ strictNullHandling: true, format: 'RFC1738' }
1157+
),
1158+
'a+b&c+d=e+f',
1159+
'with: as expected'
1160+
);
1161+
1162+
st.end();
1163+
});
1164+
11431165
t.test('throws if an invalid charset is specified', function (st) {
11441166
st['throws'](function () {
11451167
qs.stringify({ a: 'b' }, { charset: 'foobar' });

0 commit comments

Comments
 (0)