Skip to content

Commit 5b28600

Browse files
fix: better handling of nested arrays when options.multiples (#621)
1 parent f9c71d5 commit 5b28600

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* fix: exposing file writable stream errors ([#520](https://github.com/node-formidable/node-formidable/pull/520), [#316](https://github.com/node-formidable/node-formidable/pull/316), [#469](https://github.com/node-formidable/node-formidable/pull/469), [#470](https://github.com/node-formidable/node-formidable/pull/470))
2626
* feat: custom file (re)naming, thru options.filename ([#591](https://github.com/node-formidable/node-formidable/pull/591), [#84](https://github.com/node-formidable/node-formidable/issues/84), [#86](https://github.com/node-formidable/node-formidable/issues/86), [#94](https://github.com/node-formidable/node-formidable/issues/94), [#154](https://github.com/node-formidable/node-formidable/issues/154), [#158](https://github.com/node-formidable/node-formidable/issues/158), [#488](https://github.com/node-formidable/node-formidable/issues/488), [#595](https://github.com/node-formidable/node-formidable/issues/595))
2727
* fix: make opts.filename from #591 work with opts.keepExtensions ([#597](https://github.com/node-formidable/node-formidable/pull/597))
28+
* fix: better handling of nested arrays when options.multiples ([#621](https://github.com/node-formidable/node-formidable/pull/621))
29+
2830
### v1.2.1 (2018-03-20)
2931

3032
* `maxFileSize` option with default of 200MB (Charlike Mike Reagent, Nima Shahri)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"dependencies": {
3434
"dezalgo": "1.0.3",
3535
"hexoid": "1.0.0",
36-
"once": "1.4.0"
36+
"once": "1.4.0",
37+
"qs": "^6.9.3"
3738
},
3839
"devDependencies": {
3940
"@commitlint/cli": "8.3.5",

src/Formidable.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const once = require('once');
1111
const dezalgo = require('dezalgo');
1212
const { EventEmitter } = require('events');
1313
const { StringDecoder } = require('string_decoder');
14+
const qs = require('qs');
1415

1516
const toHexoId = hexoid(25);
1617
const DEFAULT_OPTIONS = {
@@ -133,20 +134,13 @@ class IncomingForm extends EventEmitter {
133134
if (cb) {
134135
const callback = once(dezalgo(cb));
135136
const fields = {};
137+
let mockFields = '';
136138
const files = {};
137-
139+
138140
this.on('field', (name, value) => {
139-
// TODO: too much nesting
140-
if (this.options.multiples && name.slice(-2) === '[]') {
141-
const realName = name.slice(0, name.length - 2);
142-
if (hasOwnProp(fields, realName)) {
143-
if (!Array.isArray(fields[realName])) {
144-
fields[realName] = [fields[realName]];
145-
}
146-
} else {
147-
fields[realName] = [];
148-
}
149-
fields[realName].push(value);
141+
if (this.options.multiples) {
142+
let mObj = { [name] : value };
143+
mockFields = mockFields + '&' + qs.stringify(mObj);
150144
} else {
151145
fields[name] = value;
152146
}
@@ -170,6 +164,9 @@ class IncomingForm extends EventEmitter {
170164
callback(err, fields, files);
171165
});
172166
this.on('end', () => {
167+
if (this.options.multiples) {
168+
Object.assign(fields, qs.parse(mockFields));
169+
}
173170
callback(null, fields, files);
174171
});
175172
}

test/unit/formidable.test.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,58 @@ function makeHeader(filename) {
104104
req.headers = 'content-type: json; content-length:8';
105105
form.parse(req, (error, fields) => {
106106
expect(Array.isArray(fields.a)).toBe(true);
107-
expect(fields.a[0]).toBe(1);
108-
expect(fields.a[1]).toBe(2);
107+
expect(fields.a[0]).toBe('1');
108+
expect(fields.a[1]).toBe('2');
109109
});
110-
form.emit('field', 'a[]', 1);
111-
form.emit('field', 'a[]', 2);
110+
form.emit('field', 'a[]', '1');
111+
form.emit('field', 'a[]', '2');
112+
form.emit('end');
113+
});
114+
115+
test(`${name}#_Nested array parameters support`, () => {
116+
const form = getForm(name, { multiples: true });
117+
118+
const req = new Request();
119+
req.headers = 'content-type: json; content-length:8';
120+
form.parse(req, (error, fields) => {
121+
expect(Array.isArray(fields.a)).toBe(true);
122+
expect(fields.a[0][0]).toBe('a');
123+
expect(fields.a[0][1]).toBe('b');
124+
expect(fields.a[1][0]).toBe('c');
125+
});
126+
form.emit('field', 'a[0][]', 'a');
127+
form.emit('field', 'a[0][]', 'b');
128+
form.emit('field', 'a[1][]', 'c');
129+
form.emit('end');
130+
});
131+
132+
test(`${name}#_Object parameters support`, () => {
133+
const form = getForm(name, { multiples: true });
134+
135+
const req = new Request();
136+
req.headers = 'content-type: json; content-length:8';
137+
form.parse(req, (error, fields) => {
138+
expect(fields.a.x).toBe('1');
139+
expect(fields.a.y).toBe('2');
140+
});
141+
form.emit('field', 'a[x]', '1');
142+
form.emit('field', 'a[y]', '2');
143+
form.emit('end');
144+
});
145+
146+
test(`${name}#_Nested object parameters support`, () => {
147+
const form = getForm(name, { multiples: true });
148+
149+
const req = new Request();
150+
req.headers = 'content-type: json; content-length:8';
151+
form.parse(req, (error, fields) => {
152+
expect(fields.a.l1.k1).toBe('2');
153+
expect(fields.a.l1.k2).toBe('3');
154+
expect(fields.a.l2.k3).toBe('5');
155+
});
156+
form.emit('field', 'a[l1][k1]', '2');
157+
form.emit('field', 'a[l1][k2]', '3');
158+
form.emit('field', 'a[l2][k3]', '5');
112159
form.emit('end');
113160
});
114161

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4810,7 +4810,7 @@ qs@6.7.0:
48104810
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
48114811
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==
48124812

4813-
qs@^6.5.1:
4813+
qs@^6.5.1, qs@^6.9.3:
48144814
version "6.9.3"
48154815
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.3.tgz#bfadcd296c2d549f1dffa560619132c977f5008e"
48164816
integrity sha512-EbZYNarm6138UKKq46tdx08Yo/q9ZhFoAXAI1meAFd2GtbRDhbZY2WQSICskT0c5q99aFzLG1D4nvTk9tqfXIw==

0 commit comments

Comments
 (0)