Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #12098 [Serializer] Handle circular references (dunglas)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Serializer] Handle circular references | Q | A | ------------- | --- | Bug fix? | Yes: avoid infinite loops. Allows to improve #5347 | New feature? | yes (circular reference handler) | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#4299 This PR adds handling of circular references in the `Serializer` component. The number of allowed iterations is configurable (one by default). The behavior when a circular reference is detected is configurable. By default an exception is thrown. Instead of throwing an exception, it's possible to register a custom handler (e.g.: a Doctrine Handler returning the object ID). Usage: ```php use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; use Symfony\Component\Serializer\Serializer; class MyObj { private $id = 1312; public function getId() { return $this->getId(); } public function getMe() { return $this; } } $normalizer = new GetSetMethodNormalizer(); $normalizer->setCircularReferenceLimit(3); $normalizer->setCircularReferenceHandler(function ($obj) { return $obj->getId(); }); $serializer = new Serializer([$normalizer]); $serializer->normalize(new MyObj()); ``` Commits ------- 48491c4 [Serializer] Handle circular references
- Loading branch information
Showing
5 changed files
with
204 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Serializer/Exception/CircularReferenceException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Exception; | ||
|
||
/** | ||
* CircularReferenceException | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class CircularReferenceException extends RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Serializer/Tests/Fixtures/CircularReferenceDummy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class CircularReferenceDummy | ||
{ | ||
public function getMe() | ||
{ | ||
return $this; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/Serializer/Tests/Fixtures/SiblingHolder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class SiblingHolder | ||
{ | ||
private $sibling0; | ||
private $sibling1; | ||
private $sibling2; | ||
|
||
public function __construct() | ||
{ | ||
$sibling = new Sibling(); | ||
$this->sibling0 = $sibling; | ||
$this->sibling1 = $sibling; | ||
$this->sibling2 = $sibling; | ||
} | ||
|
||
public function getSibling0() | ||
{ | ||
return $this->sibling0; | ||
} | ||
|
||
public function getSibling1() | ||
{ | ||
return $this->sibling1; | ||
} | ||
|
||
public function getSibling2() | ||
{ | ||
return $this->sibling2; | ||
} | ||
} | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class Sibling | ||
{ | ||
public function getCoopTilleuls() | ||
{ | ||
return 'Les-Tilleuls.coop'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters