-
-
Notifications
You must be signed in to change notification settings - Fork 966
Allow plain IDs with allow_plain_identifiers
#1365
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use ApiPlatform\Core\Api\IriConverterInterface; | ||
| use ApiPlatform\Core\Api\OperationType; | ||
| use ApiPlatform\Core\Api\ResourceClassResolverInterface; | ||
| use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; | ||
| use ApiPlatform\Core\Exception\InvalidArgumentException; | ||
| use ApiPlatform\Core\Exception\ItemNotFoundException; | ||
| use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
|
|
@@ -44,8 +45,10 @@ abstract class AbstractItemNormalizer extends AbstractObjectNormalizer | |
| protected $resourceClassResolver; | ||
| protected $propertyAccessor; | ||
| protected $localCache = []; | ||
| protected $itemDataProvider; | ||
| protected $allowPlainIdentifiers; | ||
|
|
||
| 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, ItemDataProviderInterface $itemDataProvider = null, bool $allowPlainIdentifiers = false) | ||
| { | ||
| parent::__construct($classMetadataFactory, $nameConverter); | ||
|
|
||
|
|
@@ -54,6 +57,8 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName | |
| $this->iriConverter = $iriConverter; | ||
| $this->resourceClassResolver = $resourceClassResolver; | ||
| $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); | ||
| $this->itemDataProvider = $itemDataProvider; | ||
| $this->allowPlainIdentifiers = $allowPlainIdentifiers; | ||
|
|
||
| $this->setCircularReferenceHandler(function ($object) { | ||
| return $this->iriConverter->getIriFromItem($object); | ||
|
|
@@ -299,6 +304,17 @@ private function denormalizeRelation(string $attributeName, PropertyMetadata $pr | |
| } | ||
|
|
||
| if (!is_array($value)) { | ||
| // repeat the code so that IRIs keep working with the json format | ||
| if (true === $this->allowPlainIdentifiers && $this->itemDataProvider) { | ||
|
Contributor
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. What is
Member
Author
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. We could, maybe a bit overkill, this dependency should be a hard dep, it's just nullable to avoid breaking. |
||
| try { | ||
| return $this->itemDataProvider->getItem($className, $value, null, $context + ['fetch_data' => true]); | ||
| } catch (ItemNotFoundException $e) { | ||
| throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); | ||
| } catch (InvalidArgumentException $e) { | ||
| // Give a chance to other normalizers (e.g.: DateTimeNormalizer) | ||
| } | ||
| } | ||
|
|
||
| throw new InvalidArgumentException(sprintf( | ||
| 'Expected IRI or nested document for attribute "%s", "%s" given.', $attributeName, gettype($value) | ||
| )); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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'd like to test that
Content-Typeisapplication/jsonhere, not sure if it's possible or wanted...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 just check the
$formatvar.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 really,
jsonis the format forhal,jsonldandjsonno?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, it's
jsonldfor JSON-LD andjsonhalfor HAL.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.
Okay, my bad dunno why I thought so...