diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 3f09854ac3221..945103371d5a4 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 6.4 --- + * Add `Request::resetFormats()` internal method to reset the list of supported format to MIME type mappings * Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable` * Support root-level `Generator` in `StreamedJsonResponse` * Add `UriSigner` from the HttpKernel component diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 922014133293e..c7decbdf003de 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1749,6 +1749,15 @@ public function preferSafeContent(): bool return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe'); } + /** + * Resets the mappings of formats to mime types. + * @internal + */ + public static function resetFormats(): void + { + static::initializeFormats(); + } + /* * The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24) * diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 7a4807ecf721e..06bedb287b2d0 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -33,6 +33,16 @@ protected function tearDown(): void Request::setTrustedHosts([]); } + public function testResetFormats() + { + $request = new Request(); + $request->setFormat('json', ['application/problem+json']); + $modifiedRequestFormat = $request->getFormat('application/json'); + $this->assertNull($modifiedRequestFormat); + Request::resetFormats(); + $this->assertEquals('json', $request->getFormat('application/json')); + } + public function testInitialize() { $request = new Request(); diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index c1743b1d141b8..98886ce397ab6 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 6.4 --- + * Add call to `Request::resetFormats()` in `Kernel::boot()` to reset the request formats at the start of a new main request. * Support backed enums in #[MapQueryParameter] * `BundleInterface` no longer extends `ContainerAwareInterface` * Add optional `$className` parameter to `ControllerEvent::getAttributes()` diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 185c9686aa097..46dbdfd345018 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -110,6 +110,9 @@ public function boot() { if (true === $this->booted) { if (!$this->requestStackSize && $this->resetServices) { + if (method_exists(Request::class, 'resetFormats')) { + Request::resetFormats(); + } if ($this->container->has('services_resetter')) { $this->container->get('services_resetter')->reset(); } diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index 94c498a0f2808..dfd829ed67b6d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -121,6 +121,34 @@ public function testInitializeContainerClearsOldContainers() $this->assertFileDoesNotExist($legacyContainerDir.'.legacy'); } + public function testBootResetsRequestFormatsOnReset() + { + $request = new Request(); + $request->setFormat('json', ['application/problem+json']); + $modifiedRequestFormat = $request->getFormat('application/json'); + + $httpKernelMock = $this->getMockBuilder(HttpKernel::class) + ->disableOriginalConstructor() + ->getMock(); + + $httpKernelMock + ->expects($this->any()) + ->method('handle') + ->with($request); + + $kernel = $this->getKernel(['getHttpKernel']); + $kernel->expects($this->any()) + ->method('getHttpKernel') + ->willReturn($httpKernelMock); + + $kernel->handle($request); + + $kernel->boot(); + $kernel->handle($request); + $this->assertNull($modifiedRequestFormat); + $this->assertSame('json', $request->getFormat('application/json')); + } + public function testBootInitializesBundlesAndContainer() { $kernel = $this->getKernel(['initializeBundles']);