Closed
Description
Symfony version(s) affected
7.1.x
Description
I'm trying to use #[MapUploadedFile]
for a POST route with multipart/form-data
.
When I pass a file it's working as expected, but when I try to make it optional, the test fails because it receives an array while waiting for null | UploadedFile
.
Is it expected? Maybe I'm just wrongly using the browser client to make a request without files?
How to reproduce
Controller
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
use Symfony\Component\Routing\Annotation\Route;
class UpdateController extends AbstractController
{
#[Route('/', name: 'update', methods: ['POST'])]
public function index(
#[MapUploadedFile]
?UploadedFile $file,
): JsonResponse {
dump($file);
return $this->json('', Response::HTTP_OK);
}
}
Test
<?php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UpdateControllerTest extends WebTestCase
{
public function testPictureIsOptionnal(
): void {
$client = static::createClient();
$crawler = $client->request('POST', '/', [], [], ['CONTENT_TYPE' => 'multipart/form-data']);
self::assertResponseIsSuccessful();
}
}
Error
ErrorException: App\Controller\UpdateController::index(): Argument #1 ($uploadedPicture) must be of type ?Symfony\Component\HttpFoundation\File\UploadedFile, array given, called in /var/www/html/vendor/symfony/http-kernel/HttpKernel.php on line 183 in /var/www/html/src/Controller/UpdateController.php:1
Possible Solution
No response
Additional Context
No response