core: The property is not readable because it is typed "string"

API Platform version(s) affected: 2.6.0

Description
When doing a PATCH operation with a nested collection which has typed properties, it throws an exception

How to reproduce

/**
 * @ApiResource(denormalizationContext={"groups"={"group:write"}})
 */
class Group {
    /**
     * @ORM\OneToMany(targetEntity=Item::class, mappedBy="group", cascade={"persist", "remove"}, orphanRemoval=true)
     * @Groups({"group:write"})
     */
    private Collection $items;
...
}

/**
 * @ApiResource
 */
class Item {
    /**
     * @ORM\Column(type="string", length=255)
     * @Groups({"group:write"})
     */
    private ?string $name;
...
}
PATCH /api/groups/1
Content-type: application/merge-patch+json
{
"items": [{"id": "/api/items/1", "name": "test"}]
}
The property "App\Entity\Item::$name" is not readable because it is typed "string". You should initialize it or declare a default value instead.

Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException
in vendor/symfony/property-access/PropertyAccessor.php (line 448)
in vendor/symfony/property-access/PropertyAccessor.php -> readProperty (line 115)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> getValue (line 563)
in vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php -> getAttributeValue (line 329)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> denormalize (line 250)
in vendor/api-platform/core/src/Serializer/ItemNormalizer.php -> denormalize (line 70)
in vendor/symfony/serializer/Serializer.php -> denormalize (line 208)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> denormalize (line 486)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> denormalizeRelation (line 445)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> denormalizeCollection (line 737)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> createAttributeValue (line 403)
in vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php -> setAttributeValue (line 336)
in vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php -> denormalize (line 250)
in vendor/api-platform/core/src/Serializer/ItemNormalizer.php -> denormalize (line 70)
in vendor/symfony/serializer/Serializer.php -> denormalize (line 208)
in vendor/symfony/serializer/Serializer.php -> denormalize (line 144)
in vendor/api-platform/core/src/EventListener/DeserializeListener.php -> deserialize (line 106)
in vendor/symfony/event-dispatcher/Debug/WrappedListener.php -> onKernelRequest (line 117)
in vendor/symfony/event-dispatcher/EventDispatcher.php -> __invoke (line 230)
in vendor/symfony/event-dispatcher/EventDispatcher.php -> callListeners (line 59)
in vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php -> dispatch (line 151)
in vendor/symfony/http-kernel/HttpKernel.php -> dispatch (line 133)
in vendor/symfony/http-kernel/HttpKernel.php -> handleRaw (line 79)
in vendor/symfony/http-kernel/Kernel.php -> handle (line 195)

Possible Solution
?

Additional Context
this works in 2.5.9 and 2.5.10

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 23 (8 by maintainers)

Most upvoted comments

Raised an issue in symfony for possible serializer fix: symfony/symfony#40578

@vuryss , thank you for your fix in Symfony. It is in Symfony 5.4 milestone now.

Next month guys - PHP 8.1, Symfony 5.4 & 6.0 and this should come with it 🤞

Yes, thanks @vuryss 🥇 💯, with your contribution, we’re able to finally switch to api-platform 2.6 and enjoy the other features!

@zexa it’s more a workaround than a solution … having to define a default value for all your variables is a bit heavy and more importantly not very clean imo.

I think this is more of a PHP error and not APIP related. Like said in the error message, you should initalize the property.

private ?string $name = null;