Skip to content

Commit

Permalink
fix(serializer): disable_type_enforcement with null values (#5593)
Browse files Browse the repository at this point in the history
fixes #5584
  • Loading branch information
soyuka committed May 23, 2023
1 parent 547078c commit a0f12b6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Serializer/AbstractItemNormalizer.php
Expand Up @@ -753,7 +753,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
return $value;
}

if (null === $value && $type->isNullable()) {
if (null === $value && ($type->isNullable() || ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false))) {
return $value;
}

Expand Down
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5584;

use ApiPlatform\Metadata\ApiResource;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

#[ApiResource(denormalizationContext: [AbstractObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true])]
final class DtoWithNullValue
{
public \stdClass $dummy;
}
43 changes: 43 additions & 0 deletions tests/Serializer/AbstractItemNormalizerTest.php
Expand Up @@ -21,6 +21,7 @@
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Serializer\AbstractItemNormalizer;
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5584\DtoWithNullValue;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
Expand Down Expand Up @@ -1347,6 +1348,48 @@ public function testDenormalizePopulatingNonCloneableObject(): void
$this->assertSame($dummy, $actual);
$propertyAccessorProphecy->setValue($actual, 'name', 'bar')->shouldHaveBeenCalled();
}

public function testDenormalizeObjectWithNullDisabledTypeEnforcement(): void
{
$data = [
'dummy' => null,
];

$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(DtoWithNullValue::class, [])->willReturn(new PropertyNameCollection(['dummy']));

$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(DtoWithNullValue::class, 'dummy', [])->willReturn((new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, nullable: true)])->withDescription('')->withReadable(true)->withWritable(true));

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(null, DtoWithNullValue::class)->willReturn(DtoWithNullValue::class);
$resourceClassResolverProphecy->isResourceClass(DtoWithNullValue::class)->willReturn(true);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(NormalizerInterface::class);

$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
$propertyAccessorProphecy->reveal(),
null,
null,
[],
null,
null,
]);
$normalizer->setSerializer($serializerProphecy->reveal());

$context = [AbstractItemNormalizer::DISABLE_TYPE_ENFORCEMENT => true];
$actual = $normalizer->denormalize($data, DtoWithNullValue::class, null, $context);

$this->assertInstanceOf(DtoWithNullValue::class, $actual);
$this->assertEquals(new DtoWithNullValue(), $actual);
}
}

class ObjectWithBasicProperties
Expand Down

0 comments on commit a0f12b6

Please sign in to comment.