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

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 hits per line

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

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

27
/**
28
 * {@inheritdoc}
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
final class SerializerContextBuilder implements SerializerContextBuilderInterface
33
{
34
    public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly bool $debug = false)
35
    {
UNCOV
36
    }
1,854✔
37

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

UNCOV
47
        if (!($operation = $attributes['operation'] ?? null)) {
1,845✔
48
            if (!$this->resourceMetadataFactory) {
×
49
                throw new RuntimeException('No operation');
×
50
            }
51

52
            $operation = $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name'] ?? null);
×
53
        }
54

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

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

UNCOV
71
        if ($types = $operation->getTypes()) {
1,845✔
UNCOV
72
            $context['types'] = $types;
221✔
73
        }
74

75
        // TODO: remove this as uri variables are available in the SerializerProcessor but correctly parsed
UNCOV
76
        if ($operation->getUriVariables()) {
1,845✔
UNCOV
77
            $context['uri_variables'] = [];
760✔
78

UNCOV
79
            foreach (array_keys($operation->getUriVariables()) as $parameterName) {
760✔
UNCOV
80
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
760✔
81
            }
82
        }
83

UNCOV
84
        if (null === $context['output'] && ($options = $operation?->getStateOptions()) && class_exists(Options::class) && $options instanceof Options && $options->getEntityClass()) {
1,845✔
85
            $context['force_resource_class'] = $operation->getClass();
9✔
86
        }
87

UNCOV
88
        if ($this->debug && isset($context['groups']) && $operation instanceof ErrorOperation) {
1,845✔
UNCOV
89
            if (!\is_array($context['groups'])) {
236✔
90
                $context['groups'] = (array) $context['groups'];
×
91
            }
92

UNCOV
93
            $context['groups'][] = 'trace';
236✔
94
        }
95

UNCOV
96
        if (!$normalization) {
1,845✔
UNCOV
97
            if (!isset($context['api_allow_update'])) {
589✔
UNCOV
98
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
589✔
99

UNCOV
100
                if ($context['api_allow_update'] && 'PATCH' === $method) {
589✔
UNCOV
101
                    $context['deep_object_to_populate'] ??= true;
30✔
102
                }
103
            }
104

UNCOV
105
            if ('csv' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType())) {
589✔
UNCOV
106
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
3✔
107
            }
108
        }
UNCOV
109
        if ($operation->getCollectDenormalizationErrors() ?? false) {
1,845✔
110
            $context[DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS] = true;
1✔
111
        }
112

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

122
        // JSON API see JsonApiProvider
UNCOV
123
        if ($included = $request->attributes->get('_api_included')) {
1,845✔
UNCOV
124
            $context['api_included'] = $included;
48✔
125
        }
126

UNCOV
127
        return $context;
1,845✔
128
    }
129
}
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