Skip to content

Problem with custom variables in query / uriTemplate #5184

Closed
@bizley

Description

@bizley

API Platform version(s) affected: 3.0.3

Description
If you set custom uriTemplate for an operation with non-identifiers as variables you will get 404 or 500.

How to reproduce
Add operation with template similar to:

uriTemplate: '/photos/{uuid}/resize/{width}/{height}',

where {uuid} is an UUID identifier.

As I can see additional variables are "resolved" as identifiers for the relations in the data provider resulting in query broken or yielding no results at all.

Possible Solution
I have made something I called NoExtraLinkProvider which is basically ApiPlatform\Doctrine\Orm\State\ItemProvider with additional code:

if ($operation instanceof HttpOperation && array_key_exists('uuid', $uriVariables)) {
    $uriVariables = ['uuid' => $uriVariables['uuid']];
    $operation = $operation->withUriVariables(['uuid' => $operation->getUriVariables()['uuid']]);
}

which removes any uriVariables added automatically by the framework. With help of that I can get the Photo resource by {uuid} and pass additional {with} and {height} to my controller.

But this does not help with collection operations (without {uuid}, different resource but similar problem, like /resources/search/{search}) and I don't know what to do here.

Additional Context
I did not find how to do this in v3, in v2 it worked without any problems. Maybe I'm doing something wrong (most probably) or maybe there is something missing in this beautiful framework. Please help or point me to the proper place to find help. Thank you.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions