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

api-platform / core / 14967095168

12 May 2025 08:08AM UTC coverage: 22.155% (+13.7%) from 8.457%
14967095168

Pull #7135

github

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

1 of 2 new or added lines in 2 files covered. (50.0%)

120 existing lines in 12 files now uncovered.

10846 of 48956 relevant lines covered (22.15%)

10.12 hits per line

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

0.0
/src/Symfony/EventListener/WriteListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\Error;
17
use ApiPlatform\Metadata\Exception\InvalidIdentifierException;
18
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\UriVariablesConverterInterface;
22
use ApiPlatform\Metadata\Util\ClassInfoTrait;
23
use ApiPlatform\Metadata\Util\CloneTrait;
24
use ApiPlatform\State\ProcessorInterface;
25
use ApiPlatform\State\UriVariablesResolverTrait;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\State\Util\RequestAttributesExtractor;
28
use Symfony\Component\HttpKernel\Event\ViewEvent;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30

31
/**
32
 * Bridges persistence and the API system.
33
 *
34
 * @author Kévin Dunglas <dunglas@gmail.com>
35
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
36
 */
37
final class WriteListener
38
{
39
    use ClassInfoTrait;
40
    use CloneTrait;
41
    use OperationRequestInitiatorTrait;
42
    use UriVariablesResolverTrait;
43

44
    /**
45
     * @param ProcessorInterface<mixed, mixed> $processor
46
     */
47
    public function __construct(
48
        private readonly ProcessorInterface $processor,
49
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
50
        ?UriVariablesConverterInterface $uriVariablesConverter = null,
51
    ) {
UNCOV
52
        $this->uriVariablesConverter = $uriVariablesConverter;
×
UNCOV
53
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
54
    }
55

56
    /**
57
     * Persists, updates or delete data return by the controller if applicable.
58
     */
59
    public function onKernelView(ViewEvent $event): void
60
    {
UNCOV
61
        $request = $event->getRequest();
×
UNCOV
62
        $operation = $this->initializeOperation($request);
×
63

UNCOV
64
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['persist'] || !$operation) {
×
65
            return;
×
66
        }
67

UNCOV
68
        if (null === $operation->canWrite()) {
×
UNCOV
69
            $operation = $operation->withWrite(!$request->isMethodSafe());
×
70
        }
71

UNCOV
72
        $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
×
UNCOV
73
        if (!$uriVariables && !$operation instanceof Error && $operation instanceof HttpOperation) {
×
74
            try {
UNCOV
75
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
76
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
77
                throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
78
            }
79
        }
80

UNCOV
81
        $data = $this->processor->process($event->getControllerResult(), $operation, $uriVariables, [
×
UNCOV
82
            'request' => $request,
×
UNCOV
83
            'uri_variables' => $uriVariables,
×
UNCOV
84
            'resource_class' => $operation->getClass(),
×
UNCOV
85
            'previous_data' => false === $operation->canRead() ? null : $request->attributes->get('previous_data'),
×
UNCOV
86
        ]);
×
87

UNCOV
88
        if ($data) {
×
UNCOV
89
            $request->attributes->set('original_data', $data);
×
90
        }
91

UNCOV
92
        $event->setControllerResult($data);
×
93
    }
94
}
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