-
-
Notifications
You must be signed in to change notification settings - Fork 918
Allow blank nodes in JSON-LD #956
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 blank nodes in JSON-LD #956
Conversation
e3decb3
to
d064f7f
Compare
d064f7f
to
30a9715
Compare
private $itemDataProvider; | ||
private $routeNameResolver; | ||
private $router; | ||
private $propertyAccessor; | ||
private $itemIdentifiersExtractor; | ||
|
||
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ItemDataProviderInterface $itemDataProvider, RouteNameResolverInterface $routeNameResolver, RouterInterface $router, PropertyAccessorInterface $propertyAccessor = null) |
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 put a reminder somewhere that (when we can break things again) we need to remove those injections and rather directly inject ItemIdentifiersExtractor.
$data['@id'] = $this->iriConverter->getIriFromItem($object); | ||
$identifiers = $this->itemIdentifiersExtractor->getIdentifiersFromItem($object); | ||
|
||
$data['@id'] = count(array_filter($identifiers)) > 0 ? $this->iriConverter->getIriFromItem($relatedObject) : $this->blankNodeIdentifiersGenerator->getBlankNodeIdentifier($relatedObject, $context['jsonld_document_root']); |
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 add a method that takes identifiers and resolves to one of those to avoid repetition?
Is $context['jsonld_document_root']
used only to cache the spl_object_hash
?
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.
count(array_filter($identifiers)) > 0
can be replaced by array_filter($identifiers)
(faster, easier to read).
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 add a method that takes identifiers and resolves to one of those to avoid repetition?
Sure. I'll move it to a private method.
Is $context['jsonld_document_root'] used only to cache the spl_object_hash?
We need to keep track of which JSON-LD document we're in. But of course there are many ways of doing it. I agree this is not ideal.
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.
Yeah, nothing wrong with how you did it, just wanted to make sure I understood ;)
$data['@id'] = $this->iriConverter->getIriFromItem($object); | ||
$identifiers = $this->itemIdentifiersExtractor->getIdentifiersFromItem($object); | ||
|
||
$data['@id'] = count(array_filter($identifiers)) > 0 ? $this->iriConverter->getIriFromItem($relatedObject) : $this->blankNodeIdentifiersGenerator->getBlankNodeIdentifier($relatedObject, $context['jsonld_document_root']); |
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.
count(array_filter($identifiers)) > 0
can be replaced by array_filter($identifiers)
(faster, easier to read).
|
||
$identifiers = $this->itemIdentifiersExtractor->getIdentifiersFromItem($relatedObject); | ||
|
||
return count(array_filter($identifiers)) > 0 ? $this->iriConverter->getIriFromItem($relatedObject) : $this->blankNodeIdentifiersGenerator->getBlankNodeIdentifier($relatedObject, $context['jsonld_document_root']); |
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.
Same here array_filter($identifiers)
is enough.
*/ | ||
public function getBlankNodeIdentifier($object, string $documentRootHash): string | ||
{ | ||
$objectHash = spl_object_hash($object); |
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'm not a fond of exposing such low level data from the system publicly. Is it really safe? Another potential problem is that this ID will be reused later by PHP. WDYT about using a UUID 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.
It's not being exposed in any way. The hash is only used to keep track of which objects we've already generated a blank node identifier for, and always return that same identifier for the same object (the scope is the JSON-LD document).
We cannot use UUID for this, as the point is to detect the same object instance.
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.
Nevermind got it.
@@ -415,7 +415,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array | |||
* | |||
* @return string|array | |||
*/ | |||
private function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, string $format = null, array $context) | |||
protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, string $format = null, array $context) |
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.
Can you mark this method as @internal
?
30a9715
to
0648775
Compare
Can you rebase? It should be ok to be merged. |
@dunglas There are no tests yet. |
You do you own tests 😈 Joking, if you don't have the time lmk |
effd916
to
8d12b1b
Compare
@dunglas I need some help rebasing this... There seems to have been a proliferation of |
f74f04f
to
58d7297
Compare
@@ -86,7 +96,7 @@ public function normalize($object, $format = null, array $context = []) | |||
$context = $this->initContext($resourceClass, $context); | |||
$context['api_normalize'] = true; | |||
|
|||
if (isset($context['resources'])) { | |||
if (isset($context['resources']) && $this->hasIri($object)) { | |||
$resource = $context['iri'] ?? $this->iriConverter->getIriFromItem($object); |
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.
Is the condition still needed? if it hasIri
context['iri']
is defined no?
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.
No. $context['iri']
is only set from the JsonLd ItemNormalizer
(I think). I don't know what's the reasoning behind, but I did not change it...
58d7297
to
56b76f4
Compare
56b76f4
to
4c6d144
Compare
kinda fixed by #2495 |
TODO: