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

api-platform / core / 20744457479

06 Jan 2026 09:46AM UTC coverage: 28.884% (+0.04%) from 28.843%
20744457479

Pull #7647

github

web-flow
Merge 37bd89e4e into d23ab4301
Pull Request #7647: fix(doctrine): fix partial fetch with same entity included multiple time with different fields

31 of 116 new or added lines in 2 files covered. (26.72%)

378 existing lines in 45 files now uncovered.

16832 of 58274 relevant lines covered (28.88%)

78.61 hits per line

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

94.69
/src/Metadata/Resource/Factory/UriTemplateResourceMetadataCollectionFactory.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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Link;
20
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface;
21
use ApiPlatform\Metadata\Operations;
22
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
23
use Symfony\Component\Routing\Route;
24

25
/**
26
 * @author Antoine Bluchet <soyuka@gmail.com>
27
 */
28
final class UriTemplateResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
29
{
30
    use OperationDefaultsTrait;
31

32
    public function __construct(private readonly LinkFactoryInterface $linkFactory, private readonly PathSegmentNameGeneratorInterface $pathSegmentNameGenerator, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null)
33
    {
34
    }
2,378✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function create(string $resourceClass): ResourceMetadataCollection
40
    {
41
        $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
201✔
42
        if ($this->decorated) {
201✔
43
            $resourceMetadataCollection = $this->decorated->create($resourceClass);
201✔
44
        }
45

46
        foreach ($resourceMetadataCollection as $i => $resource) {
201✔
47
            /** @var ApiResource $resource */
48
            $resource = $this->configureUriVariables($resource);
193✔
49
            if ($resource->getUriTemplate()) {
193✔
50
                $resourceMetadataCollection[$i] = $resource->withExtraProperties($resource->getExtraProperties() + ['user_defined_uri_template' => true]);
44✔
51
            }
52

53
            $operations = new Operations();
193✔
54
            foreach ($resource->getOperations() ?? new Operations() as $key => $operation) {
193✔
55
                /** @var HttpOperation */
56
                $operation = $this->configureUriVariables($operation);
193✔
57

58
                if (
59
                    $operation->getUriTemplate()
193✔
60
                    && !($operation->getExtraProperties()['generated_operation'] ?? false)
193✔
61
                ) {
62
                    $operation = $operation->withExtraProperties($operation->getExtraProperties() + ['user_defined_uri_template' => true]);
104✔
63
                    if (!$operation->getName()) {
104✔
64
                        $operation = $operation->withName($key);
88✔
65
                    }
66

67
                    $operations->add($key, $operation);
104✔
68
                    continue;
104✔
69
                }
70

71
                if ($routeName = $operation->getRouteName()) {
165✔
72
                    if (!$operation->getName()) {
4✔
73
                        $operation = $operation->withName($routeName);
×
74
                    }
75

76
                    $operations->add($operation->getName(), $operation);
4✔
77
                    continue;
4✔
78
                }
79

80
                $operation = $operation->withUriTemplate($this->generateUriTemplate($operation));
165✔
81

82
                if (!$operation->getName()) {
165✔
83
                    $operation = $operation->withName($this->getDefaultOperationName($operation, $resourceClass));
165✔
84
                }
85

86
                $operations->add($operation->getName(), $operation);
165✔
87
            }
88

89
            $resource = $resource->withOperations($operations);
193✔
90
            $resourceMetadataCollection[$i] = $resource;
193✔
91
        }
92

93
        return $resourceMetadataCollection;
201✔
94
    }
95

96
    private function generateUriTemplate(HttpOperation $operation): string
97
    {
98
        $uriTemplate = $operation->getUriTemplate() ?? \sprintf('/%s', $this->pathSegmentNameGenerator->getSegmentName($operation->getShortName()));
165✔
99
        $uriVariables = $operation->getUriVariables() ?? [];
165✔
100

101
        if (str_ends_with($uriTemplate, '{._format}')) {
165✔
102
            $uriTemplate = substr($uriTemplate, 0, -10);
×
103
        }
104

105
        if ($parameters = array_keys($uriVariables)) {
165✔
106
            foreach ($parameters as $parameterName) {
159✔
107
                $part = \sprintf('/{%s}', $parameterName);
159✔
108
                if (!str_contains($uriTemplate, $part)) {
159✔
109
                    $uriTemplate .= \sprintf('/{%s}', $parameterName);
139✔
110
                }
111
            }
112
        }
113

114
        return \sprintf('%s%s', $uriTemplate, '{._format}');
165✔
115
    }
116

117
    private function configureUriVariables(ApiResource|HttpOperation $operation): ApiResource|HttpOperation
118
    {
119
        // We will generate the collection route, don't initialize variables here
120
        if ($operation instanceof HttpOperation && (
193✔
121
            [] === $operation->getUriVariables()
193✔
122
            || (
193✔
123
                $operation instanceof CollectionOperationInterface
193✔
124
                && null === $operation->getUriTemplate()
193✔
125
            )
193✔
126
        )) {
127
            if (null === $operation->getUriVariables()) {
122✔
128
                return $operation;
122✔
129
            }
130

UNCOV
131
            return $this->normalizeUriVariables($operation);
2✔
132
        }
133

134
        $hasUserConfiguredUriVariables = !($operation->getExtraProperties()['is_legacy_resource_metadata'] ?? false);
193✔
135
        if (!$operation->getUriVariables()) {
193✔
136
            $hasUserConfiguredUriVariables = false;
181✔
137
            $operation = $operation->withUriVariables($this->transformLinksToUriVariables($this->linkFactory->createLinksFromIdentifiers($operation)));
181✔
138
        }
139

140
        $operation = $this->normalizeUriVariables($operation);
193✔
141

142
        if (!($uriTemplate = $operation->getUriTemplate())) {
193✔
143
            if ($operation instanceof HttpOperation && 'POST' === $operation->getMethod()) {
181✔
144
                return $operation->withUriVariables([]);
84✔
145
            }
146

147
            return $operation;
181✔
148
        }
149

150
        foreach ($uriVariables = $operation->getUriVariables() as $parameterName => $l) {
110✔
151
            $link = null === $l->getFromClass() ? $l->withFromClass($operation->getClass()) : $l;
88✔
152
            $uriVariables[$parameterName] = $this->linkFactory->completeLink($link);
88✔
153
        }
154
        $operation = $operation->withUriVariables($uriVariables);
110✔
155

156
        if (str_ends_with($uriTemplate, '{._format}') || str_ends_with($uriTemplate, '.{_format}')) {
110✔
157
            $uriTemplate = substr($uriTemplate, 0, -10);
52✔
158
        }
159

160
        // TODO: move this to the Symfony bridge
161
        if (class_exists(Route::class)) {
110✔
162
            $route = (new Route($uriTemplate))->compile();
110✔
163
            $variables = $route->getPathVariables();
110✔
164

165
            if (\count($variables) !== \count($uriVariables)) {
110✔
166
                if ($hasUserConfiguredUriVariables) {
76✔
167
                    return $operation;
18✔
168
                }
169

170
                $newUriVariables = [];
64✔
171
                foreach ($variables as $variable) {
64✔
172
                    if (isset($uriVariables[$variable])) {
29✔
173
                        $newUriVariables[$variable] = $uriVariables[$variable];
1✔
174
                        continue;
1✔
175
                    }
176

177
                    $newUriVariables[$variable] = (new Link())
29✔
178
                        ->withFromClass($operation->getClass())
29✔
179
                        ->withIdentifiers([property_exists($operation->getClass(), $variable) ? $variable : 'id'])
29✔
180
                        ->withParameterName($variable);
29✔
181
                }
182

183
                return $operation->withUriVariables($newUriVariables);
64✔
184
            }
185

186
            // When an operation is generated we need to find properties matching it's uri variables
187
            if (!($operation->getExtraProperties()['generated_operation'] ?? false) || !$this->linkFactory instanceof PropertyLinkFactoryInterface) {
86✔
188
                return $operation;
86✔
189
            }
190

191
            $diff = array_diff($variables, array_keys($uriVariables));
4✔
192
            if (0 === \count($diff)) {
4✔
193
                return $operation;
4✔
194
            }
195

196
            // We generated this operation but there're some missing identifiers
197
            $uriVariables = 'POST' === $operation->getMethod() || $operation instanceof CollectionOperationInterface ? [] : $operation->getUriVariables();
1✔
198

199
            foreach ($diff as $key) {
1✔
200
                $uriVariables[$key] = $this->linkFactory->createLinkFromProperty($operation, $key);
1✔
201
            }
202

203
            return $operation->withUriVariables($uriVariables);
1✔
204
        }
205

206
        return $operation;
×
207
    }
208

209
    private function normalizeUriVariables(ApiResource|HttpOperation $operation): ApiResource|HttpOperation
210
    {
211
        $uriVariables = (array) ($operation->getUriVariables() ?? []);
193✔
212

213
        $normalizedUriVariables = [];
193✔
214
        $resourceClass = $operation->getClass();
193✔
215

216
        foreach ($uriVariables as $parameterName => $uriVariable) {
193✔
217
            $normalizedParameterName = $parameterName;
171✔
218
            $normalizedUriVariable = $uriVariable;
171✔
219

220
            if (\is_int($normalizedParameterName)) {
171✔
221
                $normalizedParameterName = $normalizedUriVariable;
33✔
222
            }
223
            if (\is_string($normalizedUriVariable)) {
171✔
224
                $normalizedUriVariable = (new Link())->withIdentifiers([$normalizedUriVariable])->withFromClass($resourceClass);
33✔
225
            }
226
            if (\is_array($normalizedUriVariable)) {
171✔
227
                if (!isset($normalizedUriVariable['from_class']) && !isset($normalizedUriVariable['expanded_value'])) {
5✔
228
                    if (2 !== \count($normalizedUriVariable)) {
×
229
                        throw new \LogicException("The uriVariables shortcut syntax needs to be the tuple: 'uriVariable' => [fromClass, fromProperty]");
×
230
                    }
231
                    $normalizedUriVariable = (new Link())->withFromProperty($normalizedUriVariable[1])->withFromClass($normalizedUriVariable[0]);
×
232
                } else {
233
                    $normalizedUriVariable = new Link($normalizedParameterName, $normalizedUriVariable['from_property'] ?? null, $normalizedUriVariable['to_property'] ?? null, $normalizedUriVariable['from_class'] ?? null, $normalizedUriVariable['to_class'] ?? null, $normalizedUriVariable['identifiers'] ?? null, $normalizedUriVariable['composite_identifier'] ?? null, $normalizedUriVariable['expanded_value'] ?? null, $normalizedUriVariable['security'] ?? null);
5✔
234
                }
235
            }
236

237
            $normalizedUriVariable = $normalizedUriVariable->withParameterName($normalizedParameterName);
171✔
238
            $normalizedUriVariables[$normalizedParameterName] = $normalizedUriVariable;
171✔
239
        }
240

241
        return $operation->withUriVariables($normalizedUriVariables);
193✔
242
    }
243

244
    /**
245
     * @param Link[] $links
246
     *
247
     * @return array<string, Link>
248
     */
249
    private function transformLinksToUriVariables(array $links): array
250
    {
251
        $uriVariables = [];
181✔
252

253
        foreach ($links as $link) {
181✔
254
            $uriVariables[$link->getParameterName()] = $link;
153✔
255
        }
256

257
        return $uriVariables;
181✔
258
    }
259
}
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