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

api-platform / core / 20847864477

09 Jan 2026 09:47AM UTC coverage: 29.1% (+0.005%) from 29.095%
20847864477

Pull #7649

github

web-flow
Merge b342dd5db into d640d106b
Pull Request #7649: feat(validator): uuid/ulid parameter validation

0 of 4 new or added lines in 1 file covered. (0.0%)

15050 existing lines in 491 files now uncovered.

16996 of 58406 relevant lines covered (29.1%)

81.8 hits per line

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

98.25
/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\OperationMetadataFactoryInterface;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
25
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
26
use ApiPlatform\Metadata\ResourceClassResolverInterface;
27
use ApiPlatform\Metadata\UrlGeneratorInterface;
28
use ApiPlatform\Metadata\Util\ClassInfoTrait;
29
use ApiPlatform\Serializer\AbstractItemNormalizer;
30
use ApiPlatform\Serializer\ContextTrait;
31
use ApiPlatform\Serializer\TagCollectorInterface;
32
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
33
use Symfony\Component\Serializer\Exception\LogicException;
34
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
35
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
36
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
37

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

49
    public const FORMAT = 'jsonld';
50
    private const JSONLD_KEYWORDS = [
51
        '@context',
52
        '@direction',
53
        '@graph',
54
        '@id',
55
        '@import',
56
        '@included',
57
        '@index',
58
        '@json',
59
        '@language',
60
        '@list',
61
        '@nest',
62
        '@none',
63
        '@prefix',
64
        '@propagate',
65
        '@protected',
66
        '@reverse',
67
        '@set',
68
        '@type',
69
        '@value',
70
        '@version',
71
        '@vocab',
72
    ];
73

74
    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)
75
    {
UNCOV
76
        parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
2,411✔
77
    }
78

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

87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getSupportedTypes(?string $format): array
91
    {
UNCOV
92
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
2,116✔
93
    }
94

95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @throws LogicException
99
     */
100
    public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
101
    {
UNCOV
102
        $resourceClass = $this->getObjectClass($data);
1,381✔
UNCOV
103
        $outputClass = $this->getOutputClass($context);
1,381✔
104

UNCOV
105
        if ($outputClass && !($context['item_uri_template'] ?? null)) {
1,381✔
UNCOV
106
            return parent::normalize($data, $format, $context);
23✔
107
        }
108

109
        // 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
UNCOV
110
        $previousResourceClass = $context['resource_class'] ?? null;
1,381✔
UNCOV
111
        $metadata = [];
1,381✔
UNCOV
112
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
1,381✔
UNCOV
113
            $resourceClass = $this->resourceClassResolver->getResourceClass($data, $previousResourceClass);
1,342✔
UNCOV
114
            $context = $this->initContext($resourceClass, $context);
1,342✔
UNCOV
115
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
1,342✔
UNCOV
116
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
49✔
UNCOV
117
            if ($context['api_collection_sub_level'] ?? false) {
49✔
UNCOV
118
                unset($context['api_collection_sub_level']);
24✔
UNCOV
119
                $context['output']['gen_id'] ??= true;
24✔
UNCOV
120
                $context['output']['iri'] = null;
24✔
121
            }
122

UNCOV
123
            if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
49✔
UNCOV
124
                $context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
6✔
UNCOV
125
            } elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
45✔
UNCOV
126
                $context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
7✔
127
            }
128

129
            // We should improve what's behind the context creation, its probably more complicated then it should
UNCOV
130
            $metadata = $this->createJsonLdContext($this->contextBuilder, $data, $context);
49✔
131
        }
132

133
        // Special case: non-resource got serialized and contains a resource therefore we need to reset part of the context
UNCOV
134
        if ($previousResourceClass !== $resourceClass && $resourceClass !== $outputClass) {
1,381✔
UNCOV
135
            unset($context['operation'], $context['operation_name'], $context['output']);
64✔
136
        }
137

UNCOV
138
        if (true === ($context['output']['gen_id'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($data, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
1,381✔
UNCOV
139
            $context['iri'] = $iri;
1,381✔
UNCOV
140
            $metadata['@id'] = $iri;
1,381✔
141
        }
142

UNCOV
143
        $context['api_normalize'] = true;
1,381✔
144

UNCOV
145
        $normalizedData = parent::normalize($data, $format, $context);
1,381✔
UNCOV
146
        if (!\is_array($normalizedData)) {
1,381✔
147
            return $normalizedData;
4✔
148
        }
149

UNCOV
150
        $operation = $context['operation'] ?? null;
1,381✔
151

UNCOV
152
        if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
1,381✔
UNCOV
153
            $operation = $this->operationMetadataFactory->create($context['item_uri_template']);
13✔
154
        }
155

UNCOV
156
        if ($isResourceClass && !$operation) {
1,381✔
UNCOV
157
            $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
707✔
158
        }
159

UNCOV
160
        if (!isset($metadata['@type']) && $operation) {
1,381✔
UNCOV
161
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
1,342✔
UNCOV
162
            if (null === $types) {
1,342✔
UNCOV
163
                $types = [$operation->getShortName()];
1,340✔
164
            }
UNCOV
165
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
1,342✔
166
        }
167

UNCOV
168
        return $metadata + $normalizedData;
1,381✔
169
    }
170

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

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

192
            try {
UNCOV
193
                $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null);
19✔
UNCOV
194
            } catch (ItemNotFoundException $e) {
10✔
UNCOV
195
                $operation = $context['operation'] ?? null;
6✔
196

UNCOV
197
                if (!('PUT' === $operation?->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true))) {
6✔
198
                    throw $e;
2✔
199
                }
200
            }
201
        }
202

UNCOV
203
        return parent::denormalize($data, $type, $format, $context);
283✔
204
    }
205

206
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
207
    {
UNCOV
208
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
1,385✔
UNCOV
209
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
1,385✔
UNCOV
210
            $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS);
267✔
211
        }
212

UNCOV
213
        return $allowedAttributes;
1,385✔
214
    }
215
}
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