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

api-platform / core / 9869113483

10 Jul 2024 06:12AM UTC coverage: 63.421%. Remained the same
9869113483

push

github

soyuka
fix(jsonld): use http method instead of operation class

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

1 existing line in 1 file now uncovered.

11178 of 17625 relevant lines covered (63.42%)

53.23 hits per line

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

66.67
/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\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
19
use ApiPlatform\JsonLd\ContextBuilderInterface;
20
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
21
use ApiPlatform\Metadata\HttpOperation;
22
use ApiPlatform\Metadata\IriConverterInterface;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use ApiPlatform\Metadata\Put;
26
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
27
use ApiPlatform\Metadata\ResourceAccessCheckerInterface;
28
use ApiPlatform\Metadata\ResourceClassResolverInterface;
29
use ApiPlatform\Metadata\UrlGeneratorInterface;
30
use ApiPlatform\Metadata\Util\ClassInfoTrait;
31
use ApiPlatform\Serializer\AbstractItemNormalizer;
32
use ApiPlatform\Serializer\ContextTrait;
33
use ApiPlatform\Serializer\TagCollectorInterface;
34
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
35
use Symfony\Component\Serializer\Exception\LogicException;
36
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
37
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
38
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
39

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

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

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

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

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

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

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

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

121
            // We should improve what's behind the context creation, its probably more complicated then it should
122
            $metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
8✔
123
        }
124

125
        // maybe not needed anymore
126
        if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
120✔
127
            unset($context['operation'], $context['operation_name']);
×
128
        }
129

130
        if (true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
120✔
131
            $context['iri'] = $iri;
120✔
132
            $metadata['@id'] = $iri;
120✔
133
        }
134

135
        $context['api_normalize'] = true;
120✔
136

137
        $data = parent::normalize($object, $format, $context);
120✔
138
        if (!\is_array($data)) {
120✔
139
            return $data;
×
140
        }
141

142
        if (!isset($metadata['@type']) && $isResourceClass) {
120✔
143
            $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
112✔
144

145
            $types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
112✔
146
            if (null === $types) {
112✔
147
                $types = [$operation->getShortName()];
108✔
148
            }
149
            $metadata['@type'] = 1 === \count($types) ? $types[0] : $types;
112✔
150
        }
151

152
        return $metadata + $data;
120✔
153
    }
154

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

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

176
            try {
177
                $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri($data['@id'], $context + ['fetch_data' => true], $context['operation'] ?? null);
×
178
            } catch (ItemNotFoundException $e) {
×
179
                $operation = $context['operation'] ?? null;
×
180

NEW
181
                if (!($operation?->getMethod() === 'PUT' && ($operation?->getExtraProperties()['standard_put'] ?? false))) {
×
UNCOV
182
                    throw $e;
×
183
                }
184
            }
185
        }
186

187
        return parent::denormalize($data, $class, $format, $context);
×
188
    }
189

190
    protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
191
    {
192
        $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
120✔
193
        if (\is_array($allowedAttributes) && ($context['api_denormalize'] ?? false)) {
120✔
194
            $allowedAttributes = array_merge($allowedAttributes, self::JSONLD_KEYWORDS);
×
195
        }
196

197
        return $allowedAttributes;
120✔
198
    }
199
}
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