File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
src/Illuminate/Http/Client Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 88use GuzzleHttp \Exception \RequestException ;
99use GuzzleHttp \Exception \TransferException ;
1010use GuzzleHttp \HandlerStack ;
11+ use Illuminate \Contracts \Support \Arrayable ;
1112use Illuminate \Http \Client \Events \ConnectionFailed ;
1213use Illuminate \Http \Client \Events \RequestSending ;
1314use Illuminate \Http \Client \Events \ResponseReceived ;
@@ -747,7 +748,13 @@ protected function parseHttpOptions(array $options)
747748 $ options [$ this ->bodyFormat ] = $ this ->pendingBody ;
748749 }
749750
750- return collect ($ options )->toArray ();
751+ return collect ($ options )->map (function ($ value , $ key ) {
752+ if ($ key === 'json ' && $ value instanceof JsonSerializable) {
753+ return $ value ;
754+ }
755+
756+ return $ value instanceof Arrayable ? $ value ->toArray () : $ value ;
757+ })->all ();
751758 }
752759
753760 /**
Original file line number Diff line number Diff line change 66use GuzzleHttp \Promise \PromiseInterface ;
77use GuzzleHttp \Psr7 \Response as Psr7Response ;
88use Illuminate \Contracts \Events \Dispatcher ;
9+ use Illuminate \Contracts \Support \Arrayable ;
910use Illuminate \Http \Client \Events \RequestSending ;
1011use Illuminate \Http \Client \Events \ResponseReceived ;
1112use Illuminate \Http \Client \Factory ;
@@ -221,6 +222,34 @@ public function jsonSerialize(): mixed
221222 });
222223 }
223224
225+ public function testPrefersJsonSerializableOverArrayableData ()
226+ {
227+ $ this ->factory ->fake ();
228+
229+ $ this ->factory ->asJson ()->post ('http://foo.com/form ' , new class implements JsonSerializable, Arrayable
230+ {
231+ public function jsonSerialize (): mixed
232+ {
233+ return [
234+ 'attributes ' => (object ) [],
235+ ];
236+ }
237+
238+ public function toArray (): array
239+ {
240+ return [
241+ 'attributes ' => [],
242+ ];
243+ }
244+ });
245+
246+ $ this ->factory ->assertSent (function (Request $ request ) {
247+ return $ request ->url () === 'http://foo.com/form ' &&
248+ $ request ->hasHeader ('Content-Type ' , 'application/json ' ) &&
249+ $ request ->body () === '{"attributes":{}} ' ;
250+ });
251+ }
252+
224253 public function testRecordedCallsAreEmptiedWhenFakeIsCalled ()
225254 {
226255 $ this ->factory ->fake ([
You can’t perform that action at this time.
0 commit comments