Skip to content

Commit 04f5944

Browse files
authored
Merge pull request #20007 from xicond/bugfix/20002-Superfluous_query_on_HEAD_request_in_serializer
fix: #20002 optimize head request on serializeDataProvider
2 parents 75e5a08 + c1315b9 commit 04f5944

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Yii Framework 2 Change Log
66

77
- Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher)
88
- 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)
9+
- Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond)
910
- Enh #12743: Added new methods `BaseActiveRecord::loadRelations()` and `BaseActiveRecord::loadRelationsFor()` to eager load related models for existing primary model instances (PowerGamer1)
1011

11-
1212
2.0.49.2 October 12, 2023
1313
-------------------------
1414

framework/rest/Serializer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,20 @@ protected function getRequestedFields()
188188
*/
189189
protected function serializeDataProvider($dataProvider)
190190
{
191+
if (($pagination = $dataProvider->getPagination()) !== false) {
192+
$this->addPaginationHeaders($pagination);
193+
}
194+
if ($this->request->getIsHead()) {
195+
return null;
196+
}
191197
if ($this->preserveKeys) {
192198
$models = $dataProvider->getModels();
193199
} else {
194200
$models = array_values($dataProvider->getModels());
195201
}
196202
$models = $this->serializeModels($models);
197203

198-
if (($pagination = $dataProvider->getPagination()) !== false) {
199-
$this->addPaginationHeaders($pagination);
200-
}
201-
202-
if ($this->request->getIsHead()) {
203-
return null;
204-
} elseif ($this->collectionEnvelope === null) {
204+
if ($this->collectionEnvelope === null) {
205205
return $models;
206206
}
207207

tests/framework/rest/SerializerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use yii\base\Model;
1111
use yii\data\ArrayDataProvider;
1212
use yii\rest\Serializer;
13+
use yii\web\Request;
1314
use yiiunit\TestCase;
1415

1516
/**
@@ -415,6 +416,32 @@ public function testSerializeDataProvider($dataProvider, $expectedResult, $saveK
415416
$this->assertEquals($expectedResult, $serializer->serialize($dataProvider));
416417
}
417418

419+
/**
420+
* @dataProvider dataProviderSerializeDataProvider
421+
*
422+
* @param \yii\data\DataProviderInterface $dataProvider
423+
* @param array $expectedResult
424+
* @param bool $saveKeys
425+
*/
426+
public function testHeadSerializeDataProvider($dataProvider, $expectedResult, $saveKeys = false)
427+
{
428+
$serializer = new Serializer();
429+
$serializer->preserveKeys = $saveKeys;
430+
$serializer->collectionEnvelope = 'data';
431+
432+
$this->assertEquals($expectedResult, $serializer->serialize($dataProvider)['data']);
433+
434+
$_SERVER['REQUEST_METHOD'] = 'HEAD';
435+
$request = new Request();
436+
$_POST[$request->methodParam] = 'HEAD';
437+
$serializer = new Serializer([
438+
'request' => $request
439+
]);
440+
$serializer->preserveKeys = $saveKeys;
441+
$this->assertEmpty($serializer->serialize($dataProvider));
442+
unset($_POST[$request->methodParam], $_SERVER['REQUEST_METHOD']);
443+
}
444+
418445
/**
419446
* @see https://github.com/yiisoft/yii2/issues/16334
420447
*/

0 commit comments

Comments
 (0)