Project

General

Profile

Actions

Bug #89510

closed

JsonView does not handle _only/_exclude correctly

Added by Markus Klein about 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Must have
Assignee:
Category:
Extbase
Target version:
-
Start date:
2019-10-25
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
8
PHP Version:
Tags:
Complexity:
easy
Is Regression:
Sprint Focus:

Description

Setup

if ($this->view instanceof \TYPO3\CMS\Extbase\Mvc\View\JsonView) {
    $this->view->setConfiguration(
        [
            'value' => [
                'items' => [
                    '_exclude' => ['foo'],
                ],
            ],
        ]
    );
    $value = [
        'items' => [
            ['name' => 'a'],
            ['name' => 'b'],
        ]
    ];
}
$this->view->assign('value', $value);

Expected JSON output

{"recentRequests": [{"name": "a"},{"name": "b"}]}

Actual JSON output

{"recentRequests": {"1": {"name": "b"}]}

Reasoning

\TYPO3\CMS\Extbase\Mvc\View\JsonView::transformValue checks for _exclude keys and does so by using in_array.
Unfortunately it does not set the third parameter to true, which would enforce the data type.
This way, when processing the array of "recentRequests", each array key is checked against the _exclude array.
For the first element in the array (key = 0) this yields true, hence the first array element is skipped (excluded).
(in_array(0, ['foo']) === true !!)

Ultimately, this leads to an PHP array with missing first index 0, which causes the data to be converted to a JSON object (instead of array).
(The program on the client side expects an array though and crashes.)

Fix

Adjust the in_array call.

Simply enforce the data type

Actions

Also available in: Atom PDF