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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

92.86
/src/Serializer/SerializerContextBuilder.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\Serializer;
15

16
use ApiPlatform\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Util\RequestAttributesExtractor;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Serializer\Encoder\CsvEncoder;
21

22
/**
23
 * {@inheritdoc}
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 */
27
final class SerializerContextBuilder implements SerializerContextBuilderInterface
28
{
29
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
30
    {
31
    }
28✔
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
37
    {
38
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
18✔
39
            throw new RuntimeException('Request attributes are not valid.');
1✔
40
        }
41

42
        $operation = $attributes['operation'] ?? $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name']);
17✔
43
        $context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
17✔
44
        $context['operation_name'] = $operation->getName();
17✔
45
        $context['operation'] = $operation;
17✔
46
        $context['resource_class'] = $attributes['resource_class'];
17✔
47
        $context['skip_null_values'] ??= true;
17✔
48
        $context['iri_only'] ??= false;
17✔
49
        $context['request_uri'] = $request->getRequestUri();
17✔
50
        $context['uri'] = $request->getUri();
17✔
51
        $context['input'] = $operation->getInput();
17✔
52
        $context['output'] = $operation->getOutput();
17✔
53

54
        if ($operation->getTypes()) {
17✔
55
            $context['types'] = $operation->getTypes();
×
56
        }
57

58
        if ($operation->getUriVariables()) {
17✔
59
            $context['uri_variables'] = [];
3✔
60

61
            foreach (array_keys($operation->getUriVariables()) as $parameterName) {
3✔
62
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
3✔
63
            }
64
        }
65

66
        if (!$normalization) {
17✔
67
            if (!isset($context['api_allow_update'])) {
7✔
68
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
7✔
69

70
                if ($context['api_allow_update'] && 'PATCH' === $method) {
7✔
71
                    $context['deep_object_to_populate'] ??= true;
1✔
72
                }
73
            }
74

75
            if ('csv' === $request->getContentType()) {
7✔
76
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
77
            }
78
        }
79

80
        return $context;
17✔
81
    }
82
}
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