Skip to content

Commit 19211c1

Browse files
authored
fix: handle nested object fields in form data (#14469)
Fixes #14460 correcting object pointer handling in deep_set so nested form fields are properly stored in the same object hierarchy.
1 parent fae6b5a commit 19211c1

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: handle nested object fields in form data

packages/kit/src/runtime/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ export function deep_set(object, keys, value) {
123123
throw new Error(`Invalid array key ${keys[i + 1]}`);
124124
}
125125
} else {
126-
object = object[key] ??= is_array ? [] : Object.create(null); // guard against prototype pollution
126+
object[key] ??= is_array ? [] : Object.create(null); // guard against prototype pollution
127127
}
128+
object = object[key];
128129
}
129130

130131
object[keys[keys.length - 1]] = value;

packages/kit/src/runtime/utils.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,22 @@ describe('convert_formdata', () => {
107107
array: ['a', 'b', 'c']
108108
});
109109
});
110+
111+
test('handles multiple fields at the same nested level', () => {
112+
const data = new FormData();
113+
114+
data.append('user.name.first', 'first');
115+
data.append('user.name.last', 'last');
116+
117+
const converted = convert_formdata(data);
118+
119+
expect(converted).toEqual({
120+
user: {
121+
name: {
122+
first: 'first',
123+
last: 'last'
124+
}
125+
}
126+
});
127+
});
110128
});

0 commit comments

Comments
 (0)