Skip to content

feat: custom link provider #5685

@divine

Description

@divine

Description

In order to enhance the capabilities of the Link in a subresource, there should be a way to easily integrate/modify custom relationships or data that the LinkHandlerTrait does not understand well.

Currently:

#[ApiResource(
    uriTemplate: '/questions/{id}/answer', 
    uriVariables: [
        'id' => new Link(
            fromClass: Question::class,
            fromProperty: 'answer'
        )
    ], 
    operations: [new Get()]
)]

What can be done?

#[ApiResource(
    uriTemplate: '/questions/{id}/answer', 
    uriVariables: [
        'id' => new Link(
            fromClass: Question::class,
            fromProperty: 'answer',
            provider: CustomLinkProvider::class,,
        )
    ], 
    operations: [new Get()]
)]

Why?

At present, integrating custom Link that have a customized relationship requires manually copying the CollectionProvider and configuring bindings. This is required because pagination and filters doesn't work otherwise. The following YAML configuration is needed as well:

App\State\DummyCollectionStateProvider:
    arguments:
        $resourceMetadataCollectionFactory: '@api_platform.metadata.resource.metadata_collection_factory'
        $managerRegistry: '@doctrine_mongodb'
        $collectionExtensions: !tagged api_platform.doctrine_mongodb.odm.aggregation_extension.collection
    tags:
        - { name: 'api_platform.state_provider', priority: -100, key: 'ApiPlatform\Doctrine\Odm\State\CollectionProvider' }
        - { name: 'api_platform.state_provider', priority: -100, key: 'api_platform.doctrine_mongodb.odm.state.collection_provider' }

A more convenient approach would be to build a custom aggregation like a custom Link provider, without relying on the implementation of the function found here: LinksHandlerTrait.php.

Unfortunately, the current implementation of custom state providers, as outlined in the documentation, does not offer the ability to hook into the LinksHandlerTrait and lacks built-in support for Pagination or Filters.

Example

To illustrate the need for such functionality, let's consider an issue I encountered while trying to resolve an odd relation problem: Pull Request #5659. A more flexible LinksHandlerTrait with support for custom aggregations could have provided a smoother resolution to this issue.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions