Skip to content

Commit

Permalink
Merge branch '6.4' into 7.0
Browse files Browse the repository at this point in the history
* 6.4: (29 commits)
  fix tests
  add missing method
  fix merge
  fix test
  fix merge
  fix test
  change test to use a real ObjectManager
  [Mailer] Document the usage of custom headers in Infobip bridge
  [SecurityBundle] Add `provider` XML attribute to the authenticators it’s missing from
  [DoctrineBridge] Test reset with a true manager
  Sync php-cs-fixer config file with 7.2
  [HttpClient] Fix parsing SSE
  [Notifier] Fix thread key in GoogleChat bridge
  [HttpKernel][Security] Fix accessing session for stateless request
  [Serializer] Fix `ObjectNormalizer` with property path
  test handling of special "value" constraint option
  [PhpUnitBridge] Add missing import
  [FrameworkBundle] Fix setting default context for certain normalizers
  [57251] Missing translations for Romanian (ro)
  [ErrorHandler] Fix rendered exception code highlighting on PHP 8.3
  ...
  • Loading branch information
xabbuh committed Jun 22, 2024
2 parents 1ee25f5 + b3a0478 commit f44feb2
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 79 deletions.
8 changes: 5 additions & 3 deletions DependencyInjection/FrameworkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1917,18 +1917,20 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
}

$arguments = $container->getDefinition('serializer.normalizer.object')->getArguments();
$context = [];
$context = $arguments[6] ?? $defaultContext;

if (isset($config['circular_reference_handler']) && $config['circular_reference_handler']) {
$context += ($arguments[6] ?? $defaultContext) + ['circular_reference_handler' => new Reference($config['circular_reference_handler'])];
$context += ['circular_reference_handler' => new Reference($config['circular_reference_handler'])];
$container->getDefinition('serializer.normalizer.object')->setArgument(5, null);
}

if ($config['max_depth_handler'] ?? false) {
$context += ($arguments[6] ?? $defaultContext) + ['max_depth_handler' => new Reference($config['max_depth_handler'])];
$context += ['max_depth_handler' => new Reference($config['max_depth_handler'])];
}

$container->getDefinition('serializer.normalizer.object')->setArgument(6, $context);

$container->getDefinition('serializer.normalizer.property')->setArgument(5, $defaultContext);
}

private function registerPropertyInfoConfiguration(ContainerBuilder $container, PhpFileLoader $loader): void
Expand Down
1 change: 0 additions & 1 deletion Resources/config/serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
service('property_info')->ignoreOnInvalid(),
service('serializer.mapping.class_discriminator_resolver')->ignoreOnInvalid(),
null,
[],
])

->set('serializer.denormalizer.array', ArrayDenormalizer::class)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/AbstractWebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected static function getKernelClass(): string

protected static function createKernel(array $options = []): KernelInterface
{
$class = self::getKernelClass();
$class = static::getKernelClass();

if (!isset($options['test_case'])) {
throw new \InvalidArgumentException('The option "test_case" must be set.');
Expand Down
72 changes: 47 additions & 25 deletions Tests/Functional/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Tests\Fixtures\TranslatableBackedEnum;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\AppKernel;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -35,39 +38,58 @@ public function testDeserializeArrayOfObject()
$this->assertEquals($expected, $result);
}

/**
* @dataProvider provideNormalizersAndEncodersWithDefaultContextOption
*/
public function testNormalizersAndEncodersUseDefaultContextConfigOption(string $normalizerId)
public function testNormalizersAndEncodersUseDefaultContextConfigOption()
{
static::bootKernel(['test_case' => 'Serializer']);
/** @var SerializerKernel $kernel */
$kernel = static::bootKernel(['test_case' => 'Serializer', 'root_config' => 'default_context.yaml']);

foreach ($kernel->normalizersAndEncoders as $normalizerOrEncoderId) {
if (!static::getContainer()->has($normalizerOrEncoderId)) {
continue;
}

$normalizerOrEncoder = static::getContainer()->get($normalizerOrEncoderId);

$normalizer = static::getContainer()->get($normalizerId);
$reflectionObject = new \ReflectionObject($normalizerOrEncoder);
$property = $reflectionObject->getProperty('defaultContext');

$reflectionObject = new \ReflectionObject($normalizer);
$property = $reflectionObject->getProperty('defaultContext');
$defaultContext = $property->getValue($normalizerOrEncoder);

$defaultContext = $property->getValue($normalizer);
self::assertArrayHasKey('fake_context_option', $defaultContext);
self::assertEquals('foo', $defaultContext['fake_context_option']);
}
}

self::assertArrayHasKey('fake_context_option', $defaultContext);
self::assertEquals('foo', $defaultContext['fake_context_option']);
protected static function getKernelClass(): string
{
return SerializerKernel::class;
}
}

class SerializerKernel extends AppKernel implements CompilerPassInterface
{
public $normalizersAndEncoders = [
'serializer.normalizer.property.alias', // Special case as this normalizer isn't tagged
];

public static function provideNormalizersAndEncodersWithDefaultContextOption(): array
public function process(ContainerBuilder $container): void
{
return [
['serializer.normalizer.constraint_violation_list.alias'],
['serializer.normalizer.dateinterval.alias'],
['serializer.normalizer.datetime.alias'],
['serializer.normalizer.json_serializable.alias'],
['serializer.normalizer.problem.alias'],
['serializer.normalizer.uid.alias'],
['serializer.normalizer.translatable.alias'],
['serializer.normalizer.object.alias'],
['serializer.encoder.xml.alias'],
['serializer.encoder.yaml.alias'],
['serializer.encoder.csv.alias'],
];
$services = array_merge(
$container->findTaggedServiceIds('serializer.normalizer'),
$container->findTaggedServiceIds('serializer.encoder')
);
foreach ($services as $serviceId => $attributes) {
$class = $container->getDefinition($serviceId)->getClass();
if (null === $reflectionConstructor = (new \ReflectionClass($class))->getConstructor()) {
continue;
}
foreach ($reflectionConstructor->getParameters() as $reflectionParam) {
if ('array $defaultContext' === $reflectionParam->getType()->getName().' $'.$reflectionParam->getName()) {
$this->normalizersAndEncoders[] = $serviceId.'.alias';
break;
}
}
}
}

public function testSerializeTranslatableBackedEnum()
Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function __construct($varDir, $testCase, $rootConfig, $environment, $debu
parent::__construct($environment, $debug);
}

protected function getContainerClass(): string
{
return parent::getContainerClass().substr(md5($this->rootConfig), -16);
}

public function registerBundles(): iterable
{
if (!file_exists($filename = $this->getProjectDir().'/'.$this->testCase.'/bundles.php')) {
Expand Down
49 changes: 0 additions & 49 deletions Tests/Functional/app/Serializer/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,13 @@ framework:
max_depth_handler: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serializer\MaxDepthHandler
default_context:
enable_max_depth: true
fake_context_option: foo
property_info: { enabled: true }

services:
serializer.alias:
alias: serializer
public: true

serializer.normalizer.constraint_violation_list.alias:
alias: serializer.normalizer.constraint_violation_list
public: true

serializer.normalizer.dateinterval.alias:
alias: serializer.normalizer.dateinterval
public: true

serializer.normalizer.datetime.alias:
alias: serializer.normalizer.datetime
public: true

serializer.normalizer.json_serializable.alias:
alias: serializer.normalizer.json_serializable
public: true

serializer.normalizer.problem.alias:
alias: serializer.normalizer.problem
public: true

serializer.normalizer.uid.alias:
alias: serializer.normalizer.uid
public: true

serializer.normalizer.translatable.alias:
alias: serializer.normalizer.translatable
public: true

serializer.normalizer.property.alias:
alias: serializer.normalizer.property
public: true

serializer.normalizer.object.alias:
alias: serializer.normalizer.object
public: true

serializer.encoder.xml.alias:
alias: serializer.encoder.xml
public: true

serializer.encoder.yaml.alias:
alias: serializer.encoder.yaml
public: true

serializer.encoder.csv.alias:
alias: serializer.encoder.csv
public: true

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serializer\CircularReferenceHandler: ~

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Serializer\MaxDepthHandler: ~
59 changes: 59 additions & 0 deletions Tests/Functional/app/Serializer/default_context.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer:
enabled: true
circular_reference_handler: ~ # This must be null
max_depth_handler: ~ # This must be null
default_context:
fake_context_option: foo

services:
serializer.normalizer.constraint_violation_list.alias:
alias: serializer.normalizer.constraint_violation_list
public: true

serializer.normalizer.dateinterval.alias:
alias: serializer.normalizer.dateinterval
public: true

serializer.normalizer.datetime.alias:
alias: serializer.normalizer.datetime
public: true

serializer.normalizer.json_serializable.alias:
alias: serializer.normalizer.json_serializable
public: true

serializer.normalizer.object.alias:
alias: serializer.normalizer.object
public: true

serializer.normalizer.problem.alias:
alias: serializer.normalizer.problem
public: true

serializer.normalizer.property.alias:
alias: serializer.normalizer.property
public: true

serializer.normalizer.uid.alias:
alias: serializer.normalizer.uid
public: true

serializer.encoder.csv.alias:
alias: serializer.encoder.csv
public: true

serializer.encoder.json.alias:
alias: serializer.encoder.json
public: true

serializer.encoder.xml.alias:
alias: serializer.encoder.xml
public: true

serializer.encoder.yaml.alias:
alias: serializer.encoder.yaml
public: true

0 comments on commit f44feb2

Please sign in to comment.