From 0ccffd5b82446bde6c3f03fe2a2ae5ec0c3d5612 Mon Sep 17 00:00:00 2001 From: David Gebler Date: Sat, 30 Nov 2024 00:04:38 +0000 Subject: [PATCH 1/2] [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 | 5 ++++ .../Component/HttpFoundation/Request.php | 8 ++++++ .../HttpFoundation/Tests/RequestTest.php | 10 +++++++ src/Symfony/Component/HttpKernel/CHANGELOG.md | 5 ++++ src/Symfony/Component/HttpKernel/Kernel.php | 1 + .../Component/HttpKernel/Tests/KernelTest.php | 28 +++++++++++++++++++ 6 files changed, 57 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 6616aa0adfed3..69602f4bf074d 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + + * Add `Request::resetFormats()` method to reset the list of supported format to MIME type mappings + 7.2 --- diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index db78105cc83cf..02bbc6b52afba 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1731,6 +1731,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 d5a41390e1b5d..74cf5c743e764 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -31,6 +31,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 1fc103b48dc1a..96f162a8ed76a 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + + * Add call to `Request::resetFormats()` in `Kernel::boot()` to reset the request formats at the start of a new main request. + 7.2 --- diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index ec5e3b0df3f20..d724bd5efd949 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -104,6 +104,7 @@ public function boot(): void { 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 f650c33ee0214..cf051e2c7d2e2 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -113,6 +113,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 5a3cf9c0b09e6a90e7e2825e2ee7570a34db4b06 Mon Sep 17 00:00:00 2001 From: dwgebler Date: Sun, 1 Dec 2024 12:59:45 +0000 Subject: [PATCH 2/2] [HttpFoundation] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix typo in function docblock Co-authored-by: Kévin Dunglas --- src/Symfony/Component/HttpFoundation/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 02bbc6b52afba..f32c29968be5c 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1732,7 +1732,7 @@ 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 {