-
-
Notifications
You must be signed in to change notification settings - Fork 861
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
Allow collection paths with wildcard parameters (e.g. /users/{id}/tasks} #885
Allow collection paths with wildcard parameters (e.g. /users/{id}/tasks} #885
Conversation
The IRI generator must work even in a non HTTP context (in CLI for instance), RequestStack cannot be a hard dependency. |
What we can probably do is finding a convention to populate router parameters using data of the related resource. Example: /foos/{foo_id}/bars/{id}
However, I'm not sure it's worth it (it will introduce a lot of complexity for no real advantage compared to put every resources at the root). |
By the way, you can already do this kind of logic by decorating the |
That's what I thought at the first place, that RequestStack can not be used in IriCoverter as a hard dependency. Moreover, there is one more problem. When I define several "get" collection operations, e.g. "/bars" and "/foos/{foo_id}/bars" the @id of the collection is always taken from the first operation. |
@dunglas I changed the code, what do you think about it? |
@dunglas ping |
I don't get exactly what it does. Can you add a functional test to explicit it (and to be sure this is properly tested). |
aef053a
to
23006c2
Compare
@dunglas the point is that a recourse can participate in several collections e.g. /items, /item-owner/{item-owner-id}/items, /brand-new-items. Some recourses even do not need to have a common collection where you can find each item of the recourse e.g. comments. It makes sense to have paths like /post/{post-id}/comments or /user/{user-id}/comments, but who does ever need just /comments? The functional tests are passed well on my local machine, what could be the reason they don't here? |
The failure is related to CS errors, run PHP CS Fixer. |
9eb9a4d
to
da01edd
Compare
da01edd
to
d8d0e91
Compare
@dunglas yes, it was CS errors, but along with swagger errors. I changed behavior of DocumentNormalizer to parse paths and to put placeholders {} as required path parameters. What do you think? Can we merge it? |
@dunglas or do I get it wrong and Hydra is not supposed to do something like that? |
I need to find some time to review this PR, sorry for the delay @samusenkoiv. |
Can you explain how URL for such patterns are generated (from where come the value replacing the |
@dunglas the URL is taken from the context. While the context is created in here https://github.com/api-platform/core/blob/master/src/EventListener/SerializeListener.php#L61, which means it is taken from the current request |
$filtersParameters = $collection ? $this->getFiltersParameters($resourceClass, $operationName, $resourceMetadata) : []; | ||
$parameters = array_merge($pathParameters, $filtersParameters); | ||
|
||
if (!empty($parameters)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use !$parameters
(the call to empty is useless).
@@ -137,7 +144,7 @@ public function getRelatedToDummyFriend() | |||
/** | |||
* Set relatedToDummyFriend. | |||
* | |||
* @param relatedToDummyFriend the value to set | |||
* @param relatedToDummyFriend $relatedToDummyFriend |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be @param RelatedToDummyFriend $relatedToDummyFriend
@dunglas check it out |
Nested collections! This is what I've been thinking of for a long time. But I don't think it's so simple to implement. This PR does not seem to change the IRI generation, which I think is crucial, e.g. |
Also, I think this should be handled at the normalizer level, i.e. allowing to serialize / deserialize into a nested collection (as opposed to embedding directly, which stops making sense anyway when there are too many items in the collection). Using custom routes like what's done in this PR is a band-aid solution that does not address the real issue. 😄 |
@teohhanhui I think you got it wrong. The PR does not pretend to implement or help with nested collection. Instead it does the following:
|
@dunglas @teohhanhui so you guys think it is not worth merging the PR? |
I'm in favor of merging this one. What do you think @teohhanhui? |
Oops, that was by accident |
so this doesn't work: I think this isn't really optimized, would you be able to show me the SQL query that is executed? Does it serialize the parent too before rendering? The regex should be tested thoroughly with unit tests. But, isn't there something better to use from the Symfony path tools or the router? |
This PR seems to be related to the question I asked here api-platform/api-platform#199 |
@esistgut your case is not related. @samusenkoiv I thought a bit more about this feature. As said in my previous comment I'd love to have the corresponding query and serialization informations.
IMO this can be misleading, because the What we can and should do instead is to "allow operations to retrieve and serialize only the association of a parent resource". This will give the same result as your patch does but it'll go deeper and notify the data providers and the serialization about what should be fetched. I don't think it's easy to implement but I'd start with the IRI declaration and try to make the Router aware of "I want a
Where
There is a strategy in place for composite identifiers in the Doctrine data provider. The rest should be handled in the IriConverter and related. |
Definitely agree with @soyuka re the routing. |
@samusenkoiv
I do not understand under what circumstances it would be appropriate to use? (Other than the nested collection use case you've described, because I think this is not the right way to get there.) |
Closing in favor of #904 |
This is what I got so far. It is not covered by tests yet.
I just wanted to confirm if you consider this place as an appropriate one for these changes.
@dunglas what do you think?