Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
public function normalize($object, $format = null, array $context = [])
{
if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object);
return $this->handleCircularReference($object, $format, $context);
}

if (!$object instanceof \JsonSerializable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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 Marvin Feldmann <breyndot.echse@gmail.com>
*/
class JsonSerializableCircularReferenceDummy implements \JsonSerializable
{
public function jsonSerialize(): array
{
return [
'me' => $this,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableCircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableDummy;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;

/**
* @author Fred Cox <mcfedr@gmail.com>
*/
class JsonSerializableNormalizerTest extends TestCase
{
use CircularReferenceTestTrait;

/**
* @var JsonSerializableNormalizer
*/
Expand Down Expand Up @@ -96,6 +101,19 @@ private function doTestCircularNormalize(bool $legacy = false)
$this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy()));
}

protected function getNormalizerForCircularReference(array $defaultContext): JsonSerializableNormalizer
{
$normalizer = new JsonSerializableNormalizer(null, null, $defaultContext);
new Serializer([$normalizer]);

return $normalizer;
}

protected function getSelfReferencingModel()
{
return new JsonSerializableCircularReferenceDummy();
}

public function testInvalidDataThrowException()
{
$this->expectException(InvalidArgumentException::class);
Expand Down