Commit 9b4e2e5e authored by Andreas Wolf's avatar Andreas Wolf Committed by Stefan Neufeind
Browse files

[BUGFIX] Empty array not encoded as empty XML tag

When encoding an empty array, an empty line with indentation was
created. If the <el> tag had a proper type="array" attribute, it could
still be decoded to an array again. If not, it was decoded to a string,
leading to errors e.g. when trying to traverse it.

The fix is to include an empty element now, which will be decoded to
an empty array again then.

Change-Id: Ia78adc1e7e81a943cdbc94f3cfa19b36442c430f
Resolves: #71394
Releases: 6.2, master
Reviewed-on: https://review.typo3.org/44599


Reviewed-by: default avatarJuan Manuel Vergés Solanas <juanmanuel.vergessolanas@gmail.com>
Tested-by: default avatarJuan Manuel Vergés Solanas <juanmanuel.vergessolanas@gmail.com>
Reviewed-by: default avatarHans Höchtl <jhoechtl@gmail.com>
Tested-by: default avatarHans Höchtl <jhoechtl@gmail.com>
Reviewed-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
parent dea3b005
......@@ -2136,11 +2136,15 @@ class GeneralUtility
$subOptions = $options;
$clearStackPath = false;
}
$content = $nl . self::array2xml($v, $NSprefix, ($level + 1), '', $spaceInd, $subOptions, array(
'parentTagName' => $tagName,
'grandParentTagName' => $stackData['parentTagName'],
'path' => ($clearStackPath ? '' : $stackData['path'] . '/' . $tagName)
)) . ($spaceInd >= 0 ? str_pad('', ($level + 1) * $indentN, $indentChar) : '');
if (count($v) === 0) {
$content = '';
} else {
$content = $nl . self::array2xml($v, $NSprefix, ($level + 1), '', $spaceInd, $subOptions, array(
'parentTagName' => $tagName,
'grandParentTagName' => $stackData['parentTagName'],
'path' => ($clearStackPath ? '' : $stackData['path'] . '/' . $tagName)
)) . ($spaceInd >= 0 ? str_pad('', ($level + 1) * $indentN, $indentChar) : '');
}
// Do not set "type = array". Makes prettier XML but means that empty arrays are not restored with xml2array
if ((int)$options['disableTypeAttrib'] != 2) {
$attr .= ' type="array"';
......
......@@ -4719,4 +4719,24 @@ text with a ' . $urlMatch . '$|s'),
GeneralUtility::rmdir($directory);
$this->assertTrue($check);
}
/**
* If the element is not empty, its contents might be treated as "something" (instead of "nothing") e.g. by Fluid
* view helpers, which is why we want to avoid that.
*
* @test
*/
public function xml2ArrayConvertsEmptyArraysToElementWithoutContent()
{
$input = [
'el' => []
];
$output = GeneralUtility::array2xml($input);
$this->assertEquals('<phparray>
<el type="array"></el>
</phparray>', $output);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment