From 0d4567ddc9891e6d0005182a73561506e3a11393 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 10 Jul 2024 09:20:01 -0400 Subject: [PATCH] Revert: "fix: #20002 optimize head request on serializeDataProvider #20007". --- framework/rest/Serializer.php | 14 ++++++------- tests/framework/rest/SerializerTest.php | 27 ------------------------- 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/framework/rest/Serializer.php b/framework/rest/Serializer.php index eedd96c2cc6..edc689e94e8 100644 --- a/framework/rest/Serializer.php +++ b/framework/rest/Serializer.php @@ -188,12 +188,6 @@ protected function getRequestedFields() */ protected function serializeDataProvider($dataProvider) { - if (($pagination = $dataProvider->getPagination()) !== false) { - $this->addPaginationHeaders($pagination); - } - if ($this->request->getIsHead()) { - return null; - } if ($this->preserveKeys) { $models = $dataProvider->getModels(); } else { @@ -201,7 +195,13 @@ protected function serializeDataProvider($dataProvider) } $models = $this->serializeModels($models); - if ($this->collectionEnvelope === null) { + if (($pagination = $dataProvider->getPagination()) !== false) { + $this->addPaginationHeaders($pagination); + } + + if ($this->request->getIsHead()) { + return null; + } elseif ($this->collectionEnvelope === null) { return $models; } diff --git a/tests/framework/rest/SerializerTest.php b/tests/framework/rest/SerializerTest.php index 91b627b8f8e..d7516e0dae0 100644 --- a/tests/framework/rest/SerializerTest.php +++ b/tests/framework/rest/SerializerTest.php @@ -10,7 +10,6 @@ use yii\base\Model; use yii\data\ArrayDataProvider; use yii\rest\Serializer; -use yii\web\Request; use yiiunit\TestCase; /** @@ -416,32 +415,6 @@ public function testSerializeDataProvider($dataProvider, $expectedResult, $saveK $this->assertEquals($expectedResult, $serializer->serialize($dataProvider)); } - /** - * @dataProvider dataProviderSerializeDataProvider - * - * @param \yii\data\DataProviderInterface $dataProvider - * @param array $expectedResult - * @param bool $saveKeys - */ - public function testHeadSerializeDataProvider($dataProvider, $expectedResult, $saveKeys = false) - { - $serializer = new Serializer(); - $serializer->preserveKeys = $saveKeys; - $serializer->collectionEnvelope = 'data'; - - $this->assertEquals($expectedResult, $serializer->serialize($dataProvider)['data']); - - $_SERVER['REQUEST_METHOD'] = 'HEAD'; - $request = new Request(); - $_POST[$request->methodParam] = 'HEAD'; - $serializer = new Serializer([ - 'request' => $request - ]); - $serializer->preserveKeys = $saveKeys; - $this->assertEmpty($serializer->serialize($dataProvider)); - unset($_POST[$request->methodParam], $_SERVER['REQUEST_METHOD']); - } - /** * @see https://github.com/yiisoft/yii2/issues/16334 */