Skip to content

allow_extra_attributes and update nested entity @id #6225

@ili101

Description

@ili101

Description
As mentioned in documentation https://api-platform.com/docs/core/serialization/#denormalization

The following rules apply when denormalizing embedded relations:

    If an @id key is present in the embedded resource, then the object corresponding to the given URI will be retrieved through the state provider. Any changes in the embedded relation will also be applied to that object.
    If no @id key exists, a new object will be created containing state provided in the embedded JSON document.

When setting allow_extra_attributes: false this results in Extra attributes are not allowed ("@id" is unknown).
If using json and not ld+json and sending id instead you get the same error on id.
P.S. not sure if intentional but for the update to actually work even if extra attributes allowed @id needs to also be provided on the parent entity.

How to reproduce
Test example https://github.com/ili101/api-platform/blob/subTest/Test.ps1 (uncomment allow_extra_attributes in api_platform.yaml)

Possible Solution
Allow @id (or maybe also @type @context) with allow_extra_attributes: false and ld+json.
Allow id with allow_extra_attributes: false and json.

Alternatively for more flexible solution, in the Entry ApiProperty or somewhere else add and option to set the base $allowedAttributes with default values here:

$allowedAttributes = [];

Something like:

    #[ApiProperty(
        ExtraAllowedExtraAttributes: ['@id', '@type'],
    )]

Additional Context
api-platform/api-platform#2096
api-platform/api-platform#2620
https://stackoverflow.com/questions/70379382/how-to-update-nested-entity-with-api-platform

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions