-
-
Notifications
You must be signed in to change notification settings - Fork 838
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
f1bbf03
to
6c77a8d
Compare
*/ | ||
public function getIriFromPlainIdentifier($id, string $resourceClass, array $context = []): string | ||
{ | ||
return $this->getIriFromItem($this->itemDataProvider->getItem($resourceClass, $id, null, $context + ['fetch_data' => true])); |
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.
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
src/Api/IriConverterInterface.php
Outdated
* | ||
* @return string | ||
*/ | ||
public function getIriFromPlainIdentifier($id, string $resourceClass, array $context = []): string; |
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.
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).
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.
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...
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.
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)); |
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.
you can drop the patch part
6cf7329
to
83b783a
Compare
83b783a
to
cd24e28
Compare
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]); |
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.
👍 I really like the fix now.
Could you also add the #1716 test + fix ? (best would be a cherry pick from that author to keep his contribution) |
cd24e28
to
bf51bfa
Compare
test from #1716 cherry-picked. |
33190b0
to
8a8c4eb
Compare
8a8c4eb
to
a53464f
Compare
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.
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; |
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.
why not remove the property ? Is it still used ?
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.
my bad, it's a protected one...
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.
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)); |
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.
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.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
What about merging #1834 in this one and use the same method (ie |
Nevermind, it doesn't solve #1840, as the first check done in the |
What is your identifier if not a string? In your issue it seems to be one? |
Let's discuss that in the related issue. :} |
725091a
to
13f91d5
Compare
08e6ed2
to
24f91c1
Compare
* | ||
* @author Hamza Amrouche <hamza.simperfit@gmail.com> | ||
*/ | ||
interface IriPlainIdentifierAwareConverterInterface extends IriConverterInterface |
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.
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.)
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.
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
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.
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 ;).
b164e7e
to
3d633d1
Compare
CHANGELOG.md
Outdated
@@ -1,5 +1,11 @@ | |||
# Changelog | |||
|
|||
## 2.3.0 | |||
|
|||
* Introduce a new interface `IriPlainIdentifierAwareConverterInterface` |
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.
This hasn't been updated to reflect the changes. 😜
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.
and btw we never change the changelog in merge requests it's done when releasing only
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.
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
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.
That's why I updated it, I've contributing some PRs to symfony lately and add to update it ;).
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.
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
021b954
to
1bc04f9
Compare
@@ -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)) { |
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.
Shouldn't we support the other path? I mean what if IRIConverter is not an instance of IriToIdentifierConverterInterface
(case where someone extends the class)?
6052490
to
949c198
Compare
PR rebased with master |
* | ||
* @author Hamza Amrouche <hamza.simperfit@gmail.com> | ||
*/ | ||
interface IriToIdentifierConverterInterface |
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.
IriToPlainIdentifier
? Because an IRI by definition is already an identifier (Internationalized Resource Identifier).
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.
see #1837 (comment)
* | ||
* @return string | ||
*/ | ||
public function getIriFromPlainIdentifier($id, string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): string; |
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.
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); |
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.
Why this change?
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.
IIRC, this change deprecation has been reverted in another PR.
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.
need to rebase, ill see
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 still there in master
@@ -43,6 +44,9 @@ | |||
|
|||
protected $propertyNameCollectionFactory; | |||
protected $propertyMetadataFactory; | |||
/** | |||
* @var IriToIdentifierConverterInterface|IriConverterInterface |
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.
I would fully split both, and add a new optional constructor parameter instead.
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.
You mean, splitting the classes too ?
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.
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) { |
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.
This test can be factorized with the one line 320 (store the result in a variable).
…DataProvider from AbstractItemNormalizer
… different than the IriConverterInterface
949c198
to
0b2c793
Compare
@dunglas done. |
can you rebase this @Simperfit ? |
@Simperfit Up is this pull request still valid? |
We will eventually deprecate plain identifiers, closing this. |
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.