From 8572ab7206c5c3541be353bea1e7994fb566b92c Mon Sep 17 00:00:00 2001 From: Ashok Suthar Date: Thu, 1 Feb 2024 11:33:22 +0100 Subject: [PATCH] [Fix] make allowDots defaults to true when (encode|decode)DotInKeys is true --- lib/parse.js | 4 +++- lib/stringify.js | 4 +++- test/parse.js | 13 +++++++++++++ test/stringify.js | 12 ++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 93144343..57dd911d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -232,8 +232,10 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { } var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset; + var allowDots = typeof opts.allowDots === 'undefined' ? opts.decodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots; + return { - allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, + allowDots: allowDots, allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays, allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes, allowSparse: typeof opts.allowSparse === 'boolean' ? opts.allowSparse : defaults.allowSparse, diff --git a/lib/stringify.js b/lib/stringify.js index 34a78547..7ab744a1 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -252,9 +252,11 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) { throw new TypeError('`commaRoundTrip` must be a boolean, or absent'); } + var allowDots = typeof opts.allowDots === 'undefined' ? opts.encodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots; + return { addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix, - allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, + allowDots: allowDots, allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays, arrayFormat: arrayFormat, charset: charset, diff --git a/test/parse.js b/test/parse.js index 41ccffa3..9d585822 100644 --- a/test/parse.js +++ b/test/parse.js @@ -122,6 +122,19 @@ test('parse()', function (t) { st.end(); }); + t.test('should decode dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) { + st.deepEqual( + qs.parse( + 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe', + { decodeDotInKeys: true } + ), + { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, + 'with allowDots undefined and decodeDotInKeys true' + ); + + st.end(); + }); + t.test('should throw when decodeDotInKeys is not of type boolean', function (st) { st['throws']( function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); }, diff --git a/test/stringify.js b/test/stringify.js index 2ab9c3be..e6b6cd19 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -118,6 +118,18 @@ test('stringify()', function (t) { st.end(); }); + t.test('should encode dot in key of object, and automatically set allowDots to `true` when encodeDotInKeys is true and allowDots in undefined', function (st) { + st.equal( + qs.stringify( + { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, + { encodeDotInKeys: true } + ), + 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe', + 'with allowDots undefined and encodeDotInKeys true' + ); + st.end(); + }); + t.test('should encode dot in key of object when encodeDotInKeys and allowDots is provided, and nothing else when encodeValuesOnly is provided', function (st) { st.equal( qs.stringify({ 'name.obj': { first: 'John', last: 'Doe' } }, {