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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

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

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

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

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

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

86
    public function getSupportedTypes($format): array
87
    {
UNCOV
88
        return self::FORMAT === $format ? parent::getSupportedTypes($format) : [];
861✔
89
    }
90

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

UNCOV
100
        if ($this->getOutputClass($context)) {
529✔
UNCOV
101
            return parent::normalize($object, $format, $context);
12✔
102
        }
103

104
        // 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
105
        $previousResourceClass = $context['resource_class'] ?? null;
529✔
UNCOV
106
        $metadata = [];
529✔
UNCOV
107
        if ($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass))) {
529✔
UNCOV
108
            $resourceClass = $this->resourceClassResolver->getResourceClass($object, $previousResourceClass);
513✔
UNCOV
109
            $context = $this->initContext($resourceClass, $context);
513✔
UNCOV
110
            $metadata = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
513✔
UNCOV
111
        } elseif ($this->contextBuilder instanceof AnonymousContextBuilderInterface) {
18✔
UNCOV
112
            if ($context['api_collection_sub_level'] ?? false) {
18✔
UNCOV
113
                unset($context['api_collection_sub_level']);
9✔
UNCOV
114
                $context['output']['genid'] = true;
9✔
UNCOV
115
                $context['output']['iri'] = null;
9✔
116
            }
117

118
            // We should improve what's behind the context creation, its probably more complicated then it should
UNCOV
119
            $metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
18✔
120
        }
121

122
        // maybe not needed anymore
UNCOV
123
        if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
529✔
124
            unset($context['operation'], $context['operation_name']);
4✔
125
        }
126

UNCOV
127
        if (true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
529✔
UNCOV
128
            $context['iri'] = $iri;
529✔
UNCOV
129
            $metadata['@id'] = $iri;
529✔
130
        }
131

UNCOV
132
        $context['api_normalize'] = true;
529✔
133

UNCOV
134
        $data = parent::normalize($object, $format, $context);
529✔
UNCOV
135
        if (!\is_array($data)) {
529✔
136
            return $data;
2✔
137
        }
138

UNCOV
139
        if (!isset($metadata['@type']) && $isResourceClass) {
529✔
UNCOV
140
            $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
513✔
141

UNCOV
142
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
513✔
UNCOV
143
            if (null === $types) {
513✔
UNCOV
144
                $types = [$operation->getShortName()];
512✔
145
            }
UNCOV
146
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
513✔
147
        }
148

UNCOV
149
        return $metadata + $data;
529✔
150
    }
151

152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
156
    {
157
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format, $context);
114✔
158
    }
159

160
    /**
161
     * {@inheritdoc}
162
     *
163
     * @throws NotNormalizableValueException
164
     */
165
    public function denormalize(mixed $data, string $class, ?string $format = null, array $context = []): mixed
166
    {
167
        // Avoid issues with proxies if we populated the object
168
        if (isset($data['@id']) && !isset($context[self::OBJECT_TO_POPULATE])) {
114✔
169
            if (true !== ($context['api_allow_update'] ?? true)) {
8✔
170
                throw new NotNormalizableValueException('Update is not allowed for this operation.');
×
171
            }
172

173
            try {
174
                $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null);
8✔
175
            } catch (ItemNotFoundException $e) {
4✔
176
                $operation = $context['operation'] ?? null;
2✔
177

178
                if (!('PUT' === $operation?->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true))) {
2✔
179
                    throw $e;
1✔
180
                }
181
            }
182
        }
183

184
        return parent::denormalize($data, $class, $format, $context);
112✔
185
    }
186

187
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
188
    {
UNCOV
189
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
530✔
UNCOV
190
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
530✔
191
            $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS);
107✔
192
        }
193

UNCOV
194
        return $allowedAttributes;
530✔
195
    }
196
}
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

© 2025 Coveralls, Inc