Skip to content

[HttpFoundation] Default accessing structured request body to empty array #50664

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

Closed
wants to merge 2 commits into from
Closed

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Jun 14, 2023

Q A
Branch? 6.3
Bug fix? yes-ish
New feature? no
Deprecations? no
Tickets Fix #50647
License MIT
Doc PR symfony/symfony-docs#...

Similar like in PHP $_POST is always type array, and in Symfony $request->request is always array-able

The state can be checked by other means (headers, method, etc.), if nessecary

@carsonbot carsonbot added this to the 6.3 milestone Jun 14, 2023
@ro0NL ro0NL changed the title [HttpFoundation] Default structured request body to empty array [HttpFoundation] Default accessing structured request body to empty array Jun 14, 2023
@stof
Copy link
Member

stof commented Jun 14, 2023

I suggest changing that only for getPayload.

toArray is only doing json-decoding, and an empty body is not valid json.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

i consider toArray / getPayload equivalent public API :)

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

i mean, from the original issue, a throwing $r->getPayload()->get('key') would be equally annoying as a throwing $r->toArray()['key'] 🤔

perhaps both POVs are equally valid, let me know if we prefer a getPayload specific solution 👍

toArray is only doing json-decoding

it's kinda an implementation detail ;)

@stof
Copy link
Member

stof commented Jun 14, 2023

@ro0NL if you don't know that the body is valid json, toArray might still throw for something else than an empty body. So I don't see how this changes much things.

note that HttpKernel will automatically convert those exceptions to a 4xx response.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

it enables doing if ($r->toArray()['key']) over if ($r->getContent && $r->toArray()['key'])

if the request content is actually corrupt, let it be bad JSON, then we should throw (which hasnt changed here)

my point here is, toArray/getPayload should not be used for state checking IMHO, but a route/controller condition should

@stof
Copy link
Member

stof commented Jun 14, 2023

@ro0NL if your endpoint expects JSON, sending an empty body is actually a corrupt request.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

im still not sure toArray conveys your endpoint expects JSON, at least not by its name 🙄

to me, and if i recall it correctly, it was designed to be an implementation detail

btw, see also #50492 (comment) 😅

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

TLDR: toArray to me is a poor-man getPayload solution, and should have been deprecated

@stof
Copy link
Member

stof commented Jun 14, 2023

@ro0NL we explicitly decided not to deprecate it immediately to make things easier for the ecosystem needing to support multiple version of Symfony.

I would expect us to deprecate it in 7.1.

@ro0NL
Copy link
Contributor Author

ro0NL commented Jun 14, 2023

Gets the request body decoded as array, typically from a JSON payload

to me proves JSON is an implementation detail

in that sense the method is already bugged for not returning $_POST

im gonna wait for toArray deprecation, so we can avoid this debate

@ro0NL ro0NL closed this Jun 14, 2023
@ro0NL ro0NL deleted the patch-1 branch June 14, 2023 20:08
@ro0NL
Copy link
Contributor Author

ro0NL commented Aug 19, 2024

@stof

I would expect us to deprecate it in 7.1.

we should get rid of it

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

Successfully merging this pull request may close these issues.

3 participants