Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow_extra_attributes and update nested entity @id #6225

Open
ili101 opened this issue Mar 16, 2024 · 2 comments · May be fixed by #6274
Open

allow_extra_attributes and update nested entity @id #6225

ili101 opened this issue Mar 16, 2024 · 2 comments · May be fixed by #6274

Comments

@ili101
Copy link

ili101 commented Mar 16, 2024

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

@soyuka
Copy link
Member

soyuka commented Mar 29, 2024

specify this inside the normalizerContext?

@ili101
Copy link
Author

ili101 commented Mar 29, 2024

specify this inside the normalizerContext?

Yes, ili101@7b84701 then you can:

#[ApiResource(
    denormalizationContext: [
        AbstractNormalizer::GROUPS => ['Main:write'],
        'additional_allowed_attributes' => ['@id', '@type', 'id', '@context'],
    ],
)]

@ili101 ili101 linked a pull request Mar 31, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants