diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e567c4deae..5564628848 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -74,6 +74,7 @@ Yii Framework 2 Change Log - Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark) - Chg #9953: `TimestampBehavior::getValue()` changed to make value processing consistent with `AttributeBehavior::getValue()` (silverfire) - Chg #6419: Added `yii\web\ErrorHandler::displayVars` make list of displayed vars customizable. `$_ENV` and `$_SERVER` are not displayed by default anymore (silverfire) +- Chg #9528: Traversable objects are now formatted as arrays in `yii\web\XmlResponseFormatter` to support SPL objects and Generators (MaXL-ru) - New #10083: Added wrapper for PHP webserver (samdark) - New: Added new requirement: ICU Data version >= 49.1 (SilverFire) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index 1564528def..de83f0d808 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -34,6 +34,8 @@ initialization to support wider range of allowed characters. Because of this cha the event. - If you overrode the `yii.confirm` function and accessed the `yii` object through `this`, you must access it with global variable `yii` instead. +* Traversable objects are now formatted as arrays in XML response to support SPL objects and Generators. Previous + behavior could be turned on by setting `XmlResponseFormatter::$useTraversableAsArray` to `false`. Upgrade from Yii 2.0.5 ---------------------- diff --git a/framework/web/XmlResponseFormatter.php b/framework/web/XmlResponseFormatter.php index ba9c5d304f..6c0b43fc64 100644 --- a/framework/web/XmlResponseFormatter.php +++ b/framework/web/XmlResponseFormatter.php @@ -46,6 +46,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa public $itemTag = 'item'; /** * @var boolean whether to enable to interpret implemented \Traversable interface objects as array + * @since 2.0.7 */ public $useTraversableAsArray = true; diff --git a/tests/framework/web/JsonResponseFormatterTest.php b/tests/framework/web/JsonResponseFormatterTest.php index ca49f48499..c13d76c510 100644 --- a/tests/framework/web/JsonResponseFormatterTest.php +++ b/tests/framework/web/JsonResponseFormatterTest.php @@ -73,4 +73,15 @@ class JsonResponseFormatterTest extends FormatterTest ], '{"0":{"id":123,"title":"<>"},"a":{"id":456,"title":"def"}}'], ]; } + + public function formatTraversableObjectDataProvider() + { + $postsStack = new \SplStack(); + $postsStack->push(new Post(915, 'record1')); + $postsStack->push(new Post(456, 'record2')); + + return [ + [$postsStack, '{"1":{"id":456,"title":"record2"},"0":{"id":915,"title":"record1"}}'] + ]; + } }