|
17 | 17 | use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
|
18 | 18 | use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
|
19 | 19 | use ApiPlatform\JsonLd\ContextBuilderInterface;
|
| 20 | +use ApiPlatform\Metadata\Exception\ItemNotFoundException; |
20 | 21 | use ApiPlatform\Metadata\HttpOperation;
|
21 | 22 | use ApiPlatform\Metadata\IriConverterInterface;
|
22 | 23 | use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
|
@@ -47,6 +48,29 @@ final class ItemNormalizer extends AbstractItemNormalizer
|
47 | 48 | use JsonLdContextTrait;
|
48 | 49 |
|
49 | 50 | public const FORMAT = 'jsonld';
|
| 51 | + private const JSONLD_KEYWORDS = [ |
| 52 | + '@context', |
| 53 | + '@direction', |
| 54 | + '@graph', |
| 55 | + '@id', |
| 56 | + '@import', |
| 57 | + '@included', |
| 58 | + '@index', |
| 59 | + '@json', |
| 60 | + '@language', |
| 61 | + '@list', |
| 62 | + '@nest', |
| 63 | + '@none', |
| 64 | + '@prefix', |
| 65 | + '@propagate', |
| 66 | + '@protected', |
| 67 | + '@reverse', |
| 68 | + '@set', |
| 69 | + '@type', |
| 70 | + '@value', |
| 71 | + '@version', |
| 72 | + '@vocab', |
| 73 | + ]; |
50 | 74 |
|
51 | 75 | public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface|LegacyIriConverterInterface $iriConverter, ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
|
52 | 76 | {
|
@@ -148,9 +172,27 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
|
148 | 172 | throw new NotNormalizableValueException('Update is not allowed for this operation.');
|
149 | 173 | }
|
150 | 174 |
|
151 |
| - $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true]); |
| 175 | + try { |
| 176 | + $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null); |
| 177 | + } catch (ItemNotFoundException $e) { |
| 178 | + $operation = $context['operation'] ?? null; |
| 179 | + |
| 180 | + if (!('PUT' === $operation?->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? false))) { |
| 181 | + throw $e; |
| 182 | + } |
| 183 | + } |
152 | 184 | }
|
153 | 185 |
|
154 | 186 | return parent::denormalize($data, $class, $format, $context);
|
155 | 187 | }
|
| 188 | + |
| 189 | + protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool |
| 190 | + { |
| 191 | + $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString); |
| 192 | + if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) { |
| 193 | + $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS); |
| 194 | + } |
| 195 | + |
| 196 | + return $allowedAttributes; |
| 197 | + } |
156 | 198 | }
|
0 commit comments