-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\JsonLd\Util; | ||
|
||
/** | ||
* Generates blank node identifiers scoped to each JSON-LD document. | ||
* | ||
* @author Teoh Han Hui <teohhanhui@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class BlankNodeIdentifiersGenerator | ||
{ | ||
const IDENTIFIER_PREFIX = '_:b'; | ||
|
||
private $blankNodeCounts = []; | ||
private $identifiers = []; | ||
|
||
/** | ||
* Gets a blank node identifier for an object, scoped to a JSON-LD document. | ||
* | ||
* @param object $object | ||
* @param string $documentRootHash | ||
* | ||
* @return string | ||
*/ | ||
public function getBlankNodeIdentifier($object, string $documentRootHash): string | ||
{ | ||
$objectHash = spl_object_hash($object); | ||
|
||
if (!isset($this->identifiers[$documentRootHash][$objectHash])) { | ||
$this->blankNodeCounts[$documentRootHash] ?? $this->blankNodeCounts[$documentRootHash] = 0; | ||
$this->identifiers[$documentRootHash] ?? $this->identifiers[$documentRootHash] = []; | ||
|
||
$this->identifiers[$documentRootHash][$objectHash] = sprintf('%s%d', self::IDENTIFIER_PREFIX, $this->blankNodeCounts[$documentRootHash]++); | ||
} | ||
|
||
return $this->identifiers[$documentRootHash][$objectHash]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
|
||
namespace ApiPlatform\Core\Serializer; | ||
|
||
use ApiPlatform\Core\Api\IdentifiersExtractor; | ||
use ApiPlatform\Core\Api\IdentifiersExtractorInterface; | ||
use ApiPlatform\Core\Api\IriConverterInterface; | ||
use ApiPlatform\Core\Api\OperationType; | ||
use ApiPlatform\Core\Api\ResourceClassResolverInterface; | ||
|
@@ -43,8 +45,9 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer | |
protected $iriConverter; | ||
protected $resourceClassResolver; | ||
protected $propertyAccessor; | ||
protected $identifiersExtractor; | ||
|
||
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null) | ||
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, PropertyAccessorInterface $propertyAccessor = null, NameConverterInterface $nameConverter = null, ClassMetadataFactoryInterface $classMetadataFactory = null, IdentifiersExtractorInterface $identifiersExtractor = null) | ||
{ | ||
parent::__construct($classMetadataFactory, $nameConverter); | ||
|
||
|
@@ -54,8 +57,15 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName | |
$this->resourceClassResolver = $resourceClassResolver; | ||
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); | ||
|
||
if (null === $identifiersExtractor) { | ||
@trigger_error('Not injecting IdentifiersExtractorInterface is deprecated since API Platform 2.1 and will not be possible anymore in API Platform 3'); | ||
$this->identifiersExtractor = new IdentifiersExtractor($this->propertyNameCollectionFactory, $this->propertyMetadataFactory, $this->propertyAccessor); | ||
} else { | ||
$this->identifiersExtractor = $identifiersExtractor; | ||
} | ||
|
||
$this->setCircularReferenceHandler(function ($object) { | ||
return $this->iriConverter->getIriFromItem($object); | ||
return $this->hasIri($object) ? $this->iriConverter->getIriFromItem($object) : spl_object_hash($object); | ||
}); | ||
} | ||
|
||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Is the condition still needed? if it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. |
||
$context['resources'][$resource] = $resource; | ||
} | ||
|
@@ -421,7 +431,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array | |
} | ||
|
||
/** | ||
* Normalizes a relation as an URI if is a Link or as a JSON-LD object. | ||
* Normalizes a relation. | ||
* | ||
* @param PropertyMetadata $propertyMetadata | ||
* @param mixed $relatedObject | ||
|
@@ -433,7 +443,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array | |
*/ | ||
private function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, string $format = null, array $context) | ||
{ | ||
if ($propertyMetadata->isReadableLink()) { | ||
if ($propertyMetadata->isReadableLink() || !$this->hasIri($relatedObject)) { | ||
return $this->serializer->normalize($relatedObject, $format, $this->createRelationSerializationContext($resourceClass, $context)); | ||
} | ||
|
||
|
@@ -444,4 +454,20 @@ private function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedO | |
|
||
return $iri; | ||
} | ||
|
||
/** | ||
* Determines whether an item has an IRI. | ||
* | ||
* @param object $object | ||
* | ||
* @return bool | ||
* | ||
* @internal | ||
*/ | ||
protected function hasIri($object): bool | ||
{ | ||
$identifiers = $this->identifiersExtractor->getIdentifiersFromItem($object); | ||
|
||
return (bool) array_filter($identifiers); | ||
} | ||
} |
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.