From 34c64e1f182db9f49bfdd220cf5db14485ca98d3 Mon Sep 17 00:00:00 2001 From: David Gebler Date: Sat, 30 Nov 2024 00:04:38 +0000 Subject: [PATCH 1/3] [HttpFoundation] [HttpKernel] Add public static method to reset Request format to mime mappings Reset Request format to mime mappings on Kernel boot for each new main request --- .../Component/HttpFoundation/CHANGELOG.md | 1 + .../Component/HttpFoundation/Request.php | 8 ++++++ .../HttpFoundation/Tests/RequestTest.php | 10 +++++++ src/Symfony/Component/HttpKernel/CHANGELOG.md | 1 + src/Symfony/Component/HttpKernel/Kernel.php | 1 + .../Component/HttpKernel/Tests/KernelTest.php | 28 +++++++++++++++++++ 6 files changed, 49 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 3f09854ac3221..45cfdd4df03be 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()` 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..19c62564938c5 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1749,6 +1749,14 @@ public function preferSafeContent(): bool return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe'); } + /** + * Reset the mappings of formats to mime types. + */ + public static function resetFormats(): void + { + static::$formats = null; + } + /* * 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..928f61256a4e1 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -110,6 +110,7 @@ public function boot() { if (true === $this->booted) { if (!$this->requestStackSize && $this->resetServices) { + 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']); From 0ccb85852c762296093d20362f9671273d3c9524 Mon Sep 17 00:00:00 2001 From: David Gebler Date: Sun, 1 Dec 2024 13:25:47 +0000 Subject: [PATCH 2/3] [HttpFoundation] Fix typo in docblock comment Initialize $formats in reset function --- src/Symfony/Component/HttpFoundation/Request.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 19c62564938c5..592c8375bf2bc 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1750,11 +1750,11 @@ public function preferSafeContent(): bool } /** - * Reset the mappings of formats to mime types. + * Resets the mappings of formats to mime types. */ public static function resetFormats(): void { - static::$formats = null; + static::initializeFormats(); } /* From 0c59f2e7384c5ba02e79bb04eba166f4c8a342ea Mon Sep 17 00:00:00 2001 From: David Gebler Date: Fri, 6 Dec 2024 15:41:00 +0000 Subject: [PATCH 3/3] [HttpFoundation][HttpKernel] Mark Request::resetFormats as internal for 6.4 --- src/Symfony/Component/HttpFoundation/CHANGELOG.md | 2 +- src/Symfony/Component/HttpFoundation/Request.php | 1 + src/Symfony/Component/HttpKernel/Kernel.php | 4 +++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 45cfdd4df03be..945103371d5a4 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 6.4 --- - * Add `Request::resetFormats()` method to reset the list of supported format to MIME type mappings + * 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 592c8375bf2bc..c7decbdf003de 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1751,6 +1751,7 @@ public function preferSafeContent(): bool /** * Resets the mappings of formats to mime types. + * @internal */ public static function resetFormats(): void { diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 928f61256a4e1..46dbdfd345018 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -110,7 +110,9 @@ public function boot() { if (true === $this->booted) { if (!$this->requestStackSize && $this->resetServices) { - Request::resetFormats(); + if (method_exists(Request::class, 'resetFormats')) { + Request::resetFormats(); + } if ($this->container->has('services_resetter')) { $this->container->get('services_resetter')->reset(); }