Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Add normalizer / denormalizer awarness #17545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2016
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 @@ -17,14 +17,18 @@
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;

/**
* Normalizer implementation.
*
* @author Kévin Dunglas <[email protected]>
*/
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break for anyone having a type hint on the SerializerAwareNormalizer class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we readd the extends and override the methods ? Or is this an acceptable BC break ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So according to this : http://symfony.com/doc/current/contributing/code/bc.html#id10 it is not something acceptable, will do a PR to readd this class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry I was on a mobile when writing the comment and forgot to add a reference to that later.

{
use SerializerAwareTrait;

const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
const OBJECT_TO_POPULATE = 'object_to_populate';
const GROUPS = 'groups';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;

/**
* @author Jordi Boggiano <[email protected]>
*/
class CustomNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
{
use SerializerAwareTrait;

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* Class accepting a denormalizer.
*
* @author Joel Wurtz <[email protected]>
*/
interface DenormalizerAwareInterface
{
/**
* Sets the owning Denormalizer object.
*
* @param DenormalizerInterface $denormalizer
*/
public function setDenormalizer(DenormalizerInterface $denormalizer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* DenormalizerAware trait.
*
* @author Joel Wurtz <[email protected]>
*/
trait DenormalizerAwareTrait
{
/**
* @var DenormalizerInterface
*/
protected $denormalizer;

/**
* Sets the Denormalizer.
*
* @param DenormalizerInterface $denormalizer A DenormalizerInterface instance
*/
public function setSerializer(DenormalizerInterface $denormalizer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setDenormalizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oups :) Fix in #18536

{
$this->denormalizer = $denormalizer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* Class accepting a normalizer.
*
* @author Joel Wurtz <[email protected]>
*/
interface NormalizerAwareInterface
{
/**
* Sets the owning Normalizer object.
*
* @param NormalizerInterface $normalizer
*/
public function setNormalizer(NormalizerInterface $normalizer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

/**
* NormalizerAware trait.
*
* @author Joel Wurtz <[email protected]>
*/
trait NormalizerAwareTrait
{
/**
* @var NormalizerInterface
*/
protected $normalizer;

/**
* Sets the normalizer.
*
* @param NormalizerInterface $normalizer A NormalizerInterface instance
*/
public function setSerializer(NormalizerInterface $normalizer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setNormalizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #18536

{
$this->normalizer = $normalizer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,17 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;
use Symfony\Component\Serializer\SerializerAwareInterface;

/**
* SerializerAware Normalizer implementation.
*
* @author Jordi Boggiano <[email protected]>
*
* @deprecated since version 3.1, to be removed in 4.0. Use the SerializerAwareTrait instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must trigger a deprecation error like this one: @trigger_error(sprintf('The class "%s" is deprecated. It will be removed in Symfony 4.0. Use the "Symfony\Component\Serializer\SerializerAwareTrait" trait instead.', __CLASS__), E_USER_DEPRECATED);

You should also adapt existing normalizers in Symfony to avoid using this deprecated class. But if you do that... It will be a small BC break (ObjectNormalizer will not inherit from the SerializerAwareNormalizer type anymore - but as there is a corresponding interface, the BC break should be very limited). What do you think @symfony/deciders?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You must trigger a deprecation error

Since there is no more code / method in this class i don't think it's possible, i have done this deprecated feature in the same way it was done for the ContainerAwareTrait in 2.8 https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/DependencyInjection/ContainerAware.php

You should also adapt existing normalizers in Symfony to avoid using this deprecated class

It's already done, but maybe i have miss some implementations ?

*/
abstract class SerializerAwareNormalizer implements SerializerAwareInterface
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* {@inheritdoc}
*/
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
use SerializerAwareTrait;
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Serializer\Encoder\ChainEncoder;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Exception\LogicException;
Expand Down Expand Up @@ -68,6 +70,14 @@ public function __construct(array $normalizers = array(), array $encoders = arra
if ($normalizer instanceof SerializerAwareInterface) {
$normalizer->setSerializer($this);
}

if ($normalizer instanceof DenormalizerAwareInterface) {
$normalizer->setDenormalizer($this);
}

if ($normalizer instanceof NormalizerAwareInterface) {
$normalizer->setNormalizer($this);
}
}
$this->normalizers = $normalizers;

Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Serializer/SerializerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer;

/**
* SerializerAware trait.
*
* @author Joel Wurtz <[email protected]>
*/
trait SerializerAwareTrait
{
/**
* @var SerializerInterface
*/
protected $serializer;

/**
* Sets the serializer.
*
* @param SerializerInterface $serializer A SerializerInterface instance
*/
public function setSerializer(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
namespace Symfony\Component\Serializer\Tests;

use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
Expand Down Expand Up @@ -312,6 +316,26 @@ public function testDeserializeArray()
$serializer->deserialize($jsonData, __NAMESPACE__.'\Model[]', 'json')
);
}

public function testNormalizerAware()
{
$normalizerAware = $this->getMock(NormalizerAwareInterface::class);
$normalizerAware->expects($this->once())
->method('setNormalizer')
->with($this->isInstanceOf(NormalizerInterface::class));

new Serializer([$normalizerAware]);
}

public function testDenormalizerAware()
{
$denormalizerAware = $this->getMock(DenormalizerAwareInterface::class);
$denormalizerAware->expects($this->once())
->method('setDenormalizer')
->with($this->isInstanceOf(DenormalizerInterface::class));

new Serializer([$denormalizerAware]);
}
}

class Model
Expand Down