Skip to content

Commit c3f0978

Browse files
Merge branch '6.4' into 7.3
* 6.4: [Cache] Fix accepting named closures as early-expiration callbacks [Mime] Update mime types [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 dd815b1 + 4e623d4 commit c3f0978

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
@@ -227,6 +227,37 @@ public function testJsonWithSerializerContextOverride()
227227
$this->assertEquals('{}', $response->getContent());
228228
}
229229

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

0 commit comments

Comments
 (0)