Skip to content

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

Closed

Conversation

teohhanhui
Copy link
Contributor

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

TODO:

  • Tests
  • Docs

@teohhanhui teohhanhui force-pushed the feature/jsonld-blank-nodes branch from e3decb3 to d064f7f Compare February 24, 2017 12:36
@teohhanhui teohhanhui requested review from dunglas and soyuka February 24, 2017 12:38
@teohhanhui teohhanhui force-pushed the feature/jsonld-blank-nodes branch from d064f7f to 30a9715 Compare February 24, 2017 12:41
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)
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 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']);
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 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?

Copy link
Member

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).

Copy link
Contributor Author

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.

Copy link
Member

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']);
Copy link
Member

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']);
Copy link
Member

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);
Copy link
Member

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?

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'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.

Copy link
Member

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)
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 mark this method as @internal?

@teohhanhui teohhanhui force-pushed the feature/jsonld-blank-nodes branch from 30a9715 to 0648775 Compare February 27, 2017 04:39
@dunglas
Copy link
Member

dunglas commented Mar 20, 2017

Can you rebase? It should be ok to be merged.

@teohhanhui
Copy link
Contributor Author

@dunglas There are no tests yet.

@soyuka
Copy link
Member

soyuka commented Mar 20, 2017

You do you own tests 😈

Joking, if you don't have the time lmk

@soyuka soyuka added this to the 2.1.0 milestone Mar 22, 2017
@teohhanhui teohhanhui force-pushed the feature/jsonld-blank-nodes branch 4 times, most recently from effd916 to 8d12b1b Compare May 5, 2017 07:32
@teohhanhui
Copy link
Contributor Author

@dunglas I need some help rebasing this... There seems to have been a proliferation of IriConverterInterface::getIriFromItem to more places. Perhaps we need a different approach?

@teohhanhui teohhanhui force-pushed the feature/jsonld-blank-nodes branch 4 times, most recently from f74f04f to 58d7297 Compare June 1, 2017 12:07
@@ -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);
Copy link
Member

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?

Copy link
Contributor Author

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...

@soyuka
Copy link
Member

soyuka commented Feb 15, 2019

kinda fixed by #2495

@soyuka soyuka closed this Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants