8000 bug #21805 Provide less state in getRequestFormat (dawehner) · symfony/symfony@d45008d · GitHub
[go: up one dir, main page]

Skip to content

Commit d45008d

Browse files
bug #21805 Provide less state in getRequestFormat (dawehner)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #21805). Discussion ---------- Provide less state in getRequestFormat Let's assume you multiple lines of code calling to ```Request::getRequestFormat```providing different default values. ``` $request->getRequestFormat(); $request->getRequestFormat('json') ``` As of HEAD this causes the second call to return 'html', unless its set explicit via ```setRequestFormat()```. IMHO this is state which can be avoided. Note: This also helps Drupal. | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | | License | MIT | Doc PR | Commits ------- 1d43007 Provide less state in getRequestFormat
2 parents 5898ec2 + 1d43007 commit d45008d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,10 +1382,10 @@ public function setFormat($format, $mimeTypes)
13821382
public function getRequestFormat($default = 'html')
13831383
{
13841384
if (null === $this->format) {
1385-
$this->format = $this->get('_format', $default);
1385+
$this->format = $this->get('_format');
13861386
}
13871387

1388-
return $this->format;
1388+
return null === $this->format ? $default : $this->format;
13891389
}
13901390

13911391
/**
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,11 @@ public function testGetRequestFormat()
14011401
$request = new Request();
14021402
$this->assertEquals('html', $request->getRequestFormat());
14031403

1404+
// Ensure that setting different default values over time is possible,
1405+
// aka. setRequestFormat determines the state.
1406+
$this->assertEquals('json', $request->getRequestFormat('json'));
1407+
$this->assertEquals('html', $request->getRequestFormat('html'));
1408+
14041409
$request = new Request();
14051410
$this->assertNull($request->getRequestFormat(null));
14061411

0