• 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

88.0
/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\Metadata\CollectionOperationInterface;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\Exception\RuntimeException;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Util\AttributesExtractor;
21
use ApiPlatform\State\SerializerContextBuilderInterface;
22
use ApiPlatform\State\Util\StateOptionsTrait;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\Serializer\Encoder\CsvEncoder;
25
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
26
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class SerializerContextBuilder implements SerializerContextBuilderInterface
34
{
35
    use StateOptionsTrait;
36

37
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly bool $debug = false)
38
    {
UNCOV
39
    }
801✔
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function createFromRequest(Request $request, bool $normalization, ?array $attributes = null): array
45
    {
UNCOV
46
        if (null === $attributes && !$attributes = AttributesExtractor::extractAttributes($request->attributes->all())) {
796✔
47
            throw new RuntimeException('Request attributes are not valid.');
×
48
        }
49

UNCOV
50
        if (!($operation = $attributes['operation'] ?? null)) {
796✔
51
            if (!$this->resourceMetadataFactory) {
×
52
                throw new RuntimeException('No operation');
×
53
            }
54

55
            $operation = $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name'] ?? null);
×
56
        }
57

UNCOV
58
        $context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
796✔
UNCOV
59
        $context['operation_name'] = $operation->getName();
796✔
UNCOV
60
        $context['operation'] = $operation;
796✔
UNCOV
61
        $context['resource_class'] = $attributes['resource_class'];
796✔
UNCOV
62
        $context['skip_null_values'] ??= true;
796✔
UNCOV
63
        $context['iri_only'] ??= false;
796✔
UNCOV
64
        $context['request_uri'] = $request->getRequestUri();
796✔
UNCOV
65
        $context['uri'] = $request->getUri();
796✔
UNCOV
66
        $context['input'] = $operation->getInput();
796✔
UNCOV
67
        $context['output'] = $operation->getOutput();
796✔
68

69
        // Special case as this is usually handled by our OperationContextTrait, here we want to force the IRI in the response
UNCOV
70
        if (!$operation instanceof CollectionOperationInterface && method_exists($operation, 'getItemUriTemplate') && $operation->getItemUriTemplate()) {
796✔
71
            $context['item_uri_template'] = $operation->getItemUriTemplate();
1✔
72
        }
73

UNCOV
74
        if ($types = $operation->getTypes()) {
796✔
75
            $context['types'] = $types;
13✔
76
        }
77

78
        // TODO: remove this as uri variables are available in the SerializerProcessor but correctly parsed
UNCOV
79
        if ($operation->getUriVariables()) {
796✔
UNCOV
80
            $context['uri_variables'] = [];
308✔
81

UNCOV
82
            foreach (array_keys($operation->getUriVariables()) as $parameterName) {
308✔
UNCOV
83
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
308✔
84
            }
85
        }
86

UNCOV
87
        if (null === $context['output'] && $this->getStateOptionsClass($operation)) {
796✔
UNCOV
88
            $context['force_resource_class'] = $operation->getClass();
5✔
89
        }
90

UNCOV
91
        if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
796✔
UNCOV
92
            if (!\is_array($context['groups'])) {
102✔
93
                $context['groups'] = (array) $context['groups'];
×
94
            }
95

UNCOV
96
            $context['groups'][] = 'trace';
102✔
97
        }
98

UNCOV
99
        if (!$normalization) {
796✔
UNCOV
100
            if (!isset($context['api_allow_update'])) {
187✔
UNCOV
101
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
187✔
102

UNCOV
103
                if ($context['api_allow_update'] && 'PATCH' === $method) {
187✔
104
                    $context['deep_object_to_populate'] ??= true;
9✔
105
                }
106
            }
107

UNCOV
108
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
187✔
109
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
1✔
110
            }
111
        }
UNCOV
112
        if ($operation->getCollectDenormalizationErrors() ?? false) {
796✔
113
            $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
×
114
        }
115

116
        // to keep the cache computation smaller, we have "operation_name" and "iri" anyways
UNCOV
117
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'root_operation';
796✔
UNCOV
118
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'operation';
796✔
UNCOV
119
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'object';
796✔
UNCOV
120
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'data';
796✔
UNCOV
121
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'property_metadata';
796✔
UNCOV
122
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'circular_reference_limit_counters';
796✔
UNCOV
123
        $context[AbstractObjectNormalizer::EXCLUDE_FROM_CACHE_KEY][] = 'debug_trace_id';
796✔
124

125
        // JSON API see JsonApiProvider
UNCOV
126
        if ($included = $request->attributes->get('_api_included')) {
796✔
127
            $context['api_included'] = $included;
16✔
128
        }
129

UNCOV
130
        return $context;
796✔
131
    }
132
}
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