Skip to content
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

Add a new getIriFromPlainIdentifier method and remove ItemDataProvider from AbstractItemNormalizer #1837

Conversation

Simperfit
Copy link
Contributor

@Simperfit Simperfit commented Apr 9, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets multiples need to be sure
License MIT
Doc PR todo

As discussed with @soyuka in #1834 we should not use ItemDataProvider in the normalizer and the the process works totally with IRIs.

So I'm adding a new method to convert from plainIdentifiers to IRIs.

*/
public function getIriFromPlainIdentifier($id, string $resourceClass, array $context = []): string
{
return $this->getIriFromItem($this->itemDataProvider->getItem($resourceClass, $id, null, $context + ['fetch_data' => true]));
Copy link
Member

Choose a reason for hiding this comment

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

Like I said on slack, we should use the router to rebuild the route not actually do a request. Code is almost the same as getIriFromItem

*
* @return string
*/
public function getIriFromPlainIdentifier($id, string $resourceClass, array $context = []): string;
Copy link
Member

@dunglas dunglas Apr 9, 2018

Choose a reason for hiding this comment

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

This is a BC break, and not something we want in this interface btw. Maybe can we introduce a new one to deal with plain identifiers (a bad practice, according to me).

Copy link
Member

@soyuka soyuka Apr 10, 2018

Choose a reason for hiding this comment

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

This is definitely a bad practice but people asked for it a lot so let's try to make everyone happy :D.

👍 to add a new service. It'll be better then injecting ItemDataProvider in the normalizers...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By a new service do you mean I should not implement the PlainIdentifierConverterInterfce in the IriConverter ? or like this it's ok ?

@@ -57,6 +57,9 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
$this->iriConverter = $iriConverter;
$this->resourceClassResolver = $resourceClassResolver;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
if (null !== $itemDataProvider) {
@trigger_error(sprintf('Passing a %s is deprecated since 2.3.0 and will not be possible in 3.0.0.', ItemDataProviderInterface::class));
Copy link
Member

@dunglas dunglas Apr 9, 2018

Choose a reason for hiding this comment

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

you can drop the patch part

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch 2 times, most recently from 6cf7329 to 83b783a Compare April 9, 2018 18:48
@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from 83b783a to cd24e28 Compare April 10, 2018 07:05
try {
return $this->itemDataProvider->getItem($className, $value, null, $context + ['fetch_data' => true]);
return $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromPlainIdentifier($value, $className), $context + ['fetch_data' => true]);
Copy link
Member

Choose a reason for hiding this comment

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

👍 I really like the fix now.

@soyuka
Copy link
Member

soyuka commented Apr 10, 2018

Could you also add the #1716 test + fix ? (best would be a cherry pick from that author to keep his contribution)

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from cd24e28 to bf51bfa Compare April 10, 2018 07:54
@Simperfit
Copy link
Contributor Author

test from #1716 cherry-picked.

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from 33190b0 to 8a8c4eb Compare April 10, 2018 08:13
@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from 8a8c4eb to a53464f Compare April 10, 2018 09:24
Copy link
Contributor

@Taluu Taluu left a comment

Choose a reason for hiding this comment

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

I think that would actually solve the problem I have on #1840, so 👍

@@ -57,6 +61,9 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
$this->iriConverter = $iriConverter;
$this->resourceClassResolver = $resourceClassResolver;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
if (null !== $itemDataProvider) {
@trigger_error(sprintf('Passing a %s is deprecated since 2.3 and will not be possible in 3.0.', ItemDataProviderInterface::class));
}
$this->itemDataProvider = $itemDataProvider;
Copy link
Contributor

Choose a reason for hiding this comment

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

why not remove the property ? Is it still used ?

Copy link
Contributor

Choose a reason for hiding this comment

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

my bad, it's a protected one...

Copy link
Member

Choose a reason for hiding this comment

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

We should but it'd be a backward compatibility break :|.

@@ -57,6 +61,9 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
$this->iriConverter = $iriConverter;
$this->resourceClassResolver = $resourceClassResolver;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
if (null !== $itemDataProvider) {
@trigger_error(sprintf('Passing a %s is deprecated since 2.3 and will not be possible in 3.0.', ItemDataProviderInterface::class));
Copy link
Contributor

Choose a reason for hiding this comment

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

missing a E_USER_DEPRECATED

try {
return $this->itemDataProvider->getItem($className, $value, null, $context + ['fetch_data' => true]);
return $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromPlainIdentifier($value, $className), $context + ['fetch_data' => true]);
} catch (ItemNotFoundException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (InvalidArgumentException $e) {

This comment was marked as resolved.

This comment was marked as resolved.

@soyuka
Copy link
Member

soyuka commented Apr 10, 2018

What about merging #1834 in this one and use the same method (ie getIriFromPlainIdentifier)?

@Taluu
Copy link
Contributor

Taluu commented Apr 10, 2018

Nevermind, it doesn't solve #1840, as the first check done in the \is_string() calls prevent from using plain identifiers...

@soyuka
Copy link
Member

soyuka commented Apr 10, 2018

Nevermind, it doesn't solve #1840, as the first check done in the \is_string() calls prevent from using plain identifiers...

What is your identifier if not a string? In your issue it seems to be one?
Best would be a behat test case that identifies your problem. As far as I can tell, the tests from #1547 have been maintained in #1762 ?

@Taluu
Copy link
Contributor

Taluu commented Apr 10, 2018

What is your identifier if not a string? In your issue it seems to be one?

Let's discuss that in the related issue. :}

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch 2 times, most recently from 725091a to 13f91d5 Compare April 10, 2018 14:00
@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from 08e6ed2 to 24f91c1 Compare April 11, 2018 16:07
*
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
interface IriPlainIdentifierAwareConverterInterface extends IriConverterInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it needs to extend the existing interface. We could just keep the type hint as IriConverterInterface, but check if it is also an IdentifierToIriConverterInterface. ("Plain" is redundant here.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Mainly because I prefer small, single-purpose interfaces.

Edit: Uhh, it's one of the SOLID principles... https://en.wikipedia.org/wiki/Interface_segregation_principle

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 agree with you, but in fact this interface says, "Ok i'm an IriConverter but I can manage plain identifier too".
I don't mind not extending the IriConverterInterface ;).

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch 4 times, most recently from b164e7e to 3d633d1 Compare April 12, 2018 08:13
CHANGELOG.md Outdated
@@ -1,5 +1,11 @@
# Changelog

## 2.3.0

* Introduce a new interface `IriPlainIdentifierAwareConverterInterface`
Copy link
Contributor

Choose a reason for hiding this comment

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

This hasn't been updated to reflect the changes. 😜

Copy link
Member

Choose a reason for hiding this comment

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

and btw we never change the changelog in merge requests it's done when releasing only

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if that's a good idea, to be honest. For example, Symfony requires updating the changelog: https://raw.githubusercontent.com/symfony/symfony/master/.github/PULL_REQUEST_TEMPLATE.md

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's why I updated it, I've contributing some PRs to symfony lately and add to update it ;).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I think it's a good practice too to update the changelog when adding new things or deprecating things so we gain time when releasing

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch 3 times, most recently from 021b954 to 1bc04f9 Compare April 12, 2018 10:06
@Simperfit Simperfit added this to In progress in 2.3.x Apr 13, 2018
@Simperfit Simperfit moved this from In progress to Done in 2.3.x Apr 13, 2018
@Simperfit Simperfit moved this from Done to In progress in 2.3.x Apr 13, 2018
@@ -313,9 +320,9 @@ protected function denormalizeRelation(string $attributeName, PropertyMetadata $

if (!\is_array($value)) {
// repeat the code so that IRIs keep working with the json format
if (true === $this->allowPlainIdentifiers && $this->itemDataProvider) {
if (true === $this->allowPlainIdentifiers && ($this->iriConverter instanceof IriToIdentifierConverterInterface && $this->iriConverter instanceof IriConverterInterface)) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we support the other path? I mean what if IRIConverter is not an instance of IriToIdentifierConverterInterface (case where someone extends the class)?

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch 3 times, most recently from 6052490 to 949c198 Compare May 4, 2018 08:49
@Simperfit
Copy link
Contributor Author

PR rebased with master

*
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
interface IriToIdentifierConverterInterface
Copy link
Member

Choose a reason for hiding this comment

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

IriToPlainIdentifier? Because an IRI by definition is already an identifier (Internationalized Resource 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.

*
* @return string
*/
public function getIriFromPlainIdentifier($id, string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string;
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to only allow arrays for the $id parameter, it will be more in sync with the work of @soyuka on complex identifiers.

@@ -59,7 +60,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
$this->identifierDenormalizer = $identifierDenormalizer;

if (null === $identifiersExtractor) {
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3', IdentifiersExtractorInterface::class), E_USER_DEPRECATED);
@trigger_error(sprintf('Not injecting "%s" is deprecated since API Platform 2.3 and will not be possible anymore in API Platform 3.', IdentifiersExtractorInterface::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.

Why this change?

Copy link
Member

Choose a reason for hiding this comment

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

IIRC, this change deprecation has been reverted in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to rebase, ill see

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It still there in master

@@ -43,6 +44,9 @@

protected $propertyNameCollectionFactory;
protected $propertyMetadataFactory;
/**
* @var IriToIdentifierConverterInterface|IriConverterInterface
Copy link
Member

Choose a reason for hiding this comment

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

I would fully split both, and add a new optional constructor parameter instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean, splitting the classes too ?

Copy link
Member

@dunglas dunglas May 9, 2018

Choose a reason for hiding this comment

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

maybe, but at least inject it 2 times

return $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromPlainIdentifier($value, $className), $context + ['fetch_data' => true]);
}

if (true === $this->allowPlainIdentifiers && $this->itemDataProvider) {
Copy link
Member

Choose a reason for hiding this comment

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

This test can be factorized with the one line 320 (store the result in a variable).

@Simperfit Simperfit force-pushed the feature/remove-item-data-provider-form-abstract-item-normalizer branch from 949c198 to 0b2c793 Compare May 9, 2018 12:58
@Simperfit
Copy link
Contributor Author

@dunglas done.

@soyuka
Copy link
Member

soyuka commented Jun 14, 2018

can you rebase this @Simperfit ?

@flug
Copy link
Contributor

flug commented Nov 14, 2019

@Simperfit Up is this pull request still valid?

@soyuka
Copy link
Member

soyuka commented Nov 28, 2020

We will eventually deprecate plain identifiers, closing this.

@soyuka soyuka closed this Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
2.3.x
  
In progress
Development

Successfully merging this pull request may close these issues.

None yet

7 participants