Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel casting empty JSON objects as empty arrays on the Illuminate\Http\Request class #43513

Closed
lgrassini opened this issue Aug 2, 2022 · 1 comment

Comments

@lgrassini
Copy link

  • Laravel Version: 9.22.1
  • PHP Version: 8.0.2
  • Database Driver & Version: None

Description:

When sending a POST or PUT request providing JSON data in the body the Illuminate\Http\Request class will cast any empty JSON object as empty array.

Steps To Reproduce:

Create a simple route that will just output the received request:

    Route::post('/test', function (Request $request) {
        return $request;
    });

Send a POST or PUT request to the above created endpoint with a JSON payload containing an empty JSON object:

POST /api/test HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: 127.0.0.1:8000
Content-Length: 48

{"order_id":123,"customer_id":321,"products":{}}

The response of that request will be:

{
  "order_id": 123,
  "customer_id": 321,
  "products": []
}

Note how Illuminate\Http\Request is casting "products":{} as "products": [].
We are expecting the empty object products to be casted as it is, an empty JSON object, instead of an empty array.

This is also replicable on Laravel 8.x.
A similar issue was already reported here and closed without solution.

@driesvints
Copy link
Member

Requests are decoded as associative arrays. After doing that, there's no way of knowing what was an array and what was an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants