flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.63k stars 407 forks source link

[2.x] Fix JSON request with empty body #457

Closed mu1f407 closed 2 years ago

mu1f407 commented 2 years ago

Hi,

this PR fixes bug introduced in #439. Previously (1.x) loose comparison was used to check if body is empty, now (2.0) strict comparison is used. This changes behaviour when JSON body is empty. In that case the Request::getBody() returns null. It is then passed to json_decode() which fails with error:

TypeError: json_decode(): Argument #1 ($json) must be of type string, null given

I fixed it with adding explicit $body !== null check. I also added test which can trigger this error. Some changes in RequestTest was needed though to tests work correctly (reset of some variables to default state).