10000 [Serializer] Set context annotation as not final by benjaminmal · Pull Request #45155 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Set context annotation as not final #45155

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
Jan 25, 2022
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
[Serializer] Set context annotation as not final
  • Loading branch information
benjaminmal authored and fabpot committed Jan 25, 2022
commit f04ba9dd408a78d22bfe84db34ed0245e2ddad1c
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class Context
class Context
{
private array $groups;

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.1
---

* Set `Context` annotation as not final
* Deprecate `ContextAwareNormalizerInterface`, use `NormalizerInterface` instead
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
*/
trait ContextMetadataTestTrait
{
public function testContextMetadataNormalize()
/**
* @dataProvider contextMetadataDummyProvider
*/
public function testContextMetadataNormalize(string $contextMetadataDummyClass)
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor());
new Serializer([new DateTimeNormalizer(), $normalizer]);

$dummy = new ContextMetadataDummy();
$dummy = new $contextMetadataDummyClass();
$dummy->date = new \DateTime('2011-07-28T08:44:00.123+00:00');

self::assertEquals(['date' => '2011-07-28T08:44:00+00:00'], $normalizer->normalize($dummy));
Expand All @@ -48,28 +51,39 @@ public function testContextMetadataNormalize()
]), 'base denormalization context is unchanged for this group');
}

public function testContextMetadataContextDenormalize()
/**
* @dataProvider contextMetadataDummyProvider
*/
public function testContextMetadataContextDenormalize(string $contextMetadataDummyClass)
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory, null, null, new PhpDocExtractor());
10000 new Serializer([new DateTimeNormalizer(), $normalizer]);

/** @var ContextMetadataDummy $dummy */
$dummy = $normalizer->denormalize(['date' => '2011-07-28T08:44:00+00:00'], ContextMetadataDummy::class);
/** @var ContextMetadataDummy|ContextChildMetadataDummy $dummy */
$dummy = $normalizer->denormalize(['date' => '2011-07-28T08:44:00+00:00'], $contextMetadataDummyClass);
self::assertEquals(new \DateTime('2011-07-28T08:44:00+00:00'), $dummy->date);

/** @var ContextMetadataDummy $dummy */
/** @var ContextMetadataDummy|ContextChildMetadataDummy $dummy */
$dummy = $normalizer->denormalize(['date' => '2011-07-28T08:44:00+00:00'], ContextMetadataDummy::class, null, [
ObjectNormalizer::GROUPS => 'extended',
]);
self::assertEquals(new \DateTime('2011-07-28T08:44:00+00:00'), $dummy->date, 'base denormalization context is unchanged for this group');

/** @var ContextMetadataDummy $dummy */
$dummy = $normalizer->denormalize(['date' => '28/07/2011'], ContextMetadataDummy::class, null, [
/** @var ContextMetadataDummy|ContextChildMetadataDummy $dummy */
$dummy = $normalizer->denormalize(['date' => '28/07/2011'], $contextMetadataDummyClass, null, [
ObjectNormalizer::GROUPS => 'simple',
]);
self::assertEquals('2011-07-28', $dummy->date->format('Y-m-d'), 'a specific denormalization context is used for this group');
}

public function contextMetadataDummyProvider()
{
return [
[ContextMetadataDummy::class],
[ContextChildMetadataDummy::class],
];
}
}

class ContextMetadataDummy
Expand All @@ -90,3 +104,22 @@ class ContextMetadataDummy
*/
public $date;
}

class ContextChildMetadataDummy
{
/**
* @var \DateTime
*
* @Groups({ "extended", "simple" })
* @DummyContextChild({ DateTimeNormalizer::FORMAT_KEY = \DateTime::RFC3339 })
* @DummyContextChild(
* normalizationContext = { DateTimeNormalizer::FORMAT_KEY = \DateTime::RFC3339_EXTENDED },
* groups = {"extended"}
* )
* @DummyContextChild(
* denormalizationContext = { DateTimeNormalizer::FORMAT_KEY = "d/m/Y" },
* groups = {"simple"}
* )
*/
public $date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\Serializer\Tests\Normalizer\Features;

use Symfony\Component\Serializer\Annotation\Context;

/**
* Annotation class for @DummyContextChild().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY", "METHOD"})
*/
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
class DummyContextChild extends Context
{
}
0