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

api-platform / core / 19437753965

17 Nov 2025 04:59PM UTC coverage: 24.689%. First build
19437753965

Pull #7541

github

web-flow
Merge 357e51854 into c28c624a8
Pull Request #7541: fix(state): object mapper flag

76 of 94 new or added lines in 26 files covered. (80.85%)

14208 of 57548 relevant lines covered (24.69%)

26.95 hits per line

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

88.24
/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
    }
792✔
29

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

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

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

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

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

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

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

58
                // Look for Mapping metadata
59
                if ($this->canBeMapped($class) || ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
116✔
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✔
NEW
64
                                break;
×
65
                            }
66
                        }
67
                    }
68

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

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

79
            $resourceMetadataCollection[$key] = $resourceMetadata->withOperations($operations);
116✔
80
        }
81

82
        return $resourceMetadataCollection;
118✔
83
    }
84

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

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