• 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

98.75
/src/JsonLd/ContextBuilder.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;
15

16
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
24
use ApiPlatform\Metadata\UrlGeneratorInterface;
25
use ApiPlatform\Metadata\Util\ClassInfoTrait;
26
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ContextBuilder implements AnonymousContextBuilderInterface
34
{
35
    use ClassInfoTrait;
36
    use HydraPrefixTrait;
37

38
    public const FORMAT = 'jsonld';
39
    public const HYDRA_PREFIX = 'hydra:';
40
    public const HYDRA_CONTEXT_HAS_PREFIX = 'hydra_prefix';
41

42
    public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly UrlGeneratorInterface $urlGenerator, private readonly ?IriConverterInterface $iriConverter = null, private readonly ?NameConverterInterface $nameConverter = null, private array $defaultContext = [])
43
    {
UNCOV
44
    }
960✔
45

46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getBaseContext(int $referenceType = UrlGeneratorInterface::ABS_URL): array
50
    {
UNCOV
51
        return [
54✔
UNCOV
52
            '@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
54✔
UNCOV
53
            'hydra' => self::HYDRA_NS,
54✔
UNCOV
54
        ];
54✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getEntrypointContext(int $referenceType = UrlGeneratorInterface::ABS_PATH): array
61
    {
UNCOV
62
        $context = $this->getBaseContext($referenceType);
2✔
63

UNCOV
64
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
2✔
UNCOV
65
            $shortName = $this->resourceMetadataFactory->create($resourceClass)[0]->getShortName();
2✔
UNCOV
66
            $resourceName = lcfirst($shortName);
2✔
67

UNCOV
68
            $context[$resourceName] = [
2✔
UNCOV
69
                '@id' => 'Entrypoint/'.$resourceName,
2✔
UNCOV
70
                '@type' => '@id',
2✔
UNCOV
71
            ];
2✔
72
        }
73

UNCOV
74
        return $context;
2✔
75
    }
76

77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getResourceContext(string $resourceClass, int $referenceType = UrlGeneratorInterface::ABS_PATH): array
81
    {
82
        /** @var HttpOperation $operation */
UNCOV
83
        $operation = $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
13✔
UNCOV
84
        if (null === $shortName = $operation->getShortName()) {
13✔
85
            return [];
×
86
        }
87

UNCOV
88
        $context = $operation->getNormalizationContext();
13✔
UNCOV
89
        if ($context['iri_only'] ?? false) {
13✔
UNCOV
90
            $context = $this->getBaseContext($referenceType);
4✔
UNCOV
91
            $context[$this->getHydraPrefix($context).'member']['@type'] = '@id';
4✔
92

UNCOV
93
            return $context;
4✔
94
        }
95

UNCOV
96
        return $this->getResourceContextWithShortname($resourceClass, $referenceType, $shortName, $operation);
9✔
97
    }
98

99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getResourceContextUri(string $resourceClass, ?int $referenceType = null): string
103
    {
UNCOV
104
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)[0];
542✔
UNCOV
105
        if (null === $referenceType) {
542✔
UNCOV
106
            $referenceType = $resourceMetadata->getUrlGenerationStrategy();
542✔
107
        }
108

UNCOV
109
        return $this->urlGenerator->generate('api_jsonld_context', ['shortName' => $resourceMetadata->getShortName()], $referenceType ?? UrlGeneratorInterface::ABS_PATH);
542✔
110
    }
111

112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getAnonymousResourceContext(object $object, array $context = [], int $referenceType = UrlGeneratorInterface::ABS_PATH): array
116
    {
UNCOV
117
        $outputClass = $this->getObjectClass($object);
39✔
UNCOV
118
        $operation = $context['operation'] ?? new Get(
39✔
UNCOV
119
            shortName: (new \ReflectionClass($outputClass))->getShortName(),
39✔
UNCOV
120
            normalizationContext: [
39✔
UNCOV
121
                self::HYDRA_CONTEXT_HAS_PREFIX => $context[self::HYDRA_CONTEXT_HAS_PREFIX] ?? $this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true,
39✔
UNCOV
122
                'groups' => [],
39✔
UNCOV
123
            ],
39✔
UNCOV
124
            denormalizationContext: [
39✔
UNCOV
125
                'groups' => [],
39✔
UNCOV
126
            ]
39✔
UNCOV
127
        );
39✔
UNCOV
128
        $shortName = $operation->getShortName();
39✔
129

UNCOV
130
        $jsonLdContext = [
39✔
UNCOV
131
            '@context' => $this->getResourceContextWithShortname(
39✔
UNCOV
132
                $outputClass,
39✔
UNCOV
133
                $referenceType,
39✔
UNCOV
134
                $shortName,
39✔
UNCOV
135
                $operation
39✔
UNCOV
136
            ),
39✔
UNCOV
137
            '@type' => $shortName,
39✔
UNCOV
138
        ];
39✔
139

UNCOV
140
        if (isset($context['iri'])) {
39✔
UNCOV
141
            $jsonLdContext['@id'] = $context['iri'];
3✔
UNCOV
142
        } elseif (true === ($context['gen_id'] ?? true) && $this->iriConverter) {
36✔
UNCOV
143
            $jsonLdContext['@id'] = $this->iriConverter->getIriFromResource($object);
35✔
144
        }
145

UNCOV
146
        if ($context['has_context'] ?? false) {
39✔
UNCOV
147
            unset($jsonLdContext['@context']);
29✔
148
        }
149

150
        // here the object can be different from the resource given by the $context['api_resource'] value
UNCOV
151
        if (isset($context['api_resource'])) {
39✔
UNCOV
152
            $jsonLdContext['@type'] = $this->resourceMetadataFactory->create($this->getObjectClass($context['api_resource']))[0]->getShortName();
2✔
153
        }
154

UNCOV
155
        return $jsonLdContext;
39✔
156
    }
157

158
    private function getResourceContextWithShortname(string $resourceClass, int $referenceType, string $shortName, ?HttpOperation $operation = null): array
159
    {
UNCOV
160
        $context = $this->getBaseContext($referenceType);
48✔
UNCOV
161
        $propertyContext = $operation ? ['normalization_groups' => $operation->getNormalizationContext()['groups'] ?? null, 'denormalization_groups' => $operation->getDenormalizationContext()['groups'] ?? null] : ['normalization_groups' => [], 'denormalization_groups' => []];
48✔
162

UNCOV
163
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
48✔
UNCOV
164
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName, $propertyContext);
48✔
165

UNCOV
166
            if ($propertyMetadata->isIdentifier() && true !== $propertyMetadata->isWritable()) {
48✔
UNCOV
167
                continue;
11✔
168
            }
169

UNCOV
170
            $convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT) : $propertyName;
48✔
UNCOV
171
            $jsonldContext = $propertyMetadata->getJsonldContext() ?? [];
48✔
172

UNCOV
173
            if ($id = $propertyMetadata->getIris()) {
48✔
174
                $id = 1 === (is_countable($id) ? \count($id) : 0) ? $id[0] : $id;
1✔
175
            }
176

UNCOV
177
            if (!$id) {
48✔
UNCOV
178
                $id = \sprintf('%s/%s', $shortName, $convertedName);
48✔
179
            }
180

UNCOV
181
            if (false === $propertyMetadata->isReadableLink()) {
48✔
182
                $jsonldContext += [
1✔
183
                    '@id' => $id,
1✔
184
                    '@type' => '@id',
1✔
185
                ];
1✔
186
            }
187

UNCOV
188
            if (empty($jsonldContext)) {
48✔
UNCOV
189
                $context[$convertedName] = $id;
45✔
190
            } else {
UNCOV
191
                $context[$convertedName] = $jsonldContext + [
4✔
UNCOV
192
                    '@id' => $id,
4✔
UNCOV
193
                ];
4✔
194
            }
195
        }
196

UNCOV
197
        return $context;
48✔
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