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 10 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)
samdark marked this conversation as resolved.
Show resolved Hide resolved
- 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);
}
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#L191-L196

Added lines #L191 - L196 were 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) {

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

View check run for this annotation

Codecov / codecov/patch

framework/rest/Serializer.php#L204

Added line #L204 was not covered by tests
return $models;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/framework/rest/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

use yii\base\Model;
use yii\data\ArrayDataProvider;
use yii\data\DataProviderInterface;
use yii\rest\Serializer;
use yii\web\Request;
use yiiunit\TestCase;

/**
Expand Down Expand Up @@ -413,6 +415,22 @@ public function testSerializeDataProvider($dataProvider, $expectedResult, $saveK
$serializer->preserveKeys = $saveKeys;

$this->assertEquals($expectedResult, $serializer->serialize($dataProvider));

$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']);
}

/**
Expand Down
Loading