• 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/JsonLd/Action/ContextAction.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\JsonLd\Action;
15

16
use ApiPlatform\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
21
use ApiPlatform\State\ProcessorInterface;
22
use ApiPlatform\State\ProviderInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Serializer\SerializerInterface;
27

28
/**
29
 * Generates JSON-LD contexts.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class ContextAction
34
{
35
    public const RESERVED_SHORT_NAMES = [
36
        'ConstraintViolationList' => true,
37
        'Error' => true,
38
    ];
39

40
    public function __construct(
41
        private readonly ContextBuilderInterface $contextBuilder,
42
        private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
43
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
44
        private readonly ?ProviderInterface $provider = null,
45
        private readonly ?ProcessorInterface $processor = null,
46
        private readonly ?SerializerInterface $serializer = null,
47
    ) {
UNCOV
48
    }
×
49

50
    /**
51
     * Generates a context according to the type requested.
52
     *
53
     * @throws NotFoundHttpException
54
     *
55
     * @return array{'@context': array<string, mixed>}|Response
56
     */
57
    public function __invoke(string $shortName = 'Entrypoint', ?Request $request = null): array|Response
58
    {
UNCOV
59
        if (!$shortName) {
×
60
            $shortName = 'Entrypoint';
×
61
        }
62

UNCOV
63
        if (null !== $request && $this->provider && $this->processor && $this->serializer) {
×
UNCOV
64
            $operation = new Get(
×
UNCOV
65
                outputFormats: ['jsonld' => ['application/ld+json']],
×
UNCOV
66
                validate: false,
×
UNCOV
67
                provider: fn () => $this->getContext($shortName),
×
UNCOV
68
                serialize: false,
×
UNCOV
69
                read: true
×
UNCOV
70
            );
×
UNCOV
71
            $context = ['request' => $request];
×
UNCOV
72
            $jsonLdContext = $this->provider->provide($operation, [], $context);
×
73

UNCOV
74
            return $this->processor->process($this->serializer->serialize($jsonLdContext, 'json'), $operation, [], $context);
×
75
        }
76

UNCOV
77
        if (!$context = $this->getContext($shortName)) {
×
UNCOV
78
            throw new NotFoundHttpException();
×
79
        }
80

UNCOV
81
        return $context;
×
82
    }
83

84
    /**
85
     * @return array{'@context': array<string, mixed>}|null
86
     */
87
    private function getContext(string $shortName): ?array
88
    {
UNCOV
89
        if ('Entrypoint' === $shortName) {
×
UNCOV
90
            return ['@context' => $this->contextBuilder->getEntrypointContext()];
×
91
        }
92

93
        // TODO: remove this, exceptions are resources since 3.2
UNCOV
94
        if (isset(self::RESERVED_SHORT_NAMES[$shortName])) {
×
UNCOV
95
            return ['@context' => $this->contextBuilder->getBaseContext()];
×
96
        }
97

UNCOV
98
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
×
UNCOV
99
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($resourceClass);
×
100

101
            try {
UNCOV
102
                $resourceMetadataCollection = $resourceMetadataCollection->getOperation();
×
103
            } catch (OperationNotFoundException) {
×
104
                continue;
×
105
            }
106

UNCOV
107
            if ($shortName === $resourceMetadataCollection->getShortName()) {
×
UNCOV
108
                return ['@context' => $this->contextBuilder->getResourceContext($resourceClass)];
×
109
            }
110
        }
111

UNCOV
112
        return null;
×
113
    }
114
}
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