Skip to content

Add a BC layer on data providers and deprecation errors #1913

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

Merged
merged 1 commit into from
May 3, 2018

Conversation

antograssiot
Copy link
Contributor

Q A
Bug fix? kind of
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

The introduction of identifier denormalization changes the type of the passed $id to the getItem() functions in custom data providers from a string to an array.

This patch allow to continue to use old data providers and will warn users that they have to update their code meanwhile.

WDYT @soyuka @dunglas ?

@@ -35,10 +35,10 @@ public function getItem(string $resourceClass, $id, string $operationName = null
{
// Retrieve the blog post item from somewhere
$cnr = new ContainNonResource();
$cnr->id = $id['id'];
$cnr->id = $id;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted Antoine's changes in this custom dataprovider so it proves it will continue to work in behat tests "the old way"

@antograssiot
Copy link
Contributor Author

This can be reviewed but should not be merged in 2.2, it should target master when it will be cleaned.

return $dataProvider->getItem($resourceClass, $id, $operationName, $context);
$identifier = $id;
if (!$dataProvider instanceof DenormalizedIdentifiersAwareItemDataProviderInterface) {
@trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated, your item data providers must implement "%s".', DenormalizedIdentifiersAwareItemDataProviderInterface::class), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it mandatory to trigger a deprecation notice? In most case, you'll not use composed identifiers (it's not really reliable), and implementing this new interface looks like an annoyance for most people to handle specific use cases.

I suggest to trigger a deprecation only if \count($identifier) > 1. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas it is your call, I've no strong opinion on it.
My argument is the change affects all data providers, even if you don't use composite identifiers. In 2.2 you would get "2" passed as $id and now you will get ['id' => 2] so in an ideal world, every data provider needs to be updated. Not triggering the deprecation error is ok, it just means that we won't be able to remove this piece of code in the next major version

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making it easy for the user to add new data providers matter, so I suggest to trigger the deprecation only if \count($identifier) > 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll update acordingly this morning

namespace ApiPlatform\Core\DataProvider;

/**
* Retrieves items from a persistence layer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this sentence by something like Marks data providers able to deal with complex identifiers denormalized as an array?

@antograssiot antograssiot force-pushed the dataprovider-bc-layer branch 2 times, most recently from bf307ae to cb4f7e2 Compare May 3, 2018 04:50
@antograssiot antograssiot changed the base branch from 2.2 to master May 3, 2018 05:07
@antograssiot antograssiot force-pushed the dataprovider-bc-layer branch from cb4f7e2 to 7e78dbf Compare May 3, 2018 05:56
@antograssiot antograssiot force-pushed the dataprovider-bc-layer branch from 7e78dbf to 32dea18 Compare May 3, 2018 06:01
@trigger_error(sprintf('Receiving "$id" as non-array in an item data provider is deprecated in 2.3 in favor of implementing "%s".', DenormalizedIdentifiersAwareItemDataProviderInterface::class), E_USER_DEPRECATED);
$identifier = http_build_query($identifier, '', ';');
} else {
$identifier = current($identifier);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update done @dunglas, do you want me to move this statement up to avoid the else ?

Copy link
Member

@dunglas dunglas May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not it's ok like this (it prevents a useless call to current)

@dunglas dunglas merged commit c304b11 into api-platform:master May 3, 2018
@dunglas
Copy link
Member

dunglas commented May 3, 2018

Thanks @antograssiot!

@antograssiot antograssiot deleted the dataprovider-bc-layer branch May 3, 2018 06:24
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.

2 participants