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

feat(processor): add missing context when generating iri for content … #6574

Open
wants to merge 1 commit into
base: 3.2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/State/Processor/RespondProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$status ??= self::METHOD_TO_CODE[$method] ?? 200;

if ($hasData && $this->iriConverter) {
$iri = $this->iriConverter->getIriFromResource($originalData);
$iri = $this->iriConverter->getIriFromResource($originalData, context: $context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already refused a PR like this one, we don't know the nature of originalData and URI Variables in the context may not be the right ones anymore. Question is, how is it that we can't extract identifiers from originalData here?

Copy link
Contributor Author

@joelwurtz joelwurtz Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in this case identifiers are not in the data but only in the url, something like this :

#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/realm/{realmId}/data',
            uriVariables: [
                'realmId' => new Link(
                    toProperty: 'realm',
                    fromClass: Realm::class,
                    security: 'is_granted("REALM_GET_DATA", realm)',
                ),
            ],
            provider: RealmDataProvider::class,
        ),
    ],
)]
final class Data {
    public function __construct(public string $name)
    {
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Link suggests that you're retrieving the realmId from the property realm? Isn't the identifier in your $originalData?I wish we had a better extension point for https://github.com/api-platform/core/blob/main/src/Symfony/Routing/IriConverter.php#L187 instead of adding the context there as you won't be able to fix the tests with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No i don't have this property in my entity, AFAIK the link never map the property to the entity ? (We tried to do that in other api calls and it was never linked)

Copy link
Contributor Author

@joelwurtz joelwurtz Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution would be to be able to configure an IriConverter for a resource, so we can override this behavior per operation / resource ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Link should be a relation between the Resource and something else, in your example the configuration is likely wrong and using Link is useless. Yes the only solution here is to decorate the IriConverter.

$headers['Content-Location'] = $iri;

if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method) {
Expand Down
Loading