Skip to content

Commit

Permalink
Merge pull request #20007 from xicond/bugfix/20002-Superfluous_query_…
Browse files Browse the repository at this point in the history
…on_HEAD_request_in_serializer

fix: #20002 optimize head request on serializeDataProvider
  • Loading branch information
bizley authored Oct 20, 2023
2 parents 75e5a08 + c1315b9 commit 04f5944
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Yii Framework 2 Change Log

- Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher)
- Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir)
- Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond)
- Enh #12743: Added new methods `BaseActiveRecord::loadRelations()` and `BaseActiveRecord::loadRelationsFor()` to eager load related models for existing primary model instances (PowerGamer1)


2.0.49.2 October 12, 2023
-------------------------

Expand Down
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 (($pagination = $dataProvider->getPagination()) !== false) {
$this->addPaginationHeaders($pagination);
}

if ($this->request->getIsHead()) {
return null;
} elseif ($this->collectionEnvelope === null) {
if ($this->collectionEnvelope === null) {
return $models;
}

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

/**
Expand Down Expand Up @@ -415,6 +416,32 @@ 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 04f5944

Please sign in to comment.