Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #20002 optimize head request on serializeDataProvider #20007

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 serializeDataProvider($dataProvider)
{
if (($pagination = $dataProvider->getPagination()) !== false) {
$this->addPaginationHeaders($pagination);
}

Check warning on line 193 in framework/rest/Serializer.php

View check run for this annotation

Codecov / codecov/patch

framework/rest/Serializer.php#L193

Added line #L193 was not covered by tests
if ($this->request->getIsHead()) {
return null;
}

Check warning on line 196 in framework/rest/Serializer.php

View check run for this annotation

Codecov / codecov/patch

framework/rest/Serializer.php#L196

Added line #L196 was not covered by tests
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
Loading