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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

96.0
/src/Metadata/UriVariablesConverter.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\InvalidUriVariableException;
17
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use Symfony\Component\PropertyInfo\Type;
20

21
/**
22
 * UriVariables converter that chains uri variables transformers.
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
final class UriVariablesConverter implements UriVariablesConverterInterface
27
{
28
    /**
29
     * @param iterable<UriVariableTransformerInterface> $uriVariableTransformers
30
     */
31
    public function __construct(private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly iterable $uriVariableTransformers)
32
    {
UNCOV
33
    }
951✔
34

35
    /**
36
     * {@inheritdoc}
37
     *
38
     * To handle the composite identifiers type correctly, use an `uri_variables_map` that maps uriVariables to their uriVariablesDefinition.
39
     * Indeed, a composite identifier will already be parsed, and their corresponding properties will be the parameterName and not the defined
40
     * identifiers.
41
     */
42
    public function convert(array $uriVariables, string $class, array $context = []): array
43
    {
UNCOV
44
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($class)->getOperation();
818✔
UNCOV
45
        $context += ['operation' => $operation];
818✔
UNCOV
46
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
818✔
47

UNCOV
48
        foreach ($uriVariables as $parameterName => $value) {
818✔
UNCOV
49
            $uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
327✔
50

51
            // When a composite identifier is used, we assume that the parameterName is the property to find our type
UNCOV
52
            $properties = $uriVariableDefinition->getIdentifiers() ?? [$parameterName];
327✔
UNCOV
53
            if ($uriVariableDefinition->getCompositeIdentifier()) {
327✔
54
                $properties = [$parameterName];
×
55
            }
56

UNCOV
57
            if (!$types = $this->getIdentifierTypes($uriVariableDefinition->getFromClass() ?? $class, $properties)) {
327✔
UNCOV
58
                continue;
2✔
59
            }
60

UNCOV
61
            foreach ($this->uriVariableTransformers as $uriVariableTransformer) {
325✔
UNCOV
62
                if (!$uriVariableTransformer->supportsTransformation($value, $types, $context)) {
325✔
UNCOV
63
                    continue;
325✔
64
                }
65

66
                try {
UNCOV
67
                    $uriVariables[$parameterName] = $uriVariableTransformer->transform($value, $types, $context);
292✔
UNCOV
68
                    break;
290✔
UNCOV
69
                } catch (InvalidUriVariableException $e) {
2✔
UNCOV
70
                    throw new InvalidUriVariableException(\sprintf('Identifier "%s" could not be transformed.', $parameterName), $e->getCode(), $e);
2✔
71
                }
72
            }
73
        }
74

UNCOV
75
        return $uriVariables;
817✔
76
    }
77

78
    private function getIdentifierTypes(string $resourceClass, array $properties): array
79
    {
UNCOV
80
        $types = [];
327✔
UNCOV
81
        foreach ($properties as $property) {
327✔
UNCOV
82
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
327✔
UNCOV
83
            foreach ($propertyMetadata->getBuiltinTypes() as $type) {
327✔
UNCOV
84
                $types[] = Type::BUILTIN_TYPE_OBJECT === ($builtinType = $type->getBuiltinType()) ? $type->getClassName() : $builtinType;
325✔
85
            }
86
        }
87

UNCOV
88
        return $types;
327✔
89
    }
90
}
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

© 2025 Coveralls, Inc