Skip to content

Commit 8005567

Browse files
committed
Read POST payload from data key instead of a request body
1 parent 0f44cd0 commit 8005567

File tree

6 files changed

+81
-61
lines changed

6 files changed

+81
-61
lines changed

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ private static function parseDataFor(Request $request): array
221221
'propsFromParent' => self::parseJsonFromQuery($request, 'propsFromParent'),
222222
];
223223
} else {
224-
$requestData = $request->toArray();
224+
try {
225+
$requestData = json_decode($request->request->get('data'), true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
226+
} catch (\JsonException $e) {
227+
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
228+
}
225229

226230
$liveRequestData = [
227231
'props' => $requestData['props'] ?? [],

src/LiveComponent/tests/Functional/Controller/BatchActionControllerTest.php

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ public function testCanBatchActions(): void
3131

3232
$this->browser()
3333
->throwExceptions()
34-
->get('/_components/with_actions', ['json' => ['props' => $dehydrated->getProps()]])
34+
->get('/_components/with_actions', ['query' => ['props' => json_encode($dehydrated->getProps())]])
3535
->assertSuccessful()
3636
->assertSee('initial')
3737
->use(function (Crawler $crawler, KernelBrowser $browser) {
3838
$rootElement = $crawler->filter('ul')->first();
3939
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
4040

4141
$browser->post('/_components/with_actions/add', [
42-
'json' => [
43-
'props' => $liveProps,
44-
'args' => ['what' => 'first'],
42+
'body' => [
43+
'data' => json_encode([
44+
'props' => $liveProps,
45+
'args' => ['what' => 'first'],
46+
]),
4547
],
4648
'headers' => ['X-CSRF-TOKEN' => $crawler->filter('ul')->first()->attr('data-live-csrf-value')],
4749
]);
@@ -53,13 +55,15 @@ public function testCanBatchActions(): void
5355
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
5456

5557
$browser->post('/_components/with_actions/_batch', [
56-
'json' => [
57-
'props' => $liveProps,
58-
'actions' => [
59-
['name' => 'add', 'args' => ['what' => 'second']],
60-
['name' => 'add', 'args' => ['what' => 'third']],
61-
['name' => 'add', 'args' => ['what' => 'fourth']],
62-
],
58+
'body' => [
59+
'data' => json_encode([
60+
'props' => $liveProps,
61+
'actions' => [
62+
['name' => 'add', 'args' => ['what' => 'second']],
63+
['name' => 'add', 'args' => ['what' => 'third']],
64+
['name' => 'add', 'args' => ['what' => 'fourth']],
65+
],
66+
]),
6367
],
6468
'headers' => ['X-CSRF-TOKEN' => $crawler->filter('ul')->first()->attr('data-live-csrf-value')],
6569
]);
@@ -78,21 +82,23 @@ public function testCanBatchActionsWithAlternateRoute(): void
7882

7983
$this->browser()
8084
->throwExceptions()
81-
->get('/alt/alternate_route', ['json' => ['props' => $dehydrated->getProps()]])
85+
->get('/alt/alternate_route', ['query' => ['props' => json_encode($dehydrated->getProps())]])
8286
->assertSuccessful()
8387
->assertSee('count: 0')
8488
->use(function (Crawler $crawler, KernelBrowser $browser) {
8589
$rootElement = $crawler->filter('div')->first();
8690
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
8791

8892
$browser->post('/alt/alternate_route/_batch', [
89-
'json' => [
90-
'props' => $liveProps,
91-
'actions' => [
92-
['name' => 'increase'],
93-
['name' => 'increase'],
94-
['name' => 'increase'],
95-
],
93+
'body' => [
94+
'data' => json_encode([
95+
'props' => $liveProps,
96+
'actions' => [
97+
['name' => 'increase'],
98+
['name' => 'increase'],
99+
['name' => 'increase'],
100+
],
101+
]),
96102
],
97103
'headers' => ['X-CSRF-TOKEN' => $rootElement->attr('data-live-csrf-value')],
98104
]);
@@ -122,21 +128,23 @@ public function testRedirect(): void
122128

123129
$this->browser()
124130
->throwExceptions()
125-
->get('/_components/with_actions', ['json' => ['props' => $dehydrated->getProps()]])
131+
->get('/_components/with_actions', ['query' => ['props' => json_encode($dehydrated->getProps())]])
126132
->assertSuccessful()
127133
->interceptRedirects()
128134
->use(function (Crawler $crawler, KernelBrowser $browser) {
129135
$rootElement = $crawler->filter('ul')->first();
130136
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
131137

132138
$browser->post('/_components/with_actions/_batch', [
133-
'json' => [
134-
'props' => $liveProps,
135-
'actions' => [
136-
['name' => 'add', 'args' => ['what' => 'second']],
137-
['name' => 'redirect'],
138-
['name' => 'add', 'args' => ['what' => 'fourth']],
139-
],
139+
'body' => [
140+
'data' => json_encode([
141+
'props' => $liveProps,
142+
'actions' => [
143+
['name' => 'add', 'args' => ['what' => 'second']],
144+
['name' => 'redirect'],
145+
['name' => 'add', 'args' => ['what' => 'fourth']],
146+
],
147+
]),
140148
],
141149
'headers' => ['X-CSRF-TOKEN' => $crawler->filter('ul')->first()->attr('data-live-csrf-value')],
142150
]);
@@ -150,20 +158,22 @@ public function testException(): void
150158
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_actions'));
151159

152160
$this->browser()
153-
->get('/_components/with_actions', ['json' => ['props' => $dehydrated->getProps()]])
161+
->get('/_components/with_actions', ['query' => ['props' => json_encode($dehydrated->getProps())]])
154162
->assertSuccessful()
155163
->use(function (Crawler $crawler, KernelBrowser $browser) {
156164
$rootElement = $crawler->filter('ul')->first();
157165
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
158166

159167
$browser->post('/_components/with_actions/_batch', [
160-
'json' => [
161-
'props' => $liveProps,
162-
'actions' => [
163-
['name' => 'add', 'args' => ['what' => 'second']],
164-
['name' => 'exception'],
165-
['name' => 'add', 'args' => ['what' => 'fourth']],
166-
],
168+
'body' => [
169+
'data' => json_encode([
170+
'props' => $liveProps,
171+
'actions' => [
172+
['name' => 'add', 'args' => ['what' => 'second']],
173+
['name' => 'exception'],
174+
['name' => 'add', 'args' => ['what' => 'fourth']],
175+
],
176+
]),
167177
],
168178
'headers' => ['X-CSRF-TOKEN' => $crawler->filter('ul')->first()->attr('data-live-csrf-value')],
169179
]);
@@ -178,20 +188,22 @@ public function testCannotBatchWithNonLiveAction(): void
178188
$dehydrated = $this->dehydrateComponent($this->mountComponent('with_actions'));
179189

180190
$this->browser()
181-
->get('/_components/with_actions', ['json' => ['props' => $dehydrated->getProps()]])
191+
->get('/_components/with_actions', ['query' => ['props' => json_encode($dehydrated->getProps())]])
182192
->assertSuccessful()
183193
->use(function (Crawler $crawler, KernelBrowser $browser) {
184194
$rootElement = $crawler->filter('ul')->first();
185195
$liveProps = json_decode($rootElement->attr('data-live-props-value'), true);
186196

187197
$browser->post('/_components/with_actions/_batch', [
188-
'json' => [
189-
'props' => $liveProps,
190-
'actions' => [
191-
['name' => 'add', 'args' => ['what' => 'second']],
192-
['name' => 'nonLive'],
193-
['name' => 'add', 'args' => ['what' => 'fourth']],
194-
],
198+
'body' => [
199+
'data' => json_encode([
200+
'props' => $liveProps,
201+
'actions' => [
202+
['name' => 'add', 'args' => ['what' => 'second']],
203+
['name' => 'nonLive'],
204+
['name' => 'add', 'args' => ['what' => 'fourth']],
205+
],
206+
]),
195207
],
196208
'headers' => ['X-CSRF-TOKEN' => $crawler->filter('ul')->first()->attr('data-live-csrf-value')],
197209
]);

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testCanExecuteComponentActionNormalRoute(): void
8585
})
8686
->post('/_components/component2/increase', [
8787
'headers' => ['X-CSRF-TOKEN' => $token],
88-
'body' => json_encode(['props' => $dehydrated->getProps()]),
88+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
8989
])
9090
->assertSuccessful()
9191
->assertHeaderContains('Content-Type', 'html')
@@ -109,7 +109,7 @@ public function testCanExecuteComponentActionWithAlternateRoute(): void
109109
})
110110
->post('/alt/alternate_route/increase', [
111111
'headers' => ['X-CSRF-TOKEN' => $token],
112-
'body' => json_encode(['props' => $dehydrated->getProps()]),
112+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
113113
])
114114
->assertSuccessful()
115115
->assertOn('/alt/alternate_route/increase')
@@ -182,7 +182,7 @@ public function testDisabledCsrfTokenForComponentDoesNotFail(): void
182182
->assertHeaderContains('Content-Type', 'html')
183183
->assertContains('Count: 1')
184184
->post('/_components/disabled_csrf/increase', [
185-
'body' => json_encode(['props' => $dehydrated->getProps()]),
185+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
186186
])
187187
->assertSuccessful()
188188
->assertHeaderContains('Content-Type', 'html')
@@ -221,7 +221,7 @@ public function testCanRedirectFromComponentAction(): void
221221
// with no custom header, it redirects like a normal browser
222222
->post('/_components/component2/redirect', [
223223
'headers' => ['X-CSRF-TOKEN' => $token],
224-
'body' => json_encode(['props' => $dehydrated->getProps()]),
224+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
225225
])
226226
->assertRedirectedTo('/')
227227

@@ -231,7 +231,7 @@ public function testCanRedirectFromComponentAction(): void
231231
'Accept' => 'application/vnd.live-component+html',
232232
'X-CSRF-TOKEN' => $token,
233233
],
234-
'body' => json_encode(['props' => $dehydrated->getProps()]),
234+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
235235
])
236236
->assertStatus(204)
237237
->assertHeaderEquals('Location', '/')
@@ -260,10 +260,12 @@ public function testInjectsLiveArgs(): void
260260
})
261261
->post('/_components/component6/inject', [
262262
'headers' => ['X-CSRF-TOKEN' => $token],
263-
'body' => json_encode([
264-
'props' => $dehydrated->getProps(),
265-
'args' => $arguments,
266-
]),
263+
'body' => [
264+
'data' => json_encode([
265+
'props' => $dehydrated->getProps(),
266+
'args' => $arguments,
267+
]),
268+
],
267269
])
268270
->assertSuccessful()
269271
->assertHeaderContains('Content-Type', 'html')

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testFormValuesRebuildAfterFormChanges(): void
5353
$crawler = $browser
5454
// post to action, which will add a new embedded comment
5555
->post('/_components/form_with_collection_type/addComment', [
56-
'body' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps]),
56+
'body' => ['data' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps])],
5757
'headers' => ['X-CSRF-TOKEN' => $token],
5858
])
5959
->assertStatus(422)
@@ -89,7 +89,7 @@ public function testFormValuesRebuildAfterFormChanges(): void
8989
$crawler = $browser
9090
// post to action, which will remove the original embedded comment
9191
->post('/_components/form_with_collection_type/removeComment', [
92-
'body' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['index' => '0']]),
92+
'body' => ['data' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['index' => '0']])],
9393
'headers' => ['X-CSRF-TOKEN' => $token],
9494
])
9595
->assertStatus(422)
@@ -266,7 +266,7 @@ public function testLiveCollectionTypeFieldsAddedAndRemoved(): void
266266
})
267267
// post to action, which will add a new embedded comment
268268
->post('/_components/form_with_live_collection_type/addCollectionItem', [
269-
'body' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['name' => 'blog_post_form[comments]']]),
269+
'body' => ['data' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['name' => 'blog_post_form[comments]']])],
270270
'headers' => ['X-CSRF-TOKEN' => $token],
271271
])
272272
->assertStatus(422)
@@ -303,7 +303,7 @@ public function testLiveCollectionTypeFieldsAddedAndRemoved(): void
303303

304304
// post to action, which will remove the original embedded comment
305305
->post('/_components/form_with_live_collection_type/removeCollectionItem', [
306-
'body' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['name' => 'blog_post_form[comments]', 'index' => '0']]),
306+
'body' => ['data' => json_encode(['props' => $dehydratedProps, 'updated' => $updatedProps, 'args' => ['name' => 'blog_post_form[comments]', 'index' => '0']])],
307307
'headers' => ['X-CSRF-TOKEN' => $token],
308308
])
309309
->assertStatus(422)

src/LiveComponent/tests/Functional/LiveResponderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testComponentCanEmitEvents(): void
3131
$this->browser()
3232
->throwExceptions()
3333
->post('/_components/component_with_emit/actionThatEmits', [
34-
'body' => json_encode(['props' => $dehydrated->getProps()]),
34+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
3535
])
3636
->assertSuccessful()
3737
->assertSee('Event: event1')
@@ -46,7 +46,7 @@ public function testComponentCanDispatchBrowserEvents(): void
4646
$crawler = $this->browser()
4747
->throwExceptions()
4848
->post('/_components/component_with_emit/actionThatDispatchesABrowserEvent', [
49-
'body' => json_encode(['props' => $dehydrated->getProps()]),
49+
'body' => ['data' => json_encode(['props' => $dehydrated->getProps()])],
5050
])
5151
->assertSuccessful()
5252
->crawler()

src/LiveComponent/tests/Functional/ValidatableComponentTraitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ public function testFormValuesRebuildAfterFormChanges(): void
6262

6363
$browser
6464
->post('/_components/validating_component/resetValidationAction', [
65-
'json' => [
66-
'props' => $dehydratedProps,
65+
'body' => [
66+
'data' => json_encode([
67+
'props' => $dehydratedProps,
68+
]),
6769
],
6870
])
6971
->assertSuccessful()

0 commit comments

Comments
 (0)