Skip to content

Commit

Permalink
Merge branch 'paul/body-parser-patch' into 3.x
Browse files Browse the repository at this point in the history
Closes #2538
  • Loading branch information
akrabat committed Nov 27, 2018
2 parents c7ae7b7 + 013e1e8 commit 62a1434
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Slim/Http/Request.php
Expand Up @@ -1030,13 +1030,16 @@ public function getParsedBody()

$mediaType = $this->getMediaType();

// look for a media type with a structured syntax suffix (RFC 6839)
$parts = explode('+', $mediaType);
if (count($parts) >= 2) {
$mediaType = 'application/' . $parts[count($parts)-1];
// Check if this specific media type has a parser registered first
if (!isset($this->bodyParsers[$mediaType])) {
// If not, look for a media type with a structured syntax suffix (RFC 6839)
$parts = explode('+', $mediaType);
if (count($parts) >= 2) {
$mediaType = 'application/' . $parts[count($parts)-1];
}
}

if (isset($this->bodyParsers[$mediaType]) === true) {
if (isset($this->bodyParsers[$mediaType])) {
$body = (string)$this->getBody();
$parsed = $this->bodyParsers[$mediaType]($body);

Expand Down
19 changes: 19 additions & 0 deletions tests/Http/RequestTest.php
Expand Up @@ -940,6 +940,25 @@ public function testGetParsedBodyWithJsonStructuredSuffix()
$this->assertEquals(['foo' => 'bar'], $request->getParsedBody());
}

public function testGetParsedBodyWithJsonStructuredSuffixAndRegisteredParser()
{
$method = 'GET';
$uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', '');
$headers = new Headers();
$headers->set('Content-Type', 'application/vnd.api+json;charset=utf8');
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$body->write('{"foo":"bar"}');
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);

$request->registerMediaTypeParser('application/vnd.api+json', function ($input) {
return ['data' => $input];
});

$this->assertEquals(['data' => '{"foo":"bar"}'], $request->getParsedBody());
}

public function testGetParsedBodyXml()
{
$method = 'GET';
Expand Down

0 comments on commit 62a1434

Please sign in to comment.