Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 757da84

Browse files
committed
fix(form): pick correct root element in serialize
Pick the correct root element for references
1 parent 635e8f2 commit 757da84

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

packages/form-serializer/lib/serializeForm.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ function collectElementByIDs(element) {
1010
}
1111

1212
const { parents } = element;
13-
if (parents) {
14-
const rootElement = parents.get(0);
15-
16-
if (rootElement) {
17-
rootElement.recursiveChildren.forEach((element) => {
18-
if (element.id) {
19-
dataStructures[element.id.toValue()] = element;
20-
}
21-
});
22-
}
13+
if (!parents || parents.isEmpty) {
14+
return dataStructures;
15+
}
16+
17+
const rootElement = parents.get(parents.length - 1);
18+
19+
if (rootElement) {
20+
rootElement.recursiveChildren.forEach((element) => {
21+
if (element.id) {
22+
dataStructures[element.id.toValue()] = element;
23+
}
24+
});
2325
}
2426

2527
return dataStructures;

packages/form-serializer/test/serializeForm-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe('#serializeForm', () => {
2626
new namespace.elements.Category([
2727
new namespace.elements.String('Hello world', { id: 'message' }),
2828
], { classes: ['dataStructures'] }),
29-
element,
29+
new namespace.elements.Category([
30+
element,
31+
]),
3032
]).freeze();
3133

3234
expect(serializeForm({ api: element, mediaType: 'multipart/form-data' })).to.equal('--BOUNDARY\r\nContent-Disposition: form-data; name="undefined"\r\n\r\nHello world\r\n--BOUNDARY--\r\n');

0 commit comments

Comments
 (0)