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

api-platform / core / 20730978299

05 Jan 2026 10:22PM UTC coverage: 24.505% (-4.4%) from 28.866%
20730978299

Pull #7647

github

web-flow
Merge 706a84846 into 7d4b5e9c5
Pull Request #7647: fix(doctrine): fix partial fetch with same entity included multiple time with different fields

23 of 60 new or added lines in 2 files covered. (38.33%)

837 existing lines in 70 files now uncovered.

14251 of 58156 relevant lines covered (24.5%)

14.82 hits per line

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

63.97
/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\Expr\Join;
27
use Doctrine\ORM\Query\Expr\Select;
28
use Doctrine\ORM\QueryBuilder;
29
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
30
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
31
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
32

33
/**
34
 * Eager loads relations.
35
 *
36
 * @author Charles Sarrazin <charles@sarraz.in>
37
 * @author Kévin Dunglas <dunglas@gmail.com>
38
 * @author Antoine Bluchet <soyuka@gmail.com>
39
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
40
 */
41
final class EagerLoadingExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
42
{
43
    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)
44
    {
45
    }
167✔
46

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

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

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

69
        $options = [];
167✔
70

71
        $forceEager = $operation?->getForceEager() ?? $this->forceEager;
167✔
72
        $fetchPartial = $operation?->getFetchPartial() ?? $this->fetchPartial;
167✔
73

74
        if (!isset($context['groups']) && !isset($context['attributes'])) {
167✔
75
            $contextType = isset($context['api_denormalize']) ? 'denormalization_context' : 'normalization_context';
153✔
76
            if ($operation) {
153✔
77
                $context += 'denormalization_context' === $contextType ? ($operation->getDenormalizationContext() ?? []) : ($operation->getNormalizationContext() ?? []);
153✔
78
            }
79
        }
80

81
        if (empty($context[AbstractNormalizer::GROUPS]) && !isset($context[AbstractNormalizer::ATTRIBUTES])) {
167✔
82
            return;
153✔
83
        }
84

85
        if (!empty($context[AbstractNormalizer::GROUPS])) {
14✔
86
            $options['serializer_groups'] = (array) $context[AbstractNormalizer::GROUPS];
10✔
87
        }
88

89
        if ($operation && $normalizationGroups = $operation->getNormalizationContext()['groups'] ?? null) {
14✔
90
            $options['normalization_groups'] = $normalizationGroups;
10✔
91
        }
92

93
        if ($operation && $denormalizationGroups = $operation->getDenormalizationContext()['groups'] ?? null) {
14✔
94
            $options['denormalization_groups'] = $denormalizationGroups;
4✔
95
        }
96

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

105
            if (!isset($selectsByClass[$entity])) {
4✔
106
                $selectsByClass[$entity] = [
4✔
107
                    'aliases' => [$alias => true],
4✔
108
                    'fields' => null === $fields ? null : array_flip($fields),
4✔
109
                ];
4✔
110
            } else {
NEW
111
                $selectsByClass[$entity]['aliases'][$alias] = true;
×
NEW
112
                if (null === $selectsByClass[$entity]['fields']) {
×
NEW
113
                    continue;
×
114
                }
115

NEW
116
                if (null === $fields) {
×
NEW
117
                    $selectsByClass[$entity]['fields'] = null;
×
NEW
118
                    continue;
×
119
                }
120

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

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

138
        foreach ($selectsByClass as $data) {
14✔
139
            $fields = $data['fields'] === null ? null : array_keys($data['fields']);
4✔
140
            foreach (array_keys($data['aliases']) as $alias) {
4✔
141
                if (isset($existingSelects[$alias])) {
4✔
NEW
142
                    continue;
×
143
                }
144

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

154
    /**
155
     * Joins relations to eager load.
156
     *
157
     * @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
158
     * @param int  $joinCount    the number of joins
159
     * @param int  $currentDepth the current max depth
160
     *
161
     * @throws RuntimeException when the max number of joins has been reached
162
     */
163
    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
164
    {
165
        if ($joinCount > $this->maxJoins) {
14✔
166
            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).');
×
167
        }
168

169
        $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth;
14✔
170
        $entityManager = $queryBuilder->getEntityManager();
14✔
171
        $classMetadata = $entityManager->getClassMetadata($resourceClass);
14✔
172
        $attributesMetadata = $this->classMetadataFactory?->getMetadataFor($resourceClass)->getAttributesMetadata();
14✔
173

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

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

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

200
            // prepare the child context
201
            $childNormalizationContext = $normalizationContext;
10✔
202
            if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) {
10✔
203
                if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) {
4✔
204
                    $childNormalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association];
1✔
205
                }
206
            } else {
207
                $inAttributes = null;
6✔
208
            }
209

210
            $fetchEager = $propertyMetadata->getFetchEager();
10✔
211
            $uriTemplate = $propertyMetadata->getUriTemplate();
10✔
212

213
            if (false === $fetchEager || null !== $uriTemplate) {
10✔
214
                continue;
×
215
            }
216

217
            if (true !== $fetchEager && (false === $propertyMetadata->isReadable() || false === $inAttributes)) {
10✔
218
                continue;
8✔
219
            }
220

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

232
            $existingJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, $parentAlias, $association);
4✔
233

234
            if (null !== $existingJoin) {
4✔
235
                $associationAlias = $existingJoin->getAlias();
1✔
236
                $isLeftJoin = Join::LEFT_JOIN === $existingJoin->getJoinType();
1✔
237
            } else {
238
                $joinColumn = $mapping['joinColumns'][0] ?? ['nullable' => true];
3✔
239
                if (\is_array($joinColumn)) {
3✔
240
                    $isNullable = $joinColumn['nullable'] ?? true;
1✔
241
                } else {
242
                    $isNullable = $joinColumn->nullable ?? true;
2✔
243
                }
244

245
                $isLeftJoin = false !== $wasLeftJoin || true === $isNullable;
3✔
246
                $method = $isLeftJoin ? 'leftJoin' : 'innerJoin';
3✔
247

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

253
            if (true === $fetchPartial) {
4✔
254
                try {
NEW
255
                    yield from $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $options);
×
256
                } catch (ResourceClassNotFoundException) {
×
257
                    continue;
×
258
                }
259
            } else {
260
                yield [$resourceClass, $associationAlias, null];
4✔
261
            }
262

263
            // Avoid recursive joins for self-referencing relations
264
            if ($mapping['targetEntity'] === $resourceClass) {
4✔
265
                continue;
×
266
            }
267

268
            // Only join the relation's relations recursively if it's a readableLink
269
            if (true !== $fetchEager && (true !== $propertyMetadata->isReadableLink())) {
4✔
270
                continue;
2✔
271
            }
272

273
            if (isset($attributesMetadata[$association])) {
2✔
274
                $maxDepth = $attributesMetadata[$association]->getMaxDepth();
2✔
275

276
                // The current depth is the lowest max depth available in the ancestor tree.
277
                if (null !== $maxDepth && (null === $currentDepth || $maxDepth < $currentDepth)) {
2✔
278
                    $currentDepth = $maxDepth;
×
279
                }
280
            }
281

282
            yield from $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $options, $childNormalizationContext, $isLeftJoin, $joinCount, $currentDepth, $association);
2✔
283
        }
284
    }
285

286
    private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions): iterable
287
    {
288
        $select = [];
×
289
        $entityManager = $queryBuilder->getEntityManager();
×
290
        $targetClassMetadata = $entityManager->getClassMetadata($entity);
×
291
        if (!empty($targetClassMetadata->subClasses)) {
×
NEW
292
            yield [$entity, $associationAlias, null];
×
293

294
            return;
×
295
        }
296

297
        foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
×
298
            $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
299

300
            if (true === $propertyMetadata->isIdentifier()) {
×
301
                $select[] = $property;
×
302
                continue;
×
303
            }
304

305
            // If it's an embedded property see below
306
            if (!\array_key_exists($property, $targetClassMetadata->embeddedClasses)) {
×
307
                $isFetchable = $propertyMetadata->isFetchable();
×
308
                // the field test allows to add methods to a Resource which do not reflect real database fields
309
                if ($targetClassMetadata->hasField($property) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
310
                    $select[] = $property;
×
311
                }
312

313
                continue;
×
314
            }
315

316
            // It's an embedded property, select relevant subfields
317
            foreach ($this->propertyNameCollectionFactory->create($targetClassMetadata->embeddedClasses[$property]['class']) as $embeddedProperty) {
×
318
                $isFetchable = $propertyMetadata->isFetchable();
×
319
                $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
320
                $propertyName = "$property.$embeddedProperty";
×
321
                if ($targetClassMetadata->hasField($propertyName) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
322
                    $select[] = $propertyName;
×
323
                }
324
            }
325
        }
326

NEW
327
        yield [$entity, $associationAlias, $select];
×
328
    }
329
}
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