• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 19166809982

07 Nov 2025 11:19AM UTC coverage: 22.06% (+0.02%) from 22.042%
19166809982

Pull #7515

github

web-flow
Merge a7ee7f25b into 0edbc37fc
Pull Request #7515: fix(jsonld): read identifier with itemUriTemplate

8 of 11 new or added lines in 2 files covered. (72.73%)

4 existing lines in 1 file now uncovered.

11140 of 50498 relevant lines covered (22.06%)

23.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/JsonLd/Serializer/ItemNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\JsonLd\Serializer;
15

16
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
17
use ApiPlatform\JsonLd\ContextBuilderInterface;
18
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\IriConverterInterface;
21
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactory;
22
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
26
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
27
use ApiPlatform\Metadata\ResourceClassResolverInterface;
28
use ApiPlatform\Metadata\UrlGeneratorInterface;
29
use ApiPlatform\Metadata\Util\ClassInfoTrait;
30
use ApiPlatform\Serializer\AbstractItemNormalizer;
31
use ApiPlatform\Serializer\ContextTrait;
32
use ApiPlatform\Serializer\TagCollectorInterface;
33
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
34
use Symfony\Component\Serializer\Exception\LogicException;
35
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
36
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
37
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
38

39
/**
40
 * Converts between objects and array including JSON-LD and Hydra metadata.
41
 *
42
 * @author Kévin Dunglas <dunglas@gmail.com>
43
 */
44
final class ItemNormalizer extends AbstractItemNormalizer
45
{
46
    use ClassInfoTrait;
47
    use ContextTrait;
48
    use JsonLdContextTrait;
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
    ];
74

75
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null)
76
    {
77
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
606✔
78
    }
79

80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
84
    {
85
        return self::FORMAT === $format && parent::supportsNormalization($data, $format, $context);
390✔
86
    }
87

88
    public function getSupportedTypes($format): array
89
    {
90
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
508✔
91
    }
92

93
    /**
94
     * {@inheritdoc}
95
     *
96
     * @throws LogicException
97
     */
98
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
99
    {
100
        $resourceClass = $this->getObjectClass($object);
380✔
101

102
        if ($this->getOutputClass($context)) {
380✔
103
            return parent::normalize($object, $format, $context);
8✔
104
        }
105

106
        // TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
107
        $previousResourceClass = $context['resource_class'] ?? null;
380✔
108
        $metadata = [];
380✔
109
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
380✔
110
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
364✔
111
            $context = $this->initContext($resourceClass, $context);
364✔
112
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
364✔
113
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
20✔
114
            if ($context['api_collection_sub_level'] ?? false) {
20✔
115
                unset($context['api_collection_sub_level']);
8✔
116
                $context['output']['gen_id'] ??= true;
8✔
117
                $context['output']['iri'] = null;
8✔
118
            }
119

120
            if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
20✔
121
                $context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
2✔
122
            } else if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
18✔
123
                $context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
4✔
124
            }
125

126
            // We should improve what's behind the context creation, its probably more complicated then it should
127
            $metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
20✔
128
        }
129

130
        // Special case: non-resource got serialized and contains a resource therefore we need to reset part of the context
131
        if ($previousResourceClass !== $resourceClass) {
380✔
132
            unset($context['operation'], $context['operation_name'], $context['output']);
22✔
133
        }
134

135
        if (true === ($context['output']['gen_id'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
380✔
136
            $context['iri'] = $iri;
380✔
137
            $metadata['@id'] = $iri;
380✔
138
        }
139

140
        $context['api_normalize'] = true;
380✔
141

142
        $data = parent::normalize($object, $format, $context);
380✔
143
        if (!\is_array($data)) {
380✔
UNCOV
144
            return $data;
×
145
        }
146

147
        $operation = $context['operation'] ?? null;
380✔
148

149
        if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
380✔
150
            $operation = $this->operationMetadataFactory->create($context['item_uri_template']);
4✔
151
        }
152

153
        if ($isResourceClass && !$operation) {
380✔
154
            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
226✔
155
        }
156

157
        if (!isset($metadata['@type']) && $operation) {
380✔
158
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
364✔
159
            if (null === $types) {
364✔
160
                $types = [$operation->getShortName()];
364✔
161
            }
162
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
364✔
163
        }
164

165
        return $metadata + $data;
380✔
166
    }
167

168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
172
    {
173
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
2✔
174
    }
175

176
    /**
177
     * {@inheritdoc}
178
     *
179
     * @throws NotNormalizableValueException
180
     */
181
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
182
    {
183
        // Avoid issues with proxies if we populated the object
184
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
2✔
185
            if (true !== ($context['api_allow_update'] ?? true)) {
×
UNCOV
186
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
187
            }
188

189
            try {
190
                $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null);
×
191
            } catch (ItemNotFoundException $e) {
×
UNCOV
192
                $operation = $context['operation'] ?? null;
×
193

194
                if (!('PUT' === $operation?->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true))) {
×
UNCOV
195
                    throw $e;
×
196
                }
197
            }
198
        }
199

200
        return parent::denormalize($data, $class, $format, $context);
2✔
201
    }
202

203
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
204
    {
205
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
380✔
206
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
380✔
207
            $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS);
2✔
208
        }
209

210
        return $allowedAttributes;
380✔
211
    }
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc