Skip to content

Commit 0bdcd24

Browse files
Merge branch '7.4' into 8.0
* 7.4: [Translation] Make the extractor alias optional [Cache] Fix accepting named closures as early-expiration callbacks [Mime] Update mime types [HttpKernel] Conflict with symfony/flex < 2.10 [Yaml] Align unquoted multiline scalar parsing with spec for comments work around limitation in JsonResponse when the data is null do not use recipient phone numbers as sender e-mail addresses [Dotenv] DotenvDumpCommand cannot be internal
2 parents 9c09ad2 + 7844097 commit 0bdcd24

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Controller/AbstractController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ protected function json(mixed $data, int $status = 200, array $headers = [], arr
155155
return new JsonResponse($json, $status, $headers, true);
156156
}
157157

158+
if (null === $data) {
159+
return new JsonResponse('null', $status, $headers, true);
160+
}
161+
158162
return new JsonResponse($data, $status, $headers);
159163
}
160164

Tests/Controller/AbstractControllerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,37 @@ public function testJsonWithSerializerContextOverride()
229229
$this->assertEquals('{}', $response->getContent());
230230
}
231231

232+
public function testJsonNull()
233+
{
234+
$controller = $this->createController();
235+
$controller->setContainer(new Container());
236+
237+
$response = $controller->json(null);
238+
$this->assertInstanceOf(JsonResponse::class, $response);
239+
$this->assertSame('null', $response->getContent());
240+
}
241+
242+
public function testJsonNullWithSerializer()
243+
{
244+
$container = new Container();
245+
246+
$serializer = $this->createMock(SerializerInterface::class);
247+
$serializer
248+
->expects($this->once())
249+
->method('serialize')
250+
->with(null, 'json', ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS])
251+
->willReturn('null');
252+
253+
$container->set('serializer', $serializer);
254+
255+
$controller = $this->createController();
256+
$controller->setContainer($container);
257+
258+
$response = $controller->json(null);
259+
$this->assertInstanceOf(JsonResponse::class, $response);
260+
$this->assertSame('null', $response->getContent());
261+
}
262+
232263
public function testFile()
233264
{
234265
$container = new Container();

0 commit comments

Comments
 (0)