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

api-platform / core / 20729781900

05 Jan 2026 09:31PM UTC coverage: 28.843% (-0.02%) from 28.866%
20729781900

Pull #7645

github

web-flow
Merge 75983e84d into 7d4b5e9c5
Pull Request #7645: Fix partial fetch when relation switches context

8 of 73 new or added lines in 2 files covered. (10.96%)

33 existing lines in 7 files now uncovered.

16778 of 58170 relevant lines covered (28.84%)

78.49 hits per line

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

61.54
/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\AttributeMetadataInterface;
30
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
31
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
32
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
33

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

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

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

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

70
        $options = [];
872✔
71

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

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

82
        if (empty($context[AbstractNormalizer::GROUPS]) && !isset($context[AbstractNormalizer::ATTRIBUTES])) {
872✔
83
            return;
649✔
84
        }
85

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

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

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

98
        $this->joinRelations($queryBuilder, $queryNameGenerator, $resourceClass, $forceEager, $fetchPartial, $queryBuilder->getRootAliases()[0], $options, $context);
226✔
99
    }
100

101
    /**
102
     * Joins relations to eager load.
103
     *
104
     * @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
105
     * @param int  $joinCount    the number of joins
106
     * @param int  $currentDepth the current max depth
107
     *
108
     * @throws RuntimeException when the max number of joins has been reached
109
     */
110
    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): void
111
    {
112
        if ($joinCount > $this->maxJoins) {
226✔
113
            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).');
×
114
        }
115

116
        $currentDepth = $currentDepth > 0 ? $currentDepth - 1 : $currentDepth;
226✔
117
        $entityManager = $queryBuilder->getEntityManager();
226✔
118
        $classMetadata = $entityManager->getClassMetadata($resourceClass);
226✔
119
        $attributesMetadata = $this->classMetadataFactory?->getMetadataFor($resourceClass)->getAttributesMetadata();
226✔
120

121
        foreach ($classMetadata->associationMappings as $association => $mapping) {
226✔
122
            // Don't join if max depth is enabled and the current depth limit is reached
123
            if (0 === $currentDepth && ($normalizationContext[AbstractObjectNormalizer::ENABLE_MAX_DEPTH] ?? false)) {
170✔
124
                continue;
×
125
            }
126

127
            try {
128
                $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $options);
170✔
129
            } catch (PropertyNotFoundException) {
×
130
                // skip properties not found
131
                continue;
×
132
                // @phpstan-ignore-next-line indeed this can be thrown by the SerializerPropertyMetadataFactory
133
            } catch (ResourceClassNotFoundException) {
×
134
                // skip associations that are not resource classes
135
                continue;
×
136
            }
137

138
            if (
139
                // Always skip extra lazy associations
140
                ClassMetadata::FETCH_EXTRA_LAZY === $mapping['fetch']
170✔
141
                // We don't want to interfere with doctrine on this association
142
                || (false === $forceEager && ClassMetadata::FETCH_EAGER !== $mapping['fetch'])
170✔
143
            ) {
144
                continue;
1✔
145
            }
146

147
            // prepare the child context
148
            $childNormalizationContext = $normalizationContext;
169✔
149
            if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) {
169✔
150
                if ($inAttributes = isset($normalizationContext[AbstractNormalizer::ATTRIBUTES][$association])) {
100✔
151
                    $childNormalizationContext[AbstractNormalizer::ATTRIBUTES] = $normalizationContext[AbstractNormalizer::ATTRIBUTES][$association];
44✔
152
                }
153
            } else {
154
                $inAttributes = null;
71✔
155
            }
156

157
            $fetchEager = $propertyMetadata->getFetchEager();
169✔
158
            $uriTemplate = $propertyMetadata->getUriTemplate();
169✔
159

160
            if (false === $fetchEager || null !== $uriTemplate) {
169✔
161
                continue;
4✔
162
            }
163

164
            if (true !== $fetchEager && (false === $propertyMetadata->isReadable() || false === $inAttributes)) {
165✔
165
                continue;
146✔
166
            }
167

168
            // Avoid joining back to the parent that we just came from, but only on *ToOne relations
169
            if (
170
                null !== $parentAssociation
99✔
171
                && isset($mapping['inversedBy'])
99✔
172
                && $mapping['sourceEntity'] === $mapping['targetEntity']
99✔
173
                && $mapping['inversedBy'] === $parentAssociation
99✔
174
                && $mapping['type'] & ClassMetadata::TO_ONE
99✔
175
            ) {
176
                continue;
×
177
            }
178

179
            $existingJoin = QueryBuilderHelper::getExistingJoin($queryBuilder, $parentAlias, $association);
99✔
180

181
            if (null !== $existingJoin) {
99✔
182
                $associationAlias = $existingJoin->getAlias();
9✔
183
                $isLeftJoin = Join::LEFT_JOIN === $existingJoin->getJoinType();
9✔
184
            } else {
185
                $joinColumn = $mapping['joinColumns'][0] ?? ['nullable' => true];
93✔
186
                if (\is_array($joinColumn)) {
93✔
187
                    $isNullable = $joinColumn['nullable'] ?? true;
46✔
188
                } else {
189
                    $isNullable = $joinColumn->nullable ?? true;
69✔
190
                }
191

192
                $isLeftJoin = false !== $wasLeftJoin || true === $isNullable;
93✔
193
                $method = $isLeftJoin ? 'leftJoin' : 'innerJoin';
93✔
194

195
                $associationAlias = $queryNameGenerator->generateJoinAlias($association);
93✔
196
                $queryBuilder->{$method}(\sprintf('%s.%s', $parentAlias, $association), $associationAlias);
93✔
197
                ++$joinCount;
93✔
198
            }
199

200

201
            if (true === $fetchPartial) {
99✔
202
                try {
NEW
203
                    $propertyOptions = $this->getPropertyContext($attributesMetadata[$association] ?? null, $options);
×
NEW
204
                    $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyOptions);
×
205
                } catch (ResourceClassNotFoundException) {
×
206
                    continue;
×
207
                }
208
            } else {
209
                $propertyOptions = null;
99✔
210
                $this->addSelectOnce($queryBuilder, $associationAlias);
99✔
211
            }
212

213
            // Avoid recursive joins for self-referencing relations
214
            if ($mapping['targetEntity'] === $resourceClass) {
99✔
215
                continue;
4✔
216
            }
217

218
            // Only join the relation's relations recursively if it's a readableLink
219
            if (true !== $fetchEager && (true !== $propertyMetadata->isReadableLink())) {
95✔
220
                continue;
51✔
221
            }
222

223
            if (isset($attributesMetadata[$association])) {
56✔
224
                $maxDepth = $attributesMetadata[$association]->getMaxDepth();
56✔
225

226
                // The current depth is the lowest max depth available in the ancestor tree.
227
                if (null !== $maxDepth && (null === $currentDepth || $maxDepth < $currentDepth)) {
56✔
228
                    $currentDepth = $maxDepth;
×
229
                }
230
            }
231

232
            $propertyOptions ??= $this->getPropertyContext($attributesMetadata[$association] ?? null, $options);
56✔
233
            $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $fetchPartial, $associationAlias, $propertyOptions, $childNormalizationContext, $isLeftJoin, $joinCount, $currentDepth, $association);
56✔
234
        }
235
    }
236

237
    private function getPropertyContext(AttributeMetadataInterface|null $attributeMetadata,  array $options): array
238
    {
239
        if (null === $attributeMetadata) {
56✔
NEW
240
            return $options;
×
241
        }
242

243
        $hasNormalizationContext = (isset($options['normalization_groups']) || isset($options['serializer_groups'])) && $attributeMetadata->getNormalizationContexts() !== [];
56✔
244
        $hasDenormalizationContext = (isset($options['denormalization_groups']) || isset($options['serializer_groups'])) && $attributeMetadata->getDenormalizationContexts() !== [];
56✔
245

246
        if (!$hasNormalizationContext && !$hasDenormalizationContext) {
56✔
247
            return $options;
56✔
248
        }
249

NEW
250
        $propertyOptions = $options;
×
NEW
251
        $propertyOptions['normalization_groups'] ??= $options['serializer_groups'];
×
NEW
252
        $propertyOptions['denormalization_groups'] ??= $options['serializer_groups'];
×
253
        // we don't rely on 'serializer_groups' anymore because `context` changes normalization and/or denormalization
254
        // but does not have options for both at the same time
NEW
255
        unset($propertyOptions['serializer_groups']);
×
256

NEW
257
        if ($hasNormalizationContext) {
×
NEW
258
            $originalGroups = $options['normalization_groups'] ?? $options['serializer_groups'];
×
NEW
259
            $propertyContext = $attributeMetadata->getNormalizationContextForGroups((array) $originalGroups);
×
NEW
260
            $propertyOptions['normalization_groups'] = $propertyContext[AbstractNormalizer::GROUPS] ?? $originalGroups;
×
261
        }
NEW
262
        if ($hasDenormalizationContext) {
×
NEW
263
            $originalGroups = $options['denormalization_groups'] ?? $options['serializer_groups'];
×
NEW
264
            $propertyContext = $attributeMetadata->getDenormalizationContextForGroups((array) $originalGroups);
×
NEW
265
            $propertyOptions['denormalization_groups'] = $propertyContext[AbstractNormalizer::GROUPS] ?? $originalGroups;
×
266
        }
267

NEW
268
        return $propertyOptions;
×
269
    }
270

271
    private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions): void
272
    {
273
        $select = [];
×
274
        $entityManager = $queryBuilder->getEntityManager();
×
275
        $targetClassMetadata = $entityManager->getClassMetadata($entity);
×
276
        if (!empty($targetClassMetadata->subClasses)) {
×
277
            $this->addSelectOnce($queryBuilder, $associationAlias);
×
278

279
            return;
×
280
        }
281

282
        foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
×
283
            $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
284

285
            if (true === $propertyMetadata->isIdentifier()) {
×
286
                $select[] = $property;
×
287
                continue;
×
288
            }
289

290
            // If it's an embedded property see below
291
            if (!\array_key_exists($property, $targetClassMetadata->embeddedClasses)) {
×
292
                $isFetchable = $propertyMetadata->isFetchable();
×
293
                // the field test allows to add methods to a Resource which do not reflect real database fields
294
                if ($targetClassMetadata->hasField($property) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
295
                    $select[] = $property;
×
296
                }
297

298
                continue;
×
299
            }
300

301
            // It's an embedded property, select relevant subfields
302
            foreach ($this->propertyNameCollectionFactory->create($targetClassMetadata->embeddedClasses[$property]['class']) as $embeddedProperty) {
×
303
                $isFetchable = $propertyMetadata->isFetchable();
×
304
                $propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
×
305
                $propertyName = "$property.$embeddedProperty";
×
306
                if ($targetClassMetadata->hasField($propertyName) && (true === $isFetchable || $propertyMetadata->isReadable())) {
×
307
                    $select[] = $propertyName;
×
308
                }
309
            }
310
        }
311

312
        $queryBuilder->addSelect(\sprintf('partial %s.{%s}', $associationAlias, implode(',', $select)));
×
313
    }
314

315
    private function addSelectOnce(QueryBuilder $queryBuilder, string $alias): void
316
    {
317
        $existingSelects = array_reduce($queryBuilder->getDQLPart('select') ?? [], fn ($existing, $dqlSelect) => ($dqlSelect instanceof Select) ? array_merge($existing, $dqlSelect->getParts()) : $existing, []);
99✔
318

319
        if (!\in_array($alias, $existingSelects, true)) {
99✔
320
            $queryBuilder->addSelect($alias);
99✔
321
        }
322
    }
323
}
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