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

api-platform / core / 20822307873

08 Jan 2026 03:35PM UTC coverage: 21.258% (-7.6%) from 28.879%
20822307873

Pull #7658

github

web-flow
Merge 7e9d13d39 into 550b7621d
Pull Request #7658: fix(openapi): properly document list parameters

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

14467 existing lines in 472 files now uncovered.

12215 of 57461 relevant lines covered (21.26%)

49.28 hits per line

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

63.29
/src/Doctrine/Orm/Extension/EagerLoadingExtension.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\Doctrine\Orm\Extension;
15

16
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
20
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
21
use ApiPlatform\Metadata\Exception\RuntimeException;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use Doctrine\ORM\Mapping\ClassMetadata;
26
use Doctrine\ORM\Query\AST\PartialObjectExpression;
27
use Doctrine\ORM\Query\Expr\Join;
28
use Doctrine\ORM\Query\Expr\Select;
29
use Doctrine\ORM\QueryBuilder;
30
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
31
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
32
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
33
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
34

35
/**
36
 * Eager loads relations.
37
 *
38
 * @author Charles Sarrazin <charles@sarraz.in>
39
 * @author Kévin Dunglas <dunglas@gmail.com>
40
 * @author Antoine Bluchet <soyuka@gmail.com>
41
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
42
 */
43
final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
44
{
45
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly int $maxJoins = 30, private readonly bool $forceEager = true, private readonly bool $fetchPartial = false, private readonly ?ClassMetadataFactoryInterface $classMetadataFactory = null)
46
    {
UNCOV
47
    }
538✔
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass = null, ?Operation $operation = null, array $context = []): void
53
    {
UNCOV
54
        $this->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
297✔
55
    }
56

57
    /**
58
     * The context may contain serialization groups which helps defining joined entities that are readable.
59
     */
60
    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, ?Operation $operation = null, array $context = []): void
61
    {
UNCOV
62
        $this->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
266✔
63
    }
64

65
    private function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass, ?Operation $operation, array $context): void
66
    {
UNCOV
67
        if (null === $resourceClass) {
538✔
68
            throw new InvalidArgumentException('The "$resourceClass" parameter must not be null');
×
69
        }
70

UNCOV
71
        $options = [];
538✔
72

UNCOV
73
        $forceEager = $operation?->getForceEager() ?? $this->forceEager;
538✔
UNCOV
74
        $fetchPartial = class_exists(PartialObjectExpression::class) && ($operation?->getFetchPartial() ?? $this->fetchPartial);
538✔
75

UNCOV
76
        if (!isset($context['groups']) && !isset($context['attributes'])) {
538✔
UNCOV
77
            $contextType = isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context';
343✔
UNCOV
78
            if ($operation) {
343✔
UNCOV
79
                $context += 'denormalization_context' === $contextType ? ($operation->getDenormalizationContext() ?? []) : ($operation->getNormalizationContext() ?? []);
343✔
80
            }
81
        }
82

UNCOV
83
        if (empty($context[AbstractNormalizer::GROUPS]) && !isset($context[AbstractNormalizer::ATTRIBUTES])) {
538✔
UNCOV
84
            return;
343✔
85
        }
86

UNCOV
87
        if (!empty($context[AbstractNormalizer::GROUPS])) {
198✔
UNCOV
88
            $options['serializer_groups'] = (array) $context[AbstractNormalizer::GROUPS];
119✔
89
        }
90

UNCOV
91
        if ($operation && $normalizationGroups = $operation->getNormalizationContext()['groups'] ?? null) {
198✔
UNCOV
92
            $options['normalization_groups'] = $normalizationGroups;
113✔
93
        }
94

UNCOV
95
        if ($operation && $denormalizationGroups = $operation->getDenormalizationContext()['groups'] ?? null) {
198✔
UNCOV
96
            $options['denormalization_groups'] = $denormalizationGroups;
72✔
97
        }
98

UNCOV
99
        $selects = $this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $options, $context);
198✔
UNCOV
100
        $selectsByClass = [];
198✔
UNCOV
101
        foreach ($selects as [$entity, $alias, $fields]) {
198✔
UNCOV
102
            if ($entity === $resourceClass) {
91✔
103
                // We don't perform partial select the root entity
UNCOV
104
                $fields = null;
91✔
105
            }
106

UNCOV
107
            if (!isset($selectsByClass[$entity])) {
91✔
UNCOV
108
                $selectsByClass[$entity] = [
91✔
UNCOV
109
                    'aliases' => [$alias => true],
91✔
UNCOV
110
                    'fields' => null === $fields ? null : array_flip($fields),
91✔
UNCOV
111
                ];
91✔
112
            } else {
UNCOV
113
                $selectsByClass[$entity]['aliases'][$alias] = true;
42✔
UNCOV
114
                if (null === $selectsByClass[$entity]['fields']) {
42✔
115
                    continue;
42✔
116
                }
117

UNCOV
118
                if (null === $fields) {
×
119
                    $selectsByClass[$entity]['fields'] = null;
×
120
                    continue;
×
121
                }
122

123
                // Merge fields
UNCOV
124
                foreach ($fields as $field) {
×
UNCOV
125
                    $selectsByClass[$entity]['fields'][$field] = true;
×
126
                }
127
            }
128
        }
129

UNCOV
130
        $existingSelects = [];
198✔
UNCOV
131
        foreach ($queryBuilder->getDQLPart('select') ?? [] as $dqlSelect) {
198✔
UNCOV
132
            if (!$dqlSelect instanceof Select) {
198✔
133
                continue;
×
134
            }
UNCOV
135
            foreach ($dqlSelect->getParts() as $part) {
198✔
UNCOV
136
                $existingSelects[(string) $part] = true;
198✔
137
            }
138
        }
139

UNCOV
140
        foreach ($selectsByClass as $data) {
198✔
UNCOV
141
            $fields = null === $data['fields'] ? null : array_keys($data['fields']);
91✔
UNCOV
142
            foreach (array_keys($data['aliases']) as $alias) {
91✔
UNCOV
143
                if (isset($existingSelects[$alias])) {
91✔
144
                    continue;
×
145
                }
146

UNCOV
147
                if (null === $fields) {
91✔
UNCOV
148
                    $queryBuilder->addSelect($alias);
91✔
149
                } else {
UNCOV
150
                    $queryBuilder->addSelect(\sprintf('partial %s.{%s}', $alias, implode(',', $fields)));
×
151
                }
152
            }
153
        }
154
    }
155

156
    /**
157
     * Joins relations to eager load.
158
     *
159
     * @param bool $wasLeftJoin  if the relation containing the new one had a left join, we have to force the new one to left join too
160
     * @param int  $joinCount    the number of joins
161
     * @param int  $currentDepth the current max depth
162
     *
163
     * @throws RuntimeException when the max number of joins has been reached
164
     */
165
    private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, bool $forceEager, bool $fetchPartial, string $parentAlias, array $options = [], array $normalizationContext = [], bool $wasLeftJoin = false, int &$joinCount = 0, ?int $currentDepth = null, ?string $parentAssociation = null): iterable
166
    {
UNCOV
167
        if ($joinCount > $this->maxJoins) {
198✔
168
            throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary with the "api_platform.eager_loading.max_joins" configuration key (https://api-platform.com/docs/core/performance/#eager-loading), or limit the maximum serialization depth using the "enable_max_depth" option of the Symfony serializer (https://symfony.com/doc/current/components/serializer.html#handling-serialization-depth).');
×
169
        }
170

UNCOV
171
        $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth;
198✔
UNCOV
172
        $entityManager = $queryBuilder->getEntityManager();
198✔
UNCOV
173
        $classMetadata = $entityManager->getClassMetadata($resourceClass);
198✔
UNCOV
174
        $attributesMetadata = $this->classMetadataFactory?->getMetadataFor($resourceClass)->getAttributesMetadata();
198✔
175

UNCOV
176
        foreach ($classMetadata->associationMappings as $association => $mapping) {
198✔
177
            // Don't join if max depth is enabled and the current depth limit is reached
UNCOV
178
            if (0 === $currentDepth && ($normalizationContext[AbstractObjectNormalizer::ENABLE_MAX_DEPTH] ?? false)) {
150✔
179
                continue;
×
180
            }
181

182
            try {
UNCOV
183
                $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $options);
150✔
184
            } catch (PropertyNotFoundException) {
×
185
                // skip properties not found
186
                continue;
×
187
                // @phpstan-ignore-next-line indeed this can be thrown by the SerializerPropertyMetadataFactory
188
            } catch (ResourceClassNotFoundException) {
×
189
                // skip associations that are not resource classes
190
                continue;
×
191
            }
192

193
            if (
194
                // Always skip extra lazy associations
UNCOV
195
                ClassMetadata::FETCH_EXTRA_LAZY === $mapping['fetch']
150✔
196
                // We don't want to interfere with doctrine on this association
UNCOV
197
                || (false === $forceEager && ClassMetadata::FETCH_EAGER !== $mapping['fetch'])
150✔
198
            ) {
199
                continue;
1✔
200
            }
201

202
            // prepare the child context
UNCOV
203
            $childNormalizationContext = $normalizationContext;
149✔
UNCOV
204
            if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) {
149✔
UNCOV
205
                if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) {
92✔
UNCOV
206
                    $childNormalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association];
42✔
207
                }
208
            } else {
UNCOV
209
                $inAttributes = null;
59✔
210
            }
211

UNCOV
212
            $fetchEager = $propertyMetadata->getFetchEager();
149✔
UNCOV
213
            $uriTemplate = $propertyMetadata->getUriTemplate();
149✔
214

UNCOV
215
            if (false === $fetchEager || null !== $uriTemplate) {
149✔
216
                continue;
4✔
217
            }
218

UNCOV
219
            if (true !== $fetchEager && (false === $propertyMetadata->isReadable() || false === $inAttributes)) {
145✔
UNCOV
220
                continue;
130✔
221
            }
222

223
            // Avoid joining back to the parent that we just came from, but only on *ToOne relations
224
            if (
UNCOV
225
                null !== $parentAssociation
91✔
UNCOV
226
                && isset($mapping['inversedBy'])
91✔
UNCOV
227
                && $mapping['sourceEntity'] === $mapping['targetEntity']
91✔
UNCOV
228
                && $mapping['inversedBy'] === $parentAssociation
91✔
UNCOV
229
                && $mapping['type'] & ClassMetadata::TO_ONE
91✔
230
            ) {
231
                continue;
×
232
            }
233

UNCOV
234
            $existingJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, $parentAlias, $association);
91✔
235

UNCOV
236
            if (null !== $existingJoin) {
91✔
UNCOV
237
                $associationAlias = $existingJoin->getAlias();
7✔
UNCOV
238
                $isLeftJoin = Join::LEFT_JOIN === $existingJoin->getJoinType();
7✔
239
            } else {
UNCOV
240
                $joinColumn = $mapping['joinColumns'][0] ?? ['nullable' => true];
87✔
UNCOV
241
                if (\is_array($joinColumn)) {
87✔
UNCOV
242
                    $isNullable = $joinColumn['nullable'] ?? true;
44✔
243
                } else {
UNCOV
244
                    $isNullable = $joinColumn->nullable ?? true;
65✔
245
                }
246

UNCOV
247
                $isLeftJoin = false !== $wasLeftJoin || true === $isNullable;
87✔
UNCOV
248
                $method = $isLeftJoin ? 'leftJoin' : 'innerJoin';
87✔
249

UNCOV
250
                $associationAlias = $queryNameGenerator->generateJoinAlias($association);
87✔
UNCOV
251
                $queryBuilder->{$method}(\sprintf('%s.%s', $parentAlias, $association), $associationAlias);
87✔
UNCOV
252
                ++$joinCount;
87✔
253
            }
254

UNCOV
255
            if (true === $fetchPartial) {
91✔
256
                try {
UNCOV
257
                    $propertyOptions = $this->getPropertyContext($attributesMetadata[$association] ?? null, $options);
×
UNCOV
258
                    yield from $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyOptions);
×
259
                } catch (ResourceClassNotFoundException) {
×
260
                    continue;
×
261
                }
262
            } else {
UNCOV
263
                $propertyOptions = null;
91✔
UNCOV
264
                yield [$resourceClass, $associationAlias, null];
91✔
265
            }
266

267
            // Avoid recursive joins for self-referencing relations
UNCOV
268
            if ($mapping['targetEntity'] === $resourceClass) {
91✔
269
                continue;
4✔
270
            }
271

272
            // Only join the relation's relations recursively if it's a readableLink
UNCOV
273
            if (true !== $fetchEager && (true !== $propertyMetadata->isReadableLink())) {
87✔
UNCOV
274
                continue;
47✔
275
            }
276

UNCOV
277
            if (isset($attributesMetadata[$association])) {
52✔
UNCOV
278
                $maxDepth = $attributesMetadata[$association]->getMaxDepth();
52✔
279

280
                // The current depth is the lowest max depth available in the ancestor tree.
UNCOV
281
                if (null !== $maxDepth && (null === $currentDepth || $maxDepth < $currentDepth)) {
52✔
282
                    $currentDepth = $maxDepth;
×
283
                }
284
            }
285

UNCOV
286
            $propertyOptions ??= $this->getPropertyContext($attributesMetadata[$association] ?? null, $options);
52✔
UNCOV
287
            yield from $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $propertyOptions, $childNormalizationContext, $isLeftJoin, $joinCount, $currentDepth, $association);
52✔
288
        }
289
    }
290

291
    private function getPropertyContext(?AttributeMetadataInterface $attributeMetadata, array $options): array
292
    {
UNCOV
293
        if (null === $attributeMetadata) {
52✔
294
            return $options;
×
295
        }
296

UNCOV
297
        $hasNormalizationContext = (isset($options['normalization_groups']) || isset($options['serializer_groups'])) && [] !== $attributeMetadata->getNormalizationContexts();
52✔
UNCOV
298
        $hasDenormalizationContext = (isset($options['denormalization_groups']) || isset($options['serializer_groups'])) && [] !== $attributeMetadata->getDenormalizationContexts();
52✔
299

UNCOV
300
        if (!$hasNormalizationContext && !$hasDenormalizationContext) {
52✔
UNCOV
301
            return $options;
52✔
302
        }
303

UNCOV
304
        $propertyOptions = $options;
×
UNCOV
305
        $propertyOptions['normalization_groups'] ??= $options['serializer_groups'];
×
UNCOV
306
        $propertyOptions['denormalization_groups'] ??= $options['serializer_groups'];
×
307
        // we don't rely on 'serializer_groups' anymore because `context` changes normalization and/or denormalization
308
        // but does not have options for both at the same time
UNCOV
309
        unset($propertyOptions['serializer_groups']);
×
310

UNCOV
311
        if ($hasNormalizationContext) {
×
UNCOV
312
            $originalGroups = $options['normalization_groups'] ?? $options['serializer_groups'];
×
UNCOV
313
            $propertyContext = $attributeMetadata->getNormalizationContextForGroups((array) $originalGroups);
×
UNCOV
314
            $propertyOptions['normalization_groups'] = $propertyContext[AbstractNormalizer::GROUPS] ?? $originalGroups;
×
315
        }
UNCOV
316
        if ($hasDenormalizationContext) {
×
317
            $originalGroups = $options['denormalization_groups'] ?? $options['serializer_groups'];
×
318
            $propertyContext = $attributeMetadata->getDenormalizationContextForGroups((array) $originalGroups);
×
319
            $propertyOptions['denormalization_groups'] = $propertyContext[AbstractNormalizer::GROUPS] ?? $originalGroups;
×
320
        }
321

UNCOV
322
        return $propertyOptions;
×
323
    }
324

325
    private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions): iterable
326
    {
UNCOV
327
        $select = [];
×
UNCOV
328
        $entityManager = $queryBuilder->getEntityManager();
×
UNCOV
329
        $targetClassMetadata = $entityManager->getClassMetadata($entity);
×
UNCOV
330
        if (!empty($targetClassMetadata->subClasses)) {
×
331
            yield [$entity, $associationAlias, null];
×
332

333
            return;
×
334
        }
335

UNCOV
336
        foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
×
UNCOV
337
            $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
338

UNCOV
339
            if (true === $propertyMetadata->isIdentifier()) {
×
UNCOV
340
                $select[] = $property;
×
UNCOV
341
                continue;
×
342
            }
343

344
            // If it's an embedded property see below
UNCOV
345
            if (!\array_key_exists($property, $targetClassMetadata->embeddedClasses)) {
×
UNCOV
346
                $isFetchable = $propertyMetadata->isFetchable();
×
347
                // the field test allows to add methods to a Resource which do not reflect real database fields
UNCOV
348
                if ($targetClassMetadata->hasField($property) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
UNCOV
349
                    $select[] = $property;
×
350
                }
351

UNCOV
352
                continue;
×
353
            }
354

355
            // It's an embedded property, select relevant subfields
356
            foreach ($this->propertyNameCollectionFactory->create($targetClassMetadata->embeddedClasses[$property]['class']) as $embeddedProperty) {
×
357
                $isFetchable = $propertyMetadata->isFetchable();
×
358
                $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
359
                $propertyName = "$property.$embeddedProperty";
×
360
                if ($targetClassMetadata->hasField($propertyName) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
361
                    $select[] = $propertyName;
×
362
                }
363
            }
364
        }
365

UNCOV
366
        yield [$entity, $associationAlias, $select];
×
367
    }
368
}
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