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

api-platform / core / 14955007251

11 May 2025 10:44AM UTC coverage: 0.0% (-8.5%) from 8.457%
14955007251

Pull #7135

github

web-flow
Merge 8a231af70 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
    }
×
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();
×
UNCOV
45
        $context += ['operation' => $operation];
×
UNCOV
46
        $uriVariablesDefinitions = $operation->getUriVariables() ?? [];
×
47

UNCOV
48
        foreach ($uriVariables as $parameterName => $value) {
×
UNCOV
49
            $uriVariableDefinition = $context['uri_variables_map'][$parameterName] ?? $uriVariablesDefinitions[$parameterName] ?? $uriVariablesDefinitions['id'] ?? new Link();
×
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];
×
UNCOV
53
            if ($uriVariableDefinition->getCompositeIdentifier()) {
×
54
                $properties = [$parameterName];
×
55
            }
56

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

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

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

UNCOV
75
        return $uriVariables;
×
76
    }
77

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

UNCOV
88
        return $types;
×
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