From 867fc7b48610f9b497b5f2b21dcade4d856687f6 Mon Sep 17 00:00:00 2001 From: Ashok Suthar Date: Fri, 2 Feb 2024 18:22:12 +0100 Subject: [PATCH] [Fix] when (encode|decode)DotInKeys is false, behavior should be same as allowDots --- lib/parse.js | 9 ++++----- lib/stringify.js | 4 ---- test/parse.js | 26 +++++++------------------- test/stringify.js | 32 ++++++++++++++++---------------- 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 7bf18968..f608943a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -127,7 +127,10 @@ var parseObject = function (chain, val, options, valuesParsed) { } else { obj = options.plainObjects ? Object.create(null) : {}; var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root; - var decodedRoot = options.allowDots && options.decodeDotInKeys ? cleanRoot.replace(/%2E/g, '.') : cleanRoot; + var decodedRoot = cleanRoot; + if (options.decodeDotInKeys) { + decodedRoot = cleanRoot.replace(/%2E/g, '.'); + } var index = parseInt(decodedRoot, 10); if (!options.parseArrays && decodedRoot === '') { obj = { 0: leaf }; @@ -218,10 +221,6 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { throw new TypeError('`decodeDotInKeys` option can only be `true` or `false`, when provided'); } - if (opts.allowDots === false && opts.decodeDotInKeys === true) { - throw new TypeError('Provided combination is not valid: allowDots=false & decodeDotInKeys=true'); - } - if (opts.decoder !== null && typeof opts.decoder !== 'undefined' && typeof opts.decoder !== 'function') { throw new TypeError('Decoder has to be a function.'); } diff --git a/lib/stringify.js b/lib/stringify.js index 43caa138..584e9bd7 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -219,10 +219,6 @@ var normalizeStringifyOptions = function normalizeStringifyOptions(opts) { throw new TypeError('Encoder has to be a function.'); } - if (opts.allowDots === true && opts.encodeDotInKeys === false) { - throw new TypeError('Provided combination is not valid: allowDots=true & encodeDotInKeys=false'); - } - var charset = opts.charset || defaults.charset; if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'); diff --git a/test/parse.js b/test/parse.js index a1575698..7d64ed53 100644 --- a/test/parse.js +++ b/test/parse.js @@ -82,7 +82,11 @@ test('parse()', function (t) { { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' }, 'with allowDots false and decodeDotInKeys false' ); - + st.deepEqual( + qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }), + { name: { obj: { first: 'John', last: 'Doe' } } }, + 'with allowDots false and decodeDotInKeys false' + ); st.deepEqual( qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }), { 'name%2Eobj': { first: 'John', last: 'Doe' } }, @@ -104,10 +108,10 @@ test('parse()', function (t) { ); st.deepEqual( qs.parse( - 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe', + 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe', { allowDots: true, decodeDotInKeys: false } ), - { 'name%2Eobj%2Esubobject': { 'first%2Egodly%2Ename': 'John', last: 'Doe' } }, + { name: { obj: { subobject: { first: { godly: { name: "John" } }, last: "Doe" } } } }, 'with allowDots true and decodeDotInKeys false' ); st.deepEqual( @@ -158,22 +162,6 @@ test('parse()', function (t) { st.end(); }); - t.test('should throw when allowDots is false and decodeDotInKeys is true', function (st) { - st['throws']( - function () { - qs.parse( - 'name%252Eobj.first=John&name%252Eobj.last=Doe', - { allowDots: false, decodeDotInKeys: true } - ); - }, { - name: 'TypeError', - message: 'Provided combination is not valid: allowDots=false & decodeDotInKeys=true' - } - ); - - st.end(); - }); - t.test('allows empty arrays in obj values', function (st) { st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' }); st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' }); diff --git a/test/stringify.js b/test/stringify.js index 608b1012..712cfda7 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -73,6 +73,14 @@ test('stringify()', function (t) { 'name.obj%5Bfirst%5D=John&name.obj%5Blast%5D=Doe', 'with allowDots false and encodeDotInKeys false' ); + st.equal( + qs.stringify( + { 'name.obj': { first: 'John', last: 'Doe' } }, + { allowDots: true, encodeDotInKeys: false } + ), + 'name.obj.first=John&name.obj.last=Doe', + 'with allowDots true and encodeDotInKeys false' + ); st.equal( qs.stringify( { 'name.obj': { first: 'John', last: 'Doe' } }, @@ -98,6 +106,14 @@ test('stringify()', function (t) { 'name.obj.subobject%5Bfirst.godly.name%5D=John&name.obj.subobject%5Blast%5D=Doe', 'with allowDots false and encodeDotInKeys false' ); + st.equal( + qs.stringify( + { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, + { allowDots: true, encodeDotInKeys: false } + ), + 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe', + 'with allowDots false and encodeDotInKeys false' + ); st.equal( qs.stringify( { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, @@ -170,22 +186,6 @@ test('stringify()', function (t) { st.end(); }); - t.test('should throw when allowDots is true and encodeDotInKeys is false', function (st) { - st['throws']( - function () { - qs.stringify( - { 'name.obj': { first: 'John', last: 'Doe' } }, - { allowDots: true, encodeDotInKeys: false } - ); - }, { - message: 'Provided combination is not valid: allowDots=true & encodeDotInKeys=false', - name: 'TypeError' - } - ); - - st.end(); - }); - t.test('adds query prefix', function (st) { st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b'); st.end();