Skip to content

[HttpFoundation] Accept must take the lead for Request::getPreferredFormat() #32348

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

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,8 @@ public function setFormat($format, $mimeTypes)
* * _format request attribute
* * $default
*
* @see getPreferredFormat
*
* @param string|null $default The default format
*
* @return string|null The request format
Expand Down Expand Up @@ -1563,22 +1565,27 @@ public function isNoCache()
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
}

/**
* Gets the preferred format for the response by inspecting, in the following order:
* * the request format set using setRequestFormat
* * the values of the Accept HTTP header
* * the content type of the body of the request.
*/
public function getPreferredFormat(?string $default = 'html'): ?string
{
if (null !== $this->preferredFormat) {
return $this->preferredFormat;
}

$this->preferredFormat = $this->getRequestFormat($this->getContentType());

if (null === $this->preferredFormat) {
foreach ($this->getAcceptableContentTypes() as $contentType) {
if (null !== $this->preferredFormat = $this->getFormat($contentType)) {
break;
}
$preferredFormat = null;
foreach ($this->getAcceptableContentTypes() as $contentType) {
if ($preferredFormat = $this->getFormat($contentType)) {
break;
}
}

$this->preferredFormat = $this->getRequestFormat($preferredFormat ?: $this->getContentType());

return $this->preferredFormat ?: $default;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ public function testGetPreferredFormat()
$this->assertSame('json', $request->getPreferredFormat('json'));

$request->setRequestFormat('atom');
$request->headers->set('Content-Type', 'application/json');
$request->headers->set('Accept', 'application/xml');
$request->headers->set('Accept', 'application/ld+json');
$request->headers->set('Content-Type', 'application/merge-patch+json');
$this->assertSame('atom', $request->getPreferredFormat());

$request = new Request();
$request->headers->set('Content-Type', 'application/json');
$request->headers->set('Accept', 'application/xml');
$this->assertSame('json', $request->getPreferredFormat());
$request->headers->set('Content-Type', 'application/json');
$this->assertSame('xml', $request->getPreferredFormat());

$request = new Request();
$request->headers->set('Accept', 'application/xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ public function testPrepareSetContentType()
$response = new Response('foo');
$request = Request::create('/');
$request->setRequestFormat('json');
$request->headers->remove('accept');

$response->prepare($request);

Expand Down