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

Conversation

joelwurtz
Copy link
Contributor

@joelwurtz joelwurtz commented Sep 2, 2024

…location header

Q A
Branch? 3.2
Tickets
License MIT
Doc PR

When it generate the iri, it misses the context, so if we need some uri variables from the context to generate the route it will not work (like a object_id not available in the resource)

Without this, it will throw an error that it cannot generate an iri item for this resource

@@ -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.

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 this pull request may close these issues.

2 participants