From f545ed478f24513aac19839151606a0ca7b01967 Mon Sep 17 00:00:00 2001 From: Mark Pedron Date: Sat, 15 Jan 2022 19:29:43 +0100 Subject: [PATCH] Rename getContentType to getContentTypeFormat Resolves issue #39750. The method getContentType was confusing. This method does not return a mime type, but a mapped type name derived from the mime type in the CONTENT_TYPE header. --- UPGRADE-6.2.md | 5 +++++ .../Component/HttpFoundation/CHANGELOG.md | 1 + .../Component/HttpFoundation/Request.php | 16 +++++++++++++- .../HttpFoundation/Tests/RequestTest.php | 22 +++++++++++++++++++ .../Authenticator/FormLoginAuthenticator.php | 2 +- .../Authenticator/JsonLoginAuthenticator.php | 5 ++++- 6 files changed, 48 insertions(+), 3 deletions(-) diff --git a/UPGRADE-6.2.md b/UPGRADE-6.2.md index 0c7c7915dab83..60c5b84b43793 100644 --- a/UPGRADE-6.2.md +++ b/UPGRADE-6.2.md @@ -9,6 +9,11 @@ FrameworkBundle `Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead * Deprecate `AbstractController::renderForm()`, use `render()` instead +HttpFoundation +-------------- + + * Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead + Mailer -------- diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index fdbd39cead318..d78710a3e43f2 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Add stale while revalidate and stale if error cache header * Allow dynamic session "ttl" when using a remote storage + * Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead 6.0 --- diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index d9fc87ab33f74..6b73827eaea70 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1331,9 +1331,23 @@ public function setRequestFormat(?string $format) } /** - * Gets the format associated with the request. + * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). + * + * @deprecated since Symfony 6.2, use getContentTypeFormat instead */ public function getContentType(): ?string + { + trigger_deprecation('symfony/http-foundation', '6.2', 'The method "%s" is deprecated, use "getContentTypeFormat" instead.', __METHOD__); + + return $this->getContentTypeFormat(); + } + + /** + * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). + * + * @see Request::$formats + */ + public function getContentTypeFormat(): ?string { return $this->getFormat($this->headers->get('CONTENT_TYPE', '')); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index fafe3c7b10edb..dc054c957be5f 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpFoundation\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException; use Symfony\Component\HttpFoundation\Exception\JsonException; use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; @@ -23,6 +24,8 @@ class RequestTest extends TestCase { + use ExpectDeprecationTrait; + protected function tearDown(): void { Request::setTrustedProxies([], -1); @@ -78,14 +81,33 @@ public function testIsNoCache() $this->assertFalse($isNoCache); } + /** + * @group legacy + */ public function testGetContentType() { + $this->expectDeprecation('Since symfony/http-foundation 6.2: The method "Symfony\Component\HttpFoundation\Request::getContentType" is deprecated, use "getContentTypeFormat" instead.'); $request = new Request(); + $contentType = $request->getContentType(); $this->assertNull($contentType); } + public function testGetContentTypeFormat() + { + $request = new Request(); + $this->assertNull($request->getContentTypeFormat()); + + $server = ['HTTP_CONTENT_TYPE' => 'application/json']; + $request = new Request([], [], [], [], [], $server); + $this->assertEquals('json', $request->getContentTypeFormat()); + + $server = ['HTTP_CONTENT_TYPE' => 'text/html']; + $request = new Request([], [], [], [], [], $server); + $this->assertEquals('html', $request->getContentTypeFormat()); + } + public function testSetDefaultLocale() { $request = new Request(); diff --git a/src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php index 7e57db1a9c941..c515417ad6ba1 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php @@ -74,7 +74,7 @@ public function supports(Request $request): bool { return ($this->options['post_only'] ? $request->isMethod('POST') : true) && $this->httpUtils->checkRequestPath($request, $this->options['check_path']) - && ($this->options['form_only'] ? 'form' === $request->getContentType() : true); + && ($this->options['form_only'] ? 'form' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) : true); } public function authenticate(Request $request): Passport diff --git a/src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php b/src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php index ff446e77d8343..bd655d9cd28ce 100644 --- a/src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php +++ b/src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php @@ -63,7 +63,10 @@ public function __construct(HttpUtils $httpUtils, UserProviderInterface $userPro public function supports(Request $request): ?bool { - if (!str_contains($request->getRequestFormat() ?? '', 'json') && !str_contains($request->getContentType() ?? '', 'json')) { + if ( + !str_contains($request->getRequestFormat() ?? '', 'json') + && !str_contains((method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) ?? '', 'json') + ) { return false; }