Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  [Form] Remove unnecessary imports
  minor #58472 CS: clean some whitespaces/indentation (keradus)
  Fix newline
  harden test to not depend on the system's configured default timezone
  [Form] Support intl.use_exceptions/error_level in NumberToLocalizedStringTransformer
  [Doctrine][Messenger] Use common sequence name to get id from Oracle
  [ExpressionLanguage] Add missing test case for `Lexer`
  [FrameworkBundle] Fix passing request_stack to session.listener
  ensure session storages are opened in tests before destroying them
  [Serializer] Fix `ObjectNormalizer` gives warnings on normalizing with public static property
  [HttpKernel] Correctly merge `max-age`/`s-maxage` and `Expires` headers
  [Security][Validator] Check translations for Czech
  [Security] Fix serialized object representation in tests
  [DoctrineBridge] Fix risky test warnings
  [Serializer] Catch `NotNormalizableValueException` for variadic parameters
  • Loading branch information
xabbuh committed Oct 9, 2024
2 parents e71f703 + 537f125 commit 17aeca1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,16 @@ protected function instantiateObject(array &$data, string $class, array &$contex

$variadicParameters = [];
foreach ($data[$key] as $parameterKey => $parameterData) {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
try {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
} catch (NotNormalizableValueException $exception) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $exception;
}

$context['not_normalizable_value_exceptions'][] = $exception;
$params[$paramName] = $parameterData;
}
}

$params = array_merge(array_values($params), $variadicParameters);
Expand Down
2 changes: 1 addition & 1 deletion Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string

if ($context['_read_attributes'] ?? true) {
if (!isset(self::$isReadableCache[$class.$attribute])) {
self::$isReadableCache[$class.$attribute] = (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute)) || $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
self::$isReadableCache[$class.$attribute] = $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute) || (\is_object($classOrObject) && $this->propertyAccessor->isReadable($classOrObject, $attribute));
}

return self::$isReadableCache[$class.$attribute];
Expand Down
24 changes: 24 additions & 0 deletions Tests/Fixtures/DummyWithVariadicParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Uid\Uuid;

class DummyWithVariadicParameter
{
public array $variadic;

public function __construct(Uuid ...$variadic)
{
$this->variadic = $variadic;
}
}
10 changes: 10 additions & 0 deletions Tests/Normalizer/ObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,16 @@ public function testDenormalizeWithPropertyPath()

$this->assertEquals($expected, $obj);
}

public function testObjectNormalizerWithAttributeLoaderAndObjectHasStaticProperty()
{
$class = new class {
public static string $foo;
};

$normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));
$this->assertSame([], $normalizer->normalize($class));
}
}

class ProxyObjectDummy extends ObjectDummy
Expand Down
15 changes: 15 additions & 0 deletions Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicParameter;
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithVariadicProperty;
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooImplementationDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooInterfaceDummyDenormalizer;
Expand Down Expand Up @@ -1660,6 +1662,19 @@ public function testPartialDenormalizationWithMissingConstructorTypes()

$this->assertSame($expected, $exceptionsAsArray);
}

public function testPartialDenormalizationWithInvalidVariadicParameter()
{
$json = '{"variadic": ["a random string"]}';

$serializer = new Serializer([new UidNormalizer(), new ObjectNormalizer()], ['json' => new JsonEncoder()]);

$this->expectException(PartialDenormalizationException::class);

$serializer->deserialize($json, DummyWithVariadicParameter::class, 'json', [
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
]);
}
}

class Model
Expand Down

0 comments on commit 17aeca1

Please sign in to comment.