From 7b62e00ff449262a58799b486d68454abb7caee7 Mon Sep 17 00:00:00 2001 From: Craig Rayner Date: Fri, 26 Apr 2019 14:49:57 +1000 Subject: [PATCH 1/2] Bug #31232 {Serializer] Ensure context timezone is correctly used in denormalizer of DateTimeNormalizer If timezone applied in context, then overwrite datetime timezone. Tests written Coding Standard Change Place new tests in testDenormalizeUsingTimezonePassedInDefaultContext Code Standard Changes Code Standard Changes Mk II Code Standard Changes Mk III Code Standard Changes Mk IV Added FORCE_TIMEZONE flag for BC changed flag to FORCE_CONTEXT_TIMEZONE Coding Standing Trip Coding Standing Trip Coding Standing Trip if null === TIMEZONE_KEY, then ignore deprecation warning as PHP default will apply to denormalization. Added test for null === TIMEZONE to show different TZ if null === TIMEZONE_KEY, then ignore deprecation warning as PHP default will apply to denormalization. Added test for null === TIMEZONE to show different TZ Format now used from $format in denormalizer. Created isForceContextTimezone method. throw deprecation in denormalizer if Timezone given and force is false. Add test for correct format use, and single context timezone. Coding Standard Changes Debug testDenormalize[Null,Empty]ThrowsException Handle Legacy Timezone correctly in Constructor Change Constructor test to remove deprecation. Change FORCE_CONTEXT_TIMEZONE to ALWAYS_USE_TIMEZONE_KEY Removed deprecation as the new behaviour is an option to be selected. Coding Standard Change Remove comment as per directive. Created PRESERVE_CONTEXT_TIMEZONE flag as a must set option in the context, that defaults to false and throws a deprecation warning if not set. Built in Legacy retirement of the PRESERVE_CONTEXT_TIMEZONE flag with version >= Symfony 5.0 Coding Standard Change Travis Testing on Symfony. Repair testDenormalizeRecusive in ObjectNormalizerTest Added Deprecation to legacy timezone import if defaultContext is partially used. Moved comment to PRESERVE_CONTEXT_TIMEZONE constant. Only throw flag deprecation if the context timezone is not null. Code Review Changes Debug call to getTimeZone() --- .../Normalizer/DateTimeNormalizer.php | 50 +++++++++++++++++-- .../Normalizer/DateTimeNormalizerTest.php | 40 +++++++++++++-- .../Tests/Normalizer/ObjectNormalizerTest.php | 2 +- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index 51ad59a7e782e..5db3aa24f60f3 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -25,6 +25,15 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, const FORMAT_KEY = 'datetime_format'; const TIMEZONE_KEY = 'datetime_timezone'; + /** + * In PHP, The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). + * + * The denormalizer assumes that all DateTimeInterface object returned will have the timezone returned by the getTimezone() method. + * Default PHP behavior will occur if the getTimezone() method returns null or context[self::PRESERVE_CONTEXT_TIMEZONE] is set to false. + * This flag will be ignored in Symfony/Serializer 5.0+ + */ + const PRESERVE_CONTEXT_TIMEZONE = 'preserve_context_timezone'; + private $defaultContext; private static $supportedTypes = [ @@ -50,6 +59,12 @@ public function __construct($defaultContext = [], \DateTimeZone $timezone = null $defaultContext[self::TIMEZONE_KEY] = $timezone; } + if (!isset($defaultContext[self::TIMEZONE_KEY]) && null !== $timezone) { + @trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); + + $defaultContext[self::TIMEZONE_KEY] = $timezone; + } + $this->defaultContext = array_merge($this->defaultContext, $defaultContext); } @@ -92,16 +107,21 @@ public function denormalize($data, $class, $format = null, array $context = []) { $dateTimeFormat = $context[self::FORMAT_KEY] ?? null; $timezone = $this->getTimezone($context); + $preserveContextTimezone = $this->isPreserveContextTimezone($context); if ('' === $data || null === $data) { throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.'); } if (null !== $dateTimeFormat) { - $object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); + $object = \DateTime::createFromFormat($dateTimeFormat, $data, $timezone); if (false !== $object) { - return $object; + if ($preserveContextTimezone) { + $object->setTimezone($timezone); + } + + return \DateTime::class === $class ? $object : new \DateTimeImmutable($object->format(\DATE_RFC3339)); } $dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors(); @@ -116,7 +136,13 @@ public function denormalize($data, $class, $format = null, array $context = []) } try { - return \DateTime::class === $class ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone); + $object = new \DateTime($data, $timezone); + + if ($preserveContextTimezone) { + $object->setTimezone($timezone); + } + + return \DateTime::class === $class ? $object : new \DateTimeImmutable($object->format(\DATE_RFC3339)); } catch (\Exception $e) { throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); } @@ -164,4 +190,22 @@ private function getTimezone(array $context) return $dateTimeZone instanceof \DateTimeZone ? $dateTimeZone : new \DateTimeZone($dateTimeZone); } + + private function isPreserveContextTimezone(array $context): bool + { + //Version 5.0 of Symfony/Serializer will always preserve the context timezone, so this method always will return true, unless the timezone equals null. + if (null === $this->getTimezone($context)) { + return false; + } + + if (!isset($context[self::PRESERVE_CONTEXT_TIMEZONE]) && !isset($this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE])) { + @trigger_error('Not setting the boolean "PRESERVE_CONTEXT_TIMEZONE" flag is deprecated. Set the flag to "true" to apply the context timezone consistently, otherwise setting the flag to "false" will preserve default PHP behavior.', E_USER_DEPRECATED); + } + + if (!isset($context[self::PRESERVE_CONTEXT_TIMEZONE])) { + return (bool) isset($this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE]) ? $this->defaultContext[self::PRESERVE_CONTEXT_TIMEZONE] : false; + } + + return (bool) isset($context[self::PRESERVE_CONTEXT_TIMEZONE]) ? $context[self::PRESERVE_CONTEXT_TIMEZONE] : false; + } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php index 7d087d7d5eda5..2e542735dab81 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/DateTimeNormalizerTest.php @@ -26,7 +26,11 @@ class DateTimeNormalizerTest extends TestCase protected function setUp() { - $this->normalizer = new DateTimeNormalizer(); + $this->normalizer = new DateTimeNormalizer( + [ + DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false, + ] + ); } public function testSupportsNormalization() @@ -208,6 +212,35 @@ public function testDenormalizeUsingTimezonePassedInConstructor() $this->doTestDenormalizeUsingTimezonePassedInConstructor(); } + public function testDenormalizeUsingTimezonePassedInContext() + { + // Test correction of Timezone when timezone information is added in both the date string and the context of the normalizer. + $normalizer = new DateTimeNormalizer( + [ + // This is different from Europe times and has NO daylight saving, so tests always pass. + DateTimeNormalizer::TIMEZONE_KEY => 'Australia/Brisbane', + DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => true, + ] + ); + + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-27T16:39:26+01:00', \DateTimeInterface::class)->format(\DATE_RFC3339), 'Non UTC'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-27T15:39:26+00:00', \DateTime::class)->format(\DATE_RFC3339), 'UTC'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-28 01:39:26', \DateTime::class)->format(\DATE_RFC3339), 'No timezone in string'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-28T01:39:26+10:00', \DateTimeInterface::class)->format(\DATE_RFC3339), 'Same timezone as constructor'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('@1453909166', \DateTimeImmutable::class)->format(\DATE_RFC3339), 'Timestamp string assumes UTC'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-27T15:39:26+00:00', \DateTimeImmutable::class, \DATE_RFC3339)->format(\DATE_RFC3339), 'Check format change.'); + $this->assertSame('2016-01-28T01:39:26+10:00', $normalizer->denormalize('2016-01-27T15:39:26+00:00', \DateTimeImmutable::class, null, [DateTimeNormalizer::FORMAT_KEY => \DATE_RFC3339])->format(\DATE_RFC3339), 'Check context format denormalization.'); + + $this->assertSame('2016-01-28T00:39:26+09:00', $normalizer->denormalize('@1453909166', \DateTimeImmutable::class, null, [DateTimeNormalizer::TIMEZONE_KEY => 'Japan'])->format(\DATE_RFC3339), 'Check timezone context change.'); + + $this->assertSame('2016-01-27T15:39:26+00:00', $normalizer->denormalize('@1453909166', \DateTimeImmutable::class, null, [DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false])->format(\DATE_RFC3339), 'Should revert to UTC'); + + $normalizer = new DateTimeNormalizer(); + $this->assertSame('2016-01-27T15:39:26+00:00', $normalizer->denormalize('2016-01-27T15:39:26+00:00', \DateTimeInterface::class, null, [DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false])->format(\DATE_RFC3339), 'Should be UTC'); + $this->assertSame('+09:00', $normalizer->denormalize('2016-01-27T16:39:26+01:00', \DateTime::class, null, [DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => true, DateTimeNormalizer::TIMEZONE_KEY => '+09:00'])->getTimezone()->getName(), '+09:00 timezone with context.'); + $this->assertSame('2016-01-27T15:39:26+00:00', $normalizer->denormalize('@1453909166', \DateTimeImmutable::class, null, [DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false])->format(\DATE_RFC3339), 'Timestamp string assumes UTC, and should be UTC'); + } + public function testLegacyDenormalizeUsingTimezonePassedInConstructor() { $this->doTestDenormalizeUsingTimezonePassedInConstructor(true); @@ -217,7 +250,7 @@ private function doTestDenormalizeUsingTimezonePassedInConstructor(bool $legacy { $timezone = new \DateTimeZone('Japan'); $expected = new \DateTime('2016/12/01 17:35:00', $timezone); - $normalizer = $legacy ? new DateTimeNormalizer(null, $timezone) : new DateTimeNormalizer([DateTimeNormalizer::TIMEZONE_KEY => $timezone]); + $normalizer = $legacy ? new DateTimeNormalizer([DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => true], $timezone) : new DateTimeNormalizer([DateTimeNormalizer::TIMEZONE_KEY => $timezone, DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => true]); $this->assertEquals($expected, $normalizer->denormalize('2016.12.01 17:35:00', \DateTime::class, null, [ DateTimeNormalizer::FORMAT_KEY => 'Y.m.d H:i:s', @@ -234,11 +267,12 @@ public function testDenormalizeUsingFormatPassedInContext() /** * @dataProvider denormalizeUsingTimezonePassedInContextProvider */ - public function testDenormalizeUsingTimezonePassedInContext($input, $expected, $timezone, $format = null) + public function testDenormalizeUsingTimezonePassedInDefaultContext($input, $expected, $timezone, $format = null) { $actual = $this->normalizer->denormalize($input, \DateTimeInterface::class, null, [ DateTimeNormalizer::TIMEZONE_KEY => $timezone, DateTimeNormalizer::FORMAT_KEY => $format, + DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false, ]); $this->assertEquals($expected, $actual); diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php index 4c4448dfe4b0e..a585e08b67672 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php @@ -838,7 +838,7 @@ public function testDenomalizeRecursive() { $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); $normalizer = new ObjectNormalizer(null, null, null, $extractor); - $serializer = new Serializer([new ArrayDenormalizer(), new DateTimeNormalizer(), $normalizer]); + $serializer = new Serializer([new ArrayDenormalizer(), new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d', DateTimeNormalizer::PRESERVE_CONTEXT_TIMEZONE => false]), $normalizer]); $obj = $serializer->denormalize([ 'inner' => ['foo' => 'foo', 'bar' => 'bar'], From 1dd23a2ca494135157224bce65779ec74227046d Mon Sep 17 00:00:00 2001 From: Craig Rayner Date: Tue, 9 Jul 2019 05:06:00 +1000 Subject: [PATCH 2/2] Update DateTimeNormalizer.php Incorporate changes as suggested by @fabpot --- .../Serializer/Normalizer/DateTimeNormalizer.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php index 5db3aa24f60f3..f1e5ff4a9de91 100644 --- a/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php @@ -26,11 +26,11 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface, const TIMEZONE_KEY = 'datetime_timezone'; /** - * In PHP, The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). + * In PHP, the $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00). * * The denormalizer assumes that all DateTimeInterface object returned will have the timezone returned by the getTimezone() method. * Default PHP behavior will occur if the getTimezone() method returns null or context[self::PRESERVE_CONTEXT_TIMEZONE] is set to false. - * This flag will be ignored in Symfony/Serializer 5.0+ + * This flag will be ignored in Symfony 5.0+ */ const PRESERVE_CONTEXT_TIMEZONE = 'preserve_context_timezone'; @@ -53,14 +53,14 @@ public function __construct($defaultContext = [], \DateTimeZone $timezone = null ]; if (!\is_array($defaultContext)) { - @trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); + @trigger_error('Passing the date time format directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); $defaultContext = [self::FORMAT_KEY => (string) $defaultContext]; $defaultContext[self::TIMEZONE_KEY] = $timezone; } if (!isset($defaultContext[self::TIMEZONE_KEY]) && null !== $timezone) { - @trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); + @trigger_error('Passing the time zone directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED); $defaultContext[self::TIMEZONE_KEY] = $timezone; } @@ -193,7 +193,7 @@ private function getTimezone(array $context) private function isPreserveContextTimezone(array $context): bool { - //Version 5.0 of Symfony/Serializer will always preserve the context timezone, so this method always will return true, unless the timezone equals null. + // Version 5.0 of Symfony/Serializer will always preserve the context timezone, so this method always will return true, unless the timezone equals null. if (null === $this->getTimezone($context)) { return false; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy