• 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

89.66
/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
    }
13✔
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) {
13✔
60
            $shortName = 'Entrypoint';
×
61
        }
62

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

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

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

UNCOV
81
        return $context;
3✔
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) {
12✔
UNCOV
90
            return ['@context' => $this->contextBuilder->getEntrypointContext()];
2✔
91
        }
92

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

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

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

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

UNCOV
112
        return null;
1✔
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