Skip to content

Commit 88470cc

Browse files
committed
bug #2255 do not override the status code if it is already set (xabbuh)
This PR was merged into the 2.x branch. Discussion ---------- do not override the status code if it is already set Commits ------- e8d0314 do not override the status code if it is already set
2 parents ae1db28 + e8d0314 commit 88470cc

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.8.2
55
-----
66

7+
* fixed the `ViewHandler` to not override an already set `status_code` in the serialization context
78
* fixed embedding status codes in the response body when a mapping of exception classes to status
89
codes is configured
910

Tests/View/ViewHandlerTest.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace FOS\RestBundle\Tests\View;
1313

14+
use FOS\RestBundle\Context\Context;
1415
use FOS\RestBundle\Serializer\Serializer;
1516
use FOS\RestBundle\View\View;
1617
use FOS\RestBundle\View\ViewHandler;
1718
use PHPUnit\Framework\TestCase;
1819
use Symfony\Component\Form\Extension\Core\Type\TextType;
1920
use Symfony\Component\Form\Form;
2021
use Symfony\Component\Form\FormError;
22+
use Symfony\Component\Form\FormInterface;
2123
use Symfony\Component\Form\Forms;
2224
use Symfony\Component\Form\FormView;
2325
use Symfony\Component\HttpFoundation\Request;
@@ -97,24 +99,16 @@ public function testGetStatusCode(
9799
$data,
98100
$isSubmitted,
99101
$isValid,
100-
$isSubmittedCalled,
101-
$isValidCalled,
102102
$noContentCode,
103103
$actualStatusCode = null
104104
) {
105-
$reflectionMethod = new \ReflectionMethod(ViewHandler::class, 'getStatusCode');
106-
$reflectionMethod->setAccessible(true);
107-
108-
$form = $this->getMockBuilder(Form::class)
109-
->disableOriginalConstructor()
110-
->setMethods(array('isSubmitted', 'isValid'))
111-
->getMock();
105+
$form = $this->createMock(FormInterface::class);
112106
$form
113-
->expects($this->exactly($isSubmittedCalled))
107+
->expects($this->any())
114108
->method('isSubmitted')
115109
->will($this->returnValue($isSubmitted));
116110
$form
117-
->expects($this->exactly($isValidCalled))
111+
->expects($this->any())
118112
->method('isValid')
119113
->will($this->returnValue($isValid));
120114

@@ -123,20 +117,36 @@ public function testGetStatusCode(
123117
}
124118
$view = new View($data ?: null, $actualStatusCode ?: null);
125119

120+
if ($data) {
121+
$expectedContext = new Context();
122+
$expectedContext->setAttribute('template_data', []);
123+
124+
if (null !== $actualStatusCode || $form->isSubmitted() && !$form->isValid()) {
125+
$expectedContext->setAttribute('status_code', $expected);
126+
}
127+
128+
$this->serializer
129+
->expects($this->once())
130+
->method('serialize')
131+
->with($form, 'json', $expectedContext)
132+
->willReturn(json_encode(['code' => $expected, 'message' => $isValid ? 'OK' : 'Validation failed']));
133+
}
134+
126135
$viewHandler = $this->createViewHandler([], $expected, $noContentCode);
127-
$this->assertEquals($expected, $reflectionMethod->invoke($viewHandler, $view, $view->getData()));
136+
$this->assertEquals($expected, $viewHandler->createResponse($view, new Request(), 'json')->getStatusCode());
128137
}
129138

130139
public static function getStatusCodeDataProvider()
131140
{
132141
return [
133-
'no data' => [Response::HTTP_OK, false, false, false, 0, 0, Response::HTTP_OK],
134-
'no data with 204' => [Response::HTTP_NO_CONTENT, false, false, false, 0, 0, Response::HTTP_NO_CONTENT],
135-
'no data, but custom response code' => [Response::HTTP_OK, false, false, false, 0, 0, Response::HTTP_NO_CONTENT, Response::HTTP_OK],
136-
'form key form not bound' => [Response::HTTP_OK, true, false, true, 1, 0, Response::HTTP_OK],
137-
'form key form is bound and invalid' => [Response::HTTP_FORBIDDEN, true, true, false, 1, 1, Response::HTTP_OK],
138-
'form key form bound and valid' => [Response::HTTP_OK, true, true, true, 1, 1, Response::HTTP_OK],
139-
'form key null form bound and valid' => [Response::HTTP_OK, true, true, true, 1, 1, Response::HTTP_OK],
142+
'no data' => [Response::HTTP_OK, false, false, false, Response::HTTP_OK],
143+
'no data with 204' => [Response::HTTP_NO_CONTENT, false, false, false, Response::HTTP_NO_CONTENT],
144+
'no data, but custom response code' => [Response::HTTP_OK, false, false, false, Response::HTTP_NO_CONTENT, Response::HTTP_OK],
145+
'form key form not bound' => [Response::HTTP_OK, true, false, true, Response::HTTP_OK],
146+
'form key form is bound and invalid' => [Response::HTTP_FORBIDDEN, true, true, false, Response::HTTP_OK],
147+
'form key form is bound and invalid and status code in view is set' => [Response::HTTP_FORBIDDEN, true, true, false, Response::HTTP_OK, Response::HTTP_CREATED],
148+
'form key form bound and valid' => [Response::HTTP_OK, true, true, true, Response::HTTP_OK],
149+
'form key null form bound and valid' => [Response::HTTP_OK, true, true, true, Response::HTTP_OK],
140150
];
141151
}
142152

View/ViewHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ protected function getSerializationContext(View $view)
273273
$context->setSerializeNull($this->serializeNullStrategy);
274274
}
275275

276-
if (null !== $view->getStatusCode()) {
276+
if (null !== $view->getStatusCode() && !$context->hasAttribute('status_code')) {
277277
$context->setAttribute('status_code', $view->getStatusCode());
278278
}
279279

0 commit comments

Comments
 (0)