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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

87.88
/src/Metadata/Resource/Factory/ObjectMapperMetadataCollectionFactory.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\Doctrine\Odm\State\Options as OdmOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
19
use Symfony\Component\ObjectMapper\Metadata\Mapping;
20
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
21

22
class ObjectMapperMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
23
{
24
    public function __construct(
25
        private readonly ResourceMetadataCollectionFactoryInterface $decorated,
26
        private readonly ObjectMapperMetadataFactoryInterface $objectMapperMetadata,
27
    ) {
28
    }
814✔
29

30
    public function create(string $resourceClass): ResourceMetadataCollection
31
    {
32
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
124✔
33

34
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
124✔
35
            $operations = $resourceMetadata->getOperations();
122✔
36

37
            if (!$operations) {
122✔
38
                continue;
×
39
            }
40

41
            foreach ($operations as $operationKey => $operation) {
122✔
42
                if (null !== $operation->canMap()) {
122✔
43
                    continue;
2✔
44
                }
45

46
                $entityClass = null;
122✔
47
                if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
122✔
48
                    $entityClass = $options->getEntityClass();
10✔
49
                }
50

51
                if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) {
122✔
52
                    $entityClass = $options->getDocumentClass();
4✔
53
                }
54

55
                $class = $operation->getInput()['class'] ?? $operation->getClass();
122✔
56
                $entityMap = null;
122✔
57

58
                // Look for Mapping metadata
59
                if ($this->canBeMapped($class) || ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
122✔
60
                    $found = true;
2✔
61
                    if ($entityMap) {
2✔
62
                        foreach ($entityMap as $mapping) {
2✔
63
                            if ($found = ($mapping->source === $operation->getClass() || $mapping->target === $operation->getClass())) {
2✔
64
                                break;
×
65
                            }
66
                        }
67
                    }
68

69
                    if (!$found) {
2✔
70
                        continue;
2✔
71
                    }
72

73
                    $operations->add($operationKey, $operation->withMap(true));
2✔
74
                }
75
            }
76

77
            $resourceMetadataCollection[$key] = $resourceMetadata->withOperations($operations);
122✔
78
        }
79

80
        return $resourceMetadataCollection;
124✔
81
    }
82

83
    /**
84
     * @return bool|list<Mapping>
85
     */
86
    private function canBeMapped(string $class): bool|array
87
    {
88
        try {
89
            $r = new \ReflectionClass($class);
122✔
90
            if (!$r->isInstantiable() || !($mapping = $this->objectMapperMetadata->create($r->newInstanceWithoutConstructor(), null, ['_api_check_can_be_mapped' => true]))) {
122✔
91
                return false;
122✔
92
            }
93
        } catch (\ReflectionException $e) {
×
94
            return false;
×
95
        }
96

97
        return $mapping;
2✔
98
    }
99
}
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