[Serializer][Minor] Fix exception message
Wrong circular reference limit displayed.
This commit is contained in:
parent
b3a20e4065
commit
36ce9029f7
@ -327,7 +327,7 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
|
||||
return $circularReferenceHandler($object, $format, $context);
|
||||
}
|
||||
|
||||
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($object), $this->circularReferenceLimit));
|
||||
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($object), $context[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->defaultContext[self::CIRCULAR_REFERENCE_LIMIT] ?? $this->circularReferenceLimit));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,23 +10,36 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
*/
|
||||
trait CircularReferenceTestTrait
|
||||
{
|
||||
abstract protected function getNormalizerForCircularReference(): NormalizerInterface;
|
||||
abstract protected function getNormalizerForCircularReference(array $defaultContext): NormalizerInterface;
|
||||
|
||||
abstract protected function getSelfReferencingModel();
|
||||
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
public function provideUnableToNormalizeCircularReference(): array
|
||||
{
|
||||
$normalizer = $this->getNormalizerForCircularReference();
|
||||
return [
|
||||
[[], [], 1],
|
||||
[['circular_reference_limit' => 2], [], 2],
|
||||
[['circular_reference_limit' => 2], ['circular_reference_limit' => 3], 3],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideUnableToNormalizeCircularReference
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference(array $defaultContext, array $context, int $expectedLimit)
|
||||
{
|
||||
$normalizer = $this->getNormalizerForCircularReference($defaultContext);
|
||||
|
||||
$obj = $this->getSelfReferencingModel();
|
||||
|
||||
$this->expectException(CircularReferenceException::class);
|
||||
$normalizer->normalize($obj, null, ['circular_reference_limit' => 2]);
|
||||
$this->expectExceptionMessage(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d).', \get_class($obj), $expectedLimit));
|
||||
$normalizer->normalize($obj, null, $context);
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$normalizer = $this->getNormalizerForCircularReference();
|
||||
$normalizer = $this->getNormalizerForCircularReference([]);
|
||||
|
||||
$obj = $this->getSelfReferencingModel();
|
||||
$expected = ['me' => \get_class($obj)];
|
||||
|
@ -247,10 +247,10 @@ class GetSetMethodNormalizerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
protected function getNormalizerForCircularReference(): GetSetMethodNormalizer
|
||||
protected function getNormalizerForCircularReference(array $defaultContext): GetSetMethodNormalizer
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory));
|
||||
$normalizer = new GetSetMethodNormalizer($classMetadataFactory, new MetadataAwareNameConverter($classMetadataFactory), null, null, null, $defaultContext);
|
||||
new Serializer([$normalizer]);
|
||||
|
||||
return $normalizer;
|
||||
|
@ -365,9 +365,9 @@ class ObjectNormalizerTest extends TestCase
|
||||
|
||||
// circular reference
|
||||
|
||||
protected function getNormalizerForCircularReference(): ObjectNormalizer
|
||||
protected function getNormalizerForCircularReference(array $defaultContext): ObjectNormalizer
|
||||
{
|
||||
$normalizer = new ObjectNormalizer();
|
||||
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
|
||||
new Serializer([$normalizer]);
|
||||
|
||||
return $normalizer;
|
||||
|
@ -191,9 +191,9 @@ class PropertyNormalizerTest extends TestCase
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
}
|
||||
|
||||
protected function getNormalizerForCircularReference(): PropertyNormalizer
|
||||
protected function getNormalizerForCircularReference(array $defaultContext): PropertyNormalizer
|
||||
{
|
||||
$normalizer = new PropertyNormalizer();
|
||||
$normalizer = new PropertyNormalizer(null, null, null, null, null, $defaultContext);
|
||||
new Serializer([$normalizer]);
|
||||
|
||||
return $normalizer;
|
||||
|
Reference in New Issue
Block a user