Skip to content

Support non pre-parsed PSR-7 request body#202

Merged
vladar merged 1 commit intowebonyx:masterfrom
PowerKiKi:support-non-parsed-psr7
Nov 28, 2017
Merged

Support non pre-parsed PSR-7 request body#202
vladar merged 1 commit intowebonyx:masterfrom
PowerKiKi:support-non-parsed-psr7

Conversation

@PowerKiKi
Copy link
Copy Markdown
Contributor

Because PSR-7 specification only specify that getParsedBody() may
return the parsed body for application/json, we cannot assume that it
is always the case. So if the value returned parsed body is an empty array,
it means we should try to parse it ourselves (null would mean no body at
all according to spec).

With this modification we try to used given parsed body, but fallback on
trying to parse the body if necessary. This leave the door open to custom
implementation of parsing if needed, while making it easier to use out of
the box.

This is in direct response to #120 (comment)

Because PSR-7 specification only specify that `getParsedBody()` **may**
return the parsed body for `application/json`, we cannot assume that it
is always the case. So if the value returned parsed body is an empty array,
it means we should try to parse it ourselves (`null` would mean no body at
all according to spec).

With this modification we try to used given parsed body, but fallback on
trying to parse the body if necessary. This leave the door open to custom
implementation of parsing if needed, while making it easier to use out of
the box.
@coveralls
Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.003%) to 92.649% when pulling 11c9429 on PowerKiKi:support-non-parsed-psr7 into eaadae4 on webonyx:master.


if (empty($bodyParams)) {
$bodyParams = json_decode($request->getBody(), true);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Empty is a bit too loose. I guess we should check for null only. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is intended, as mentioned in commit message a null value would mean no body at all:

A null value indicates the absence of body content.

http://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface

So we test for an empty array, to try to parse the body ourselves. But if it was null it should throw an exception. IIRC the null case is correctly covered by pre-existing tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, you are right

@vladar vladar merged commit 9d37f4c into webonyx:master Nov 28, 2017
vladar added a commit that referenced this pull request Nov 28, 2017
@PowerKiKi PowerKiKi deleted the support-non-parsed-psr7 branch December 18, 2017 12:26
PowerKiKi added a commit to PowerKiKi/graphql-php that referenced this pull request Dec 21, 2017
This revert webonyx#202 (commit 9d37f4c) because trying to parse PSR-7 request
was a mistake. The whole point of PSR-7 is to allow for interoperability
and be able to use specialized libs for body parsing (amongst many other
things). Trying to parse ourselves would be opening a can of worm if/when
other content types have to be supported. It is more correct and future
safe to require that the body is parsed before being passed to GraphQL.
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.

3 participants