Skip to content

Commit 60d997d

Browse files
dunglasfabpot
authored andcommitted
[HttpFoundation] Accept must take the lead for Request::getPreferredFormat()
1 parent b79a1bf commit 60d997d

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,8 @@ public function setFormat($format, $mimeTypes)
13471347
* * _format request attribute
13481348
* * $default
13491349
*
1350+
* @see getPreferredFormat
1351+
*
13501352
* @param string|null $default The default format
13511353
*
13521354
* @return string|null The request format
@@ -1563,22 +1565,27 @@ public function isNoCache()
15631565
return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
15641566
}
15651567

1568+
/**
1569+
* Gets the preferred format for the response by inspecting, in the following order:
1570+
* * the request format set using setRequestFormat
1571+
* * the values of the Accept HTTP header
1572+
* * the content type of the body of the request.
1573+
*/
15661574
public function getPreferredFormat(?string $default = 'html'): ?string
15671575
{
15681576
if (null !== $this->preferredFormat) {
15691577
return $this->preferredFormat;
15701578
}
15711579

1572-
$this->preferredFormat = $this->getRequestFormat($this->getContentType());
1573-
1574-
if (null === $this->preferredFormat) {
1575-
foreach ($this->getAcceptableContentTypes() as $contentType) {
1576-
if (null !== $this->preferredFormat = $this->getFormat($contentType)) {
1577-
break;
1578-
}
1580+
$preferredFormat = null;
1581+
foreach ($this->getAcceptableContentTypes() as $contentType) {
1582+
if ($preferredFormat = $this->getFormat($contentType)) {
1583+
break;
15791584
}
15801585
}
15811586

1587+
$this->preferredFormat = $this->getRequestFormat($preferredFormat ?: $this->getContentType());
1588+
15821589
return $this->preferredFormat ?: $default;
15831590
}
15841591

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ public function testGetPreferredFormat()
407407
$this->assertSame('json', $request->getPreferredFormat('json'));
408408

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

414414
$request = new Request();
415-
$request->headers->set('Content-Type', 'application/json');
416415
$request->headers->set('Accept', 'application/xml');
417-
$this->assertSame('json', $request->getPreferredFormat());
416+
$request->headers->set('Content-Type', 'application/json');
417+
$this->assertSame('xml', $request->getPreferredFormat());
418418

419419
$request = new Request();
420420
$request->headers->set('Accept', 'application/xml');

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ public function testPrepareSetContentType()
504504
$response = new Response('foo');
505505
$request = Request::create('/');
506506
$request->setRequestFormat('json');
507+
$request->headers->remove('accept');
507508

508509
$response->prepare($request);
509510

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy