.../serialization/src/Normalizer/DateTimeIso8601Normalizer.php | 6 ++++++ core/modules/serialization/src/Normalizer/DateTimeNormalizer.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php b/core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php index a3ec201..343a258 100644 --- a/core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php +++ b/core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php @@ -64,12 +64,18 @@ public function denormalize($data, $class, $format = NULL, array $context = []) $context['datetime_allowed_formats'] = array_intersect_key($this->allowedFormats, ['date-only' => TRUE]); $datetime = parent::denormalize($data, $class, $format, $context); unset($context['datetime_allowed_formats']); + if (!$datetime instanceof \DateTime) { + return $datetime; + } return $datetime->format(DateTimeItemInterface::DATE_STORAGE_FORMAT); } else { $context['datetime_allowed_formats'] = array_diff_key($this->allowedFormats, ['date-only' => TRUE]); $datetime = parent::denormalize($data, $class, $format, $context); unset($context['datetime_allowed_formats']); + if (!$datetime instanceof \DateTime) { + return $datetime; + } $datetime->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)); return $datetime->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT); } diff --git a/core/modules/serialization/src/Normalizer/DateTimeNormalizer.php b/core/modules/serialization/src/Normalizer/DateTimeNormalizer.php index a6353a0..e0a72a9 100644 --- a/core/modules/serialization/src/Normalizer/DateTimeNormalizer.php +++ b/core/modules/serialization/src/Normalizer/DateTimeNormalizer.php @@ -68,6 +68,12 @@ protected function getNormalizationTimezone() { * {@inheritdoc} */ public function denormalize($data, $class, $format = NULL, array $context = []) { + // This only knows how to denormalize datetime strings and timestamps. If + // something else is received, let validation constraints handle this. + if (!is_string($data) && !is_numeric($data)) { + return $data; + } + // Loop through the allowed formats and create a \DateTime from the // input data if it matches the defined pattern. Since the formats are // unambiguous (i.e., they reference an absolute time with a defined time