Skip to content

Commit

Permalink
Revert: "fix: yiisoft#20002 optimize head request on serializeDataPro…
Browse files Browse the repository at this point in the history
…vider yiisoft#20007".
  • Loading branch information
terabytesoftw committed Jul 10, 2024
1 parent 2d312cf commit 0d4567d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
14 changes: 7 additions & 7 deletions framework/rest/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,20 @@ 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 {
$models = array_values($dataProvider->getModels());
}
$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;
}

Expand Down
27 changes: 0 additions & 27 deletions tests/framework/rest/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use yii\base\Model;
use yii\data\ArrayDataProvider;
use yii\rest\Serializer;
use yii\web\Request;
use yiiunit\TestCase;

/**
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 0d4567d

Please sign in to comment.