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

parse body if empty and has media type detected #100

Merged
merged 1 commit into from
Aug 29, 2019

Conversation

esetnik
Copy link
Contributor

@esetnik esetnik commented Aug 7, 2019

Fixes #99

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 15617fa on esetnik:bugfix/parsed-body into 52693db on slimphp:master.

@@ -162,13 +162,13 @@ public function getParsedBody()
{
$parsedBody = $this->serverRequest->getParsedBody();

if ($parsedBody !== null) {
if (!empty($parsedBody)) {
return $parsedBody;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't think this is the correct solution. nyholm/psr7 should be updated to not automatically set the parsed body, per PSR-7 spec:

     * If the request Content-Type is either application/x-www-form-urlencoded
     * or multipart/form-data, and the request method is POST, this method MUST
     * return the contents of $_POST.
     *
     * Otherwise, this method may return any results of deserializing
     * the request body content; as parsing returns structured content, the
     * potential types MUST be arrays or objects only. A null value indicates
     * the absence of body content.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, nyholm/psr7 is correct implementation. In the case of a json $_POST they should not send null they should send empty array. See the discussion in #99.

Copy link
Member

@l0gicgate l0gicgate Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do a null check based on content type then. If it is application/x-www-form-urlencoded or multipart/form-data we do a null check and return the implementation’s parsed body if not null otherwise parse the body.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@l0gicgate Slim doesn't need to solve this problem, IMO. It is a bug in nyholm/psr7, which is clearly not following the spec by blindly setting withParsedBody($post) without checking the request Content-Type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s also a problem with Zend-Diactoros.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, if the problem exists beyond one implementation then I agree this is a reasonable solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the spec wasn't really considering the possibility that multiple layers would attempt to parse the body. There's no way to pass information on whether body parsing was attempted already.

@@ -162,13 +162,13 @@ public function getParsedBody()
{
$parsedBody = $this->serverRequest->getParsedBody();

if ($parsedBody !== null) {
if (!empty($parsedBody)) {
return $parsedBody;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, if the problem exists beyond one implementation then I agree this is a reasonable solution.

@akrabat
Copy link
Member

akrabat commented Aug 16, 2019

As an aside, it's nice that Slim-Psr7's ServerRequestFactory gets this right!

@esetnik
Copy link
Contributor Author

esetnik commented Aug 26, 2019

Is there anything else I should do with this PR to get it merged?

@l0gicgate l0gicgate added this to the 0.8 milestone Aug 29, 2019
@l0gicgate l0gicgate merged commit a614882 into slimphp:master Aug 29, 2019
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

Successfully merging this pull request may close these issues.

getParsedBody() not working in slim v4
5 participants