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

api-platform / core / 19171120485

07 Nov 2025 02:18PM UTC coverage: 22.06% (+0.02%) from 22.042%
19171120485

Pull #7515

github

web-flow
Merge e5d1f8e3f into 0edbc37fc
Pull Request #7515: fix(jsonld): read identifier with itemUriTemplate

8 of 11 new or added lines in 2 files covered. (72.73%)

8 existing lines in 4 files now uncovered.

11140 of 50498 relevant lines covered (22.06%)

23.5 hits per line

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

52.54
/src/Metadata/IdentifiersExtractor.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;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
22
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
23
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
26

27
/**
28
 * {@inheritdoc}
29
 *
30
 * @author Antoine Bluchet <soyuka@gmail.com>
31
 */
32
final class IdentifiersExtractor implements IdentifiersExtractorInterface
33
{
34
    use ResourceClassInfoTrait;
35
    private readonly PropertyAccessorInterface $propertyAccessor;
36

37
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ?PropertyAccessorInterface $propertyAccessor = null)
38
    {
39
        $this->resourceMetadataFactory = $resourceMetadataFactory;
604✔
40
        $this->resourceClassResolver = $resourceClassResolver;
604✔
41
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
604✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     *
47
     * TODO: 3.0 identifiers should be stringable?
48
     */
49
    public function getIdentifiersFromItem(object $item, ?Operation $operation = null, array $context = []): array
50
    {
51
        if (!$this->isResourceClass($this->getObjectClass($item))) {
480✔
52
            return ['id' => $this->propertyAccessor->getValue($item, 'id')];
8✔
53
        }
54

55
        if ($operation && $operation->getClass()) {
472✔
56
            return $this->getIdentifiersFromOperation($item, $operation, $context);
472✔
57
        }
58

59
        $resourceClass = $this->getResourceClass($item, true);
20✔
60
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
20✔
61

62
        return $this->getIdentifiersFromOperation($item, $operation, $context);
20✔
63
    }
64

65
    /**
66
     * @param array<string, mixed> $context
67
     */
68
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
69
    {
70
        if ($operation instanceof HttpOperation) {
472✔
71
            $links = $operation->getUriVariables();
472✔
72
        } elseif ($operation instanceof GraphQlOperation) {
×
73
            $links = $operation->getLinks();
×
74
        }
75

76
        $identifiers = [];
472✔
77
        foreach ($links ?? [] as $k => $link) {
472✔
78
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
454✔
79
                $compositeIdentifiers = [];
×
80
                foreach ($link->getIdentifiers() as $identifier) {
×
NEW
81
                    $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName(), null, $context, $operation);
×
82
                }
83

84
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
×
85
                continue;
×
86
            }
87

88
            $parameterName = $link->getParameterName();
454✔
89
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty(), $context, $operation);
454✔
90
        }
91

92
        return $identifiers;
472✔
93
    }
94

95
    /**
96
     * Gets the value of the given class property.
97
     *
98
     * @param array<string, mixed> $context
99
     */
100
    private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty, array $context, Operation $operation): float|bool|int|string
101
    {
102
        if ($item instanceof $class) {
454✔
103
            try {
104
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
452✔
105
            } catch (NoSuchPropertyException $e) {
16✔
106
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
107
            }
108
        }
109

110
        // ItemUriTemplate is defined on a collection and we read the identifier alghough the PHP class may be different
111
        if (isset($context['item_uri_template']) && $operation->getClass() === $class) {
6✔
112
            try {
113
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
2✔
NEW
114
            } catch (NoSuchPropertyException $e) {
×
NEW
115
                throw new RuntimeException(\sprintf('Could not retrieve identifier "%s" for class "%s" using itemUriTemplate "%s". Check that the property exists and is accessible.', $property, $class, $context['item_uri_template']), $e->getCode(), $e);
×
116
            }
117
        }
118

119
        if ($toProperty) {
4✔
120
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
4✔
121
        }
122

123
        $resourceClass = $this->getResourceClass($item, true);
×
124
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
×
125
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
×
126

127
            $types = $propertyMetadata->getBuiltinTypes();
×
128
            if (null === ($type = $types[0] ?? null)) {
×
129
                continue;
×
130
            }
131

132
            try {
133
                if ($type->isCollection()) {
×
134
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
135

136
                    if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
×
137
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
×
138
                    }
139
                }
140

141
                if ($type->getClassName() === $class) {
×
142
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
×
143
                }
144
            } catch (NoSuchPropertyException $e) {
×
145
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
146
            }
147
        }
148

149
        throw new RuntimeException('Not able to retrieve identifiers.');
×
150
    }
151

152
    /**
153
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
154
     *
155
     * @param mixed|\Stringable $identifierValue
156
     */
157
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
158
    {
159
        if (null === $identifierValue) {
454✔
160
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
16✔
161
        }
162

163
        if (\is_scalar($identifierValue)) {
454✔
164
            return $identifierValue;
450✔
165
        }
166

167
        if ($identifierValue instanceof \Stringable) {
4✔
168
            return (string) $identifierValue;
4✔
169
        }
170

171
        if ($identifierValue instanceof \BackedEnum) {
×
172
            return (string) $identifierValue->value;
×
173
        }
174

175
        throw new RuntimeException(\sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName));
×
176
    }
177
}
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