Skip to content

Commit 42ab648

Browse files
committed
updating backend for movement of data and args into JSON body
1 parent 10c3c06 commit 42ab648

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ public function onKernelController(ControllerEvent $event): void
118118
return;
119119
}
120120

121+
$actionArguments = [];
121122
if ($request->query->has('data')) {
122123
// ?data=
123124
$data = json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR);
124125
} else {
125126
// OR body of the request is JSON
126-
$data = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
127+
$requestData = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
128+
$data = $requestData['data'] ?? [];
129+
$actionArguments = $requestData['args'] ?? [];
127130
}
128131

129132
if (!\is_array($controller = $event->getController()) || 2 !== \count($controller)) {
@@ -148,17 +151,11 @@ public function onKernelController(ControllerEvent $event): void
148151

149152
$request->attributes->set('_mounted_component', $mounted);
150153

151-
if (!\is_string($queryString = $request->query->get('args'))) {
152-
return;
153-
}
154-
155154
// extra variables to be made available to the controller
156155
// (for "actions" only)
157-
parse_str($queryString, $args);
158-
159156
foreach (LiveArg::liveArgs($component, $action) as $parameter => $arg) {
160-
if (isset($args[$arg])) {
161-
$request->attributes->set($parameter, $args[$arg]);
157+
if (isset($actionArguments[$arg])) {
158+
$request->attributes->set($parameter, $actionArguments[$arg]);
162159
}
163160
}
164161
}

src/LiveComponent/tests/Functional/EventListener/LiveComponentSubscriberTest.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testCanExecuteComponentAction(): void
7272
})
7373
->post('/_components/component2/increase', [
7474
'headers' => ['X-CSRF-TOKEN' => $token],
75-
'body' => json_encode($dehydrated),
75+
'body' => json_encode(['data' => $dehydrated]),
7676
])
7777
->assertSuccessful()
7878
->assertHeaderContains('Content-Type', 'html')
@@ -145,7 +145,7 @@ public function testDisabledCsrfTokenForComponentDoesNotFail(): void
145145
->assertHeaderContains('Content-Type', 'html')
146146
->assertContains('Count: 1')
147147
->post('/_components/disabled_csrf/increase', [
148-
'body' => json_encode($dehydrated),
148+
'body' => json_encode(['data' => $dehydrated]),
149149
])
150150
->assertSuccessful()
151151
->assertHeaderContains('Content-Type', 'html')
@@ -184,7 +184,7 @@ public function testCanRedirectFromComponentAction(): void
184184
// with no custom header, it redirects like a normal browser
185185
->post('/_components/component2/redirect', [
186186
'headers' => ['X-CSRF-TOKEN' => $token],
187-
'body' => json_encode($dehydrated),
187+
'body' => json_encode(['data' => $dehydrated]),
188188
])
189189
->assertRedirectedTo('/')
190190

@@ -194,7 +194,7 @@ public function testCanRedirectFromComponentAction(): void
194194
'Accept' => 'application/vnd.live-component+html',
195195
'X-CSRF-TOKEN' => $token,
196196
],
197-
'body' => json_encode($dehydrated),
197+
'body' => json_encode(['data' => $dehydrated]),
198198
])
199199
->assertStatus(204)
200200
->assertHeaderEquals('Location', '/')
@@ -206,10 +206,10 @@ public function testInjectsLiveArgs(): void
206206
$dehydrated = $this->dehydrateComponent($this->mountComponent('component6'));
207207
$token = null;
208208

209-
$argsQueryParams = http_build_query(['args' => http_build_query(['arg1' => 'hello', 'arg2' => 666, 'custom' => '33.3'])]);
209+
$arguments = ['arg1' => 'hello', 'arg2' => 666, 'custom' => '33.3'];
210210
$this->browser()
211211
->throwExceptions()
212-
->get('/_components/component6?data='.urlencode(json_encode($dehydrated)).'&'.$argsQueryParams)
212+
->get('/_components/component6?data='.urlencode(json_encode($dehydrated)))
213213
->assertSuccessful()
214214
->assertHeaderContains('Content-Type', 'html')
215215
->assertContains('Arg1: not provided')
@@ -219,9 +219,12 @@ public function testInjectsLiveArgs(): void
219219
// get a valid token to use for actions
220220
$token = $response->crawler()->filter('div')->first()->attr('data-live-csrf-value');
221221
})
222-
->post('/_components/component6/inject?'.$argsQueryParams, [
222+
->post('/_components/component6/inject', [
223223
'headers' => ['X-CSRF-TOKEN' => $token],
224-
'body' => json_encode($dehydrated),
224+
'body' => json_encode([
225+
'data' => $dehydrated,
226+
'args' => $arguments,
227+
]),
225228
])
226229
->assertSuccessful()
227230
->assertHeaderContains('Content-Type', 'html')

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testFormValuesRebuildAfterFormChanges(): void
5050

5151
// post to action, which will add a new embedded comment
5252
->post('/_components/form_with_collection_type/addComment', [
53-
'body' => json_encode($dehydrated),
53+
'body' => json_encode(['data' => $dehydrated]),
5454
'headers' => ['X-CSRF-TOKEN' => $token],
5555
])
5656
->assertStatus(422)
@@ -85,8 +85,8 @@ public function testFormValuesRebuildAfterFormChanges(): void
8585
})
8686

8787
// post to action, which will remove the original embedded comment
88-
->post('/_components/form_with_collection_type/removeComment?'.http_build_query(['args' => 'index=0']), [
89-
'body' => json_encode($dehydrated),
88+
->post('/_components/form_with_collection_type/removeComment', [
89+
'body' => json_encode(['data' => $dehydrated, 'args' => ['index' => '0']]),
9090
'headers' => ['X-CSRF-TOKEN' => $token],
9191
])
9292
->assertStatus(422)
@@ -265,8 +265,8 @@ public function testLiveCollectionTypeFieldsAddedAndRemoved(): void
265265
$token = $response->crawler()->filter('div')->first()->attr('data-live-csrf-value');
266266
})
267267
// post to action, which will add a new embedded comment
268-
->post('/_components/form_with_live_collection_type/addCollectionItem?'.http_build_query(['args' => 'name=blog_post_form[comments]']), [
269-
'body' => json_encode($dehydrated),
268+
->post('/_components/form_with_live_collection_type/addCollectionItem', [
269+
'body' => json_encode(['data' => $dehydrated, 'args' => ['name' => 'blog_post_form[comments]']]),
270270
'headers' => ['X-CSRF-TOKEN' => $token],
271271
])
272272
->assertStatus(422)
@@ -301,8 +301,8 @@ public function testLiveCollectionTypeFieldsAddedAndRemoved(): void
301301
})
302302

303303
// post to action, which will remove the original embedded comment
304-
->post('/_components/form_with_live_collection_type/removeCollectionItem?'.http_build_query(['args' => 'name=blog_post_form[comments]&index=0']), [
305-
'body' => json_encode($dehydrated),
304+
->post('/_components/form_with_live_collection_type/removeCollectionItem', [
305+
'body' => json_encode(['data' => $dehydrated, 'args' => ['name' => 'blog_post_form[comments]', 'index' => '0']]),
306306
'headers' => ['X-CSRF-TOKEN' => $token],
307307
])
308308
->assertStatus(422)

0 commit comments

Comments
 (0)