8000 bug #18630 [FrameworkBundle][Serializer] Fix a deprecation triggered … · symfony/symfony@f2228d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2228d5

Browse files
bug #18630 [FrameworkBundle][Serializer] Fix a deprecation triggered by the ClassMetadataFactory (Ener-Getick)
This PR was merged into the 3.1-dev branch. Discussion ---------- [FrameworkBundle][Serializer] Fix a deprecation triggered by the ClassMetadataFactory | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | | License | MIT Without apparent reasons, [FOSRestBundle's tests fail](https://travis-ci.org/FriendsOfSymfony/FOSRestBundle/jobs/124384888) since #18561. ``` Passing a Doctrine Cache instance as 2nd parameter of the "Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory" constructor is deprecated. This parameter will be removed in Symfony 4.0. Use the "Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory" class instead: 6x 1x in ErrorWithTemplatingFormatTest::testSerializeExceptionHtml from FOS\RestBundle\Tests\Functional 1x in SerializerErrorTest::testSerializeExceptionJson from FOS\RestBundle\Tests\Functional 1x in SerializerErrorTest::testSerializeExceptionJsonWithoutDebug from FOS\RestBundle\Tests\Functional 1x in SerializerErrorTest::testSerializeExceptionXml from FOS\RestBundle\Tests\Functional 1x in SerializerErrorTest::testSerializeInvalidFormJson from FOS\RestBundle\Tests\Functional 1x in SerializerErrorTest::testSerializeInvalidFormXml from FOS\RestBundle\Tests\Functional ``` We don't use cache in our tests but some of them are not in ``debug`` mode (will change soon) so the cache is automatically used. This PR fixes this deprecation by detecting if the cache used by the serializer is psr6 compliant or not (if it is, then it replaces the default metadata factory by an instance of the new class ``CacheClassMetadataFactory``, otherwise the second parameter of the ``ClassMetadataFactory`` is used). Commits ------- 15579d5 [FrameworkBundle] Deprecate framework.serializer.cache eccbffb [Serializer] Improve a deprecation message 96e418a Revert "[FrameworkBundle] Fallback to default cache system in production for serializer"
2 parents e0696fa + 15579d5 commit f2228d5

File tree

14 files changed

+136
-24
lines changed

14 files changed

+136
-24
lines changed

UPGRADE-3.1.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ Form
1919
* Support for data objects that implements both `Traversable` and `ArrayAccess`
2020
in `ResizeFormListener::preSubmit` method has been deprecated and will be
2121
removed in Symfony 4.0.
22-
22+
2323
* Using callable strings as choice options in ChoiceType has been deprecated
2424
in favor of `PropertyPath` in Symfony 4.0 use a "\Closure" instead.
25-
25+
2626
Before:
27-
27+
2828
```php
2929
'choice_value' => new PropertyPath('range'),
3030
'choice_label' => 'strtoupper',
3131
```
32-
32+
3333
After:
34-
34+
3535
```php
3636
'choice_value' => 'range',
3737
'choice_label' => function ($choice) {
@@ -88,6 +88,27 @@ FrameworkBundle
8888
cache service. If you are using `serializer.mapping.cache.apc`, use
8989
`serializer.mapping.cache.doctrine.apc` instead.
9090

91+
* The `framework.serializer.cache` option has been deprecated. Configure a cache pool
92+
called `serializer` under `framework.cache.pools` instead.
93+
94+
Before:
95+
96+
```yaml
97+
framework:
98+
serializer:
99+
cache: serializer.mapping.cache.apc
100+
```
101+
102+
After:
103+
104+
```yaml
105+
framework:
106+
serializer: ~
107+
cache:
108+
pools:
109+
serializer:
110+
adapter: cache.adapter.apcu
111+
91112
HttpKernel
92113
----------
93114

UPGRADE-4.0.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ Form
1616

1717
* Support for data objects that implements both `Traversable` and
1818
`ArrayAccess` in `ResizeFormListener::preSubmit` method has been removed.
19-
20-
* Using callable strings as choice options in ChoiceType is not supported
19+
20+
* Using callable strings as choice options in ChoiceType is not supported
2121
anymore in favor of passing PropertyPath instances.
22-
22+
2323
Before:
24-
24+
2525
```php
2626
'choice_value' => new PropertyPath('range'),
2727
'choice_label' => 'strtoupper',
2828
```
29-
29+
3030
After:
31-
31+
3232
```php
3333
'choice_value' => 'range',
3434
'choice_label' => function ($choice) {
3535
return strtoupper($choice);
3636
},
3737
```
38-
38+
3939

4040
FrameworkBundle
4141
---------------
@@ -78,6 +78,28 @@ FrameworkBundle
7878
* The service `serializer.mapping.cache.apc` has been removed; use
7979
`serializer.mapping.cache.doctrine.apc` instead.
8080

81+
* The `framework.serializer.cache` option has been removed. Configure a cache pool
82+
called `serializer` under `framework.cache.pools` instead.
83+
84+
Before:
85+
86+
```yaml
87+
framework:
88+
serializer:
89+
cache: serializer.mapping.cache.apc
90+
```
91+
92+
After:
93+
94+
```yaml
95+
framework:
96+
serializer: ~
97+
cache:
98+
pools:
99+
serializer:
100+
adapter: cache.adapter.apcu
101+
```
102+
81103
HttpKernel
82104
----------
83105

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode)
513513
->canBeEnabled()
514514
->children()
515515
->booleanNode('enable_annotations')->defaultFalse()->end()
516-
->scalarNode('cache')->defaultValue('serializer.mapping.cache.symfony')->end()
516+
->scalarNode('cache')->end()
517517
->scalarNode('name_converter')->end()
518518
->end()
519519
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Component\Finder\Finder;
2525
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2626
use Symfony\Component\Config\FileLocator;
27+
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
2728
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
2829
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
2930
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
@@ -982,7 +983,9 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
982983

983984
$chainLoader->replaceArgument(0, $serializerLoaders);
984985

985-
if (!$container->getParameter('kernel.debug')) {
986+
if (isset($config['cache']) && $config['cache']) {
987+
@trigger_error('The "framework.serializer.cache" option is deprecated since Symfony 3.1 and will be removed in 4.0. You can configure a cache pool called "serializer" under "framework.cache.pools" instead.', E_USER_DEPRECATED);
988+
986989
$container->setParameter(
987990
'serializer.mapping.cache.prefix',
988991
'serializer_'.$this->getKernelRootHash($container)
@@ -991,6 +994,18 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
991994
$container->getDefinition('serializer.mapping.class_metadata_factory')->replaceArgument(
992995
1, new Reference($config['cache'])
993996
);
997+
} elseif (!$container->getParameter('kernel.debug')) {
998+
$cacheMetadataFactory = new Definition(
999+
CacheClassMetadataFactory::class,
1000+
array(
1001+
new Reference('serializer.mapping.class_metadata_factory.inner'),
1002+
new Reference('cache.pool.serializer'),
1003+
)
1004+
);
1005+
$cacheMetadataFactory->setPublic(false);
1006+
$cacheMetadataFactory->setDecoratedService('serializer.mapping.class_metadata_factory');
1007+
1008+
$container->setDefinition('serializer.mapping.cache_class_metadata_factory', $cacheMetadataFactory);
9941009
}
9951010

9961011
if (isset($config['name_converter']) && $config['name_converter']) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
</service>
3939

4040
<!-- Cache -->
41-
<service id="serializer.mapping.cache.symfony" class="Symfony\Component\Cache\DoctrineProvider" public="false">
42-
<argument type="service" id="cache.pool.serializer" />
43-
</service>
44-
4541
<service id="serializer.mapping.cache.doctrine.apc" class="Doctrine\Common\Cache\ApcCache" public="false">
4642
<call method="setNamespace">
4743
<argument>%serializer.mapping.cache.prefix%</argument>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ protected static function getBundleDefaultConfig()
221221
'serializer' => array(
222222
'enabled' => false,
223223
'enable_annotations' => false,
224-
'cache' => 'serializer.mapping.cache.symfony',
225224
),
226225
'property_access' => array(
227226
'magic_call' => false,

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
'serializer' => array(
6767
'enabled' => true,
6868
'enable_annotations' => true,
69-
'cache' => 'serializer.mapping.cache.doctrine.apc',
7069
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
7170
),
7271
'ide' => 'file%%link%%format',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'serializer' => array(
5+
'enabled' => true,
6+
'cache' => 'foo',
7+
),
8+
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
</framework:translator>
4141
<framework:validation enabled="true" cache="validator.mapping.cache.doctrine.apc" />
4242
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
43-
<framework:serializer enabled="true" enable-annotations="true" cache="serializer.mapping.cache.doctrine.apc" name-converter="serializer.name_converter.camel_case_to_snake_case" />
43+
<framework:serializer enabled="true" enable-annotations="true" name-converter="serializer.name_converter.camel_case_to_snake_case" />
4444
</framework:config>
4545
</container>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config>
9+
<framework:serializer enabled="true" cache="foo"/>
10+
</framework:config>
11+
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ framework:
5252
serializer:
5353
enabled: true
5454
enable_annotations: true
55-
cache: serializer.mapping.cache.doctrine.apc
5655
name_converter: serializer.name_converter.camel_case_to_snake_case
5756
ide: file%%link%%format
5857
request:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
serializer:
3+
enabled: true
4+
cache: foo

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function testSerializerEnabled()
449449

450450
$this->assertCount(1, $argument);
451451
$this->assertEquals('Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader', $argument[0]->getClass());
452-
$this->assertEquals(new Reference('serializer.mapping.cache.doctrine.apc'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
452+
$this->assertNull($container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
453453
$this->assertEquals(new Reference('serializer.name_converter.camel_case_to_snake_case'), $container->getDefinition('serializer.normalizer.object')->getArgument(1));
454454
}
455455

@@ -521,6 +521,44 @@ public function testObjectNormalizerRegistered()
521521
$this->assertEquals(-1000, $tag[0]['priority']);
522522
}
523523

524+
public function testSerializerCacheActivated()
525+
{
526+
$container = $this->createContainerFromFile('serializer_enabled');
527+
$this->assertTrue($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
528+
}
529+
530+
public function testSerializerCacheDisabled()
531+
{
532+
$container = $this->createContainerFromFile('serializer_enabled', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
533+
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
534+
}
535+
536+
/**
537+
* @group legacy
538+
*/
539+
public function testDeprecatedSerializerCacheOption()
540+
{
541+
$deprecations = array();
542+
set_error_handler(function ($type, $msg) use (&$deprecations) {
543+
if (E_USER_DEPRECATED !== $type) {
544+
restore_error_handler();
545+
546+
return call_user_func_array('PHPUnit_Util_ErrorHandler::handleError', func_get_args());
547+
}
548+
549+
$deprecations[] = $msg;
550+
});
551+
552+
$container = $this->createContainerFromFile('serializer_legacy_cache', array('kernel.debug' => true, 'kernel.container_class' => __CLASS__));
553+
554+
restore_error_handler();
555+
556+
$this->assertCount(1, $deprecations);
557+
$this->assertContains('The "framework.serializer.cache" option is deprecated', $deprecations[0]);
558+
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
559+
$this->assertEquals(new Reference('foo'), $container->getDefinition('serializer.mapping.class_metadata_factory')->getArgument(1));
560+
}
561+
524562
public function testAssetHelperWhenAssetsAreEnabled()
525563
{
526564
$container = $this->createContainerFromFile('full');

src/Symfony/Component/Serializer/Mapping/Factory/ClassMetadataFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(LoaderInterface $loader, Cache $cache = null)
5050
$this->cache = $cache;
5151

5252
if (null !== $cache) {
53-
@trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
53+
@trigger_error(sprintf('Passing a Doctrine Cache instance as 2nd parameter of the "%s" constructor is deprecated since version 3.1. This parameter will be removed in Symfony 4.0. Use the "%s" class instead.', __CLASS__, CacheClassMetadataFactory::class), E_USER_DEPRECATED);
5454
}
5555
}
5656

0 commit comments

Comments
 (0)
0