Core: Regression: Unable to have resource without an ID since v2.2.6

Created on 25 Jun 2018  路  12Comments  路  Source: api-platform/core

In version 2.2.5, I had a resource that didn't have an id. There were several other required parameters, that could be considered a composite primary key. I am loading the actual data with a Item Data Provider.

This was working great for me until I recently upgraded to 2.2.8. By manually going back one version at a time and then stepping through the code with xDebug, I was able to figure out what was going on. The changes in the ApiPlatform\Core\EventListener\ReadListener which happened it 6d40a8c27d3779809dae69bd09d1380f5948edf3 are where the functionality changed.

Was this a purposeful design change or a regression? Is there a suggested workaround?

invalid

Most helpful comment

We should not revert the bug fix.

All 12 comments

I'm not sure how this change makes your code break if you have your own data provider ?

Before this commit, line 87 would get the value from the Data Provider. After this commit, the new line 86 (extractIdentifiers method) throws an exception with the 'id' parameter is not in the url.

Mhhh I think I get it:

Before the commit: $id = $request->attributes->get('id'); is given as attribute to the DataProvider even if the value is null.

After: id isn't there so it throws.

Am I correct?

Right on. Sorry for not being clearer originally. 馃槵

To me we fixed a bug, id should never be null in the item data provider... Anyway, because of the BC promise I'm going to put back the old behavior asap! Thanks for the report!

Was it allowed by the PHPDoc?

Original: https://github.com/api-platform/core/commit/d2a672902de3cba306ee22d9a4faeb6da01ade1f#diff-36a40c3e3dcd2e626e1516c9ef9f2093

+     * @param int|string $id

I added array later:

     * @param array|int|string $id

Not from first sight no.

We should not revert the bug fix.

@jeffreymb I'm sorry but you've hit a wontfix... Would it be possible for you to explain your use case where you have:

  • an ItemDataProvider
  • no id in your url (no {id} param)

I'll try to help you to find another solution to the problem! Thanks, I hope that you understand why we can't revert the bug fix :p.

I can understand about the wontfix.

My situation is an api endpoint to access data from a hierarchy like Volume -> Chapter -> Paragraph. Volume and chapter are required parameters to pass to the endpoint. There are a few other optional parameters also, but no obvious primary key other than the composite of all three (volume/chapter/paragraph) options. I suppose I could fudge it and pick one of volume/chapter and call it the id, but surely there is a better solution.

On what class is your ApiResource for the latter?

My first guess would be a custom Controller (ie: don't go through the ReadListener, set _api_receive to false in the operation attributes, handle the data thing yourself).

I have a single Reference class that has the ApiResource.

The idea of a custom controller had crossed my mind, I'll check into that further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stipic picture stipic  路  3Comments

silverbackdan picture silverbackdan  路  3Comments

DenisVorobyov picture DenisVorobyov  路  3Comments

lukasluecke picture lukasluecke  路  3Comments

kate-kate picture kate-kate  路  3Comments