Skip to content

refactor(state): merge parameter and link security #7195

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

Closed
wants to merge 3 commits into from

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented Jun 4, 2025

Q A
Branch? 4.1 (experimental feature changes, no bc)
Tickets Closes #7061
License MIT

I was working on parameters and it struck me that @KDederichs actually wrote a ParameterProvider for links that could be quite useful with all kind of parameters (not only uri variables also knowns as links, especially that a Link extends Parameter). This refactor removes a lot of code by reusing the logic of the actual parameters (that also have a security) and adding the features that were only present on Links to every parameters.

Tests left to be done:

This mimics the behavior of the well-known MapEntity of symfony:

use ApiPlatform\Metadata\Get;
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider'
use App\Entity\Author;

#[Get(
    uriTemplate: '/author/{authorId}/books`,
    uriVariables: ['authorId' => new Link(provider: ReadLinkParameterProvider::class, fromClass: Author::class)]
)]

After that if an Author is not found this throws a 404 (disable this using extraProperties: ['throw_not_found' => false] on the Link (or Parameter of any kind). Then uriVariables['authorId'] should contain the Author from the database.

This can be applied to any kind of parameters:

use ApiPlatform\Metadata\Get;
use ApiPlatform\State\ParameterProvider\ReadLinkParameterProvider'
use App\Entity\Author;

#[GetCollection(
    uriTemplate: '/authors`,
    parameters: ['author' => new QueryParameter(provider: ReadLinkParameterProvider::class, extraProperties: ['resource_class' => Author::class, 'uri_template' => '/authors/{id}'])]
)]
#[Get(
    uriTemplate: '/authors/{id}`,
)]

This also resolves #7112 in my opinion ping @joelwurtz @lyrixx

@soyuka soyuka force-pushed the link-parameter-providers branch 13 times, most recently from 9cc4d90 to 898b555 Compare June 8, 2025 20:56
@soyuka soyuka force-pushed the link-parameter-providers branch from 898b555 to dbb7cd5 Compare June 9, 2025 06:27
@soyuka soyuka closed this Jun 10, 2025
@soyuka soyuka force-pushed the link-parameter-providers branch from a8d9ce2 to c59b53f Compare June 10, 2025 11:18
@soyuka
Copy link
Member Author

soyuka commented Jun 10, 2025

#7200

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.

1 participant