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

api-platform / core / 6690762601

30 Oct 2023 09:24AM UTC coverage: 67.319% (+0.02%) from 67.301%
6690762601

push

github

web-flow
ci: fix PHPUNIT (#5907)

* ci: fix phpunit


???

* Unset handler_id for symfony 6.3+

* Fix serializer configuration for PHP 8.1 (dev)

* Fix https://github.com/api-platform/api-platform/issues/2437

* Fix excepted deprecation in swagger


Trigger deprecation to fit tests. Can change test if needed


forgot semicolon


try fix deprecation

* remove copied WebTestCase to fix 8.1 dev

PR https://github.com/symfony/symfony/pull/32207 got merged

* fix no deprecation

* try tag legacy to valide


add a bc layer for reworked profiler UI

* fix warning about deprecated method


ensure method exists

* skip an exceptDeprecation, this case fails for a particular CI run

* remove uneccesary changes

* change BC deprecation system for doctrine

* fix some deprecations about validation html mode and attributes for recent sf

* fix doctrine lexer deprecations

* fix bootstrap missing

* improve doc for sf 6


f


Fix tiny bug & deprecation


--

* fix possible deprecation on 7.4

* Update ci.yml

* Update ci.yml

15610 of 23188 relevant lines covered (67.32%)

10.87 hits per line

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

86.67
/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\Core\Api\OperationType;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
19
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
20
use ApiPlatform\Exception\RuntimeException;
21
use ApiPlatform\Metadata\CollectionOperationInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Util\RequestAttributesExtractor;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Serializer\Encoder\CsvEncoder;
26

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

36
    public function __construct($resourceMetadataFactory)
37
    {
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
31✔
39

40
        if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
31✔
41
            trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
6✔
42
        }
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function createFromRequest(Request $request, bool $normalization, array $attributes = null): array
49
    {
50
        if (null === $attributes && !$attributes = RequestAttributesExtractor::extractAttributes($request)) {
21✔
51
            throw new RuntimeException('Request attributes are not valid.');
2✔
52
        }
53

54
        // TODO remove call to getContentType() when requiring symfony/http-foundation ≥ 6.2
55
        $contentTypeFormat = method_exists($request, 'getContentTypeFormat')
19✔
56
            ? $request->getContentTypeFormat()
19✔
57
            : $request->getContentType();
×
58

59
        // TODO: 3.0 change the condition to remove the ResourceMetadataFactorym only used to skip null values
60
        if (
61
            $this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface
19✔
62
            && (isset($attributes['operation_name']) || isset($attributes['operation']))
19✔
63
        ) {
64
            $operation = $attributes['operation'] ?? $this->resourceMetadataFactory->create($attributes['resource_class'])->getOperation($attributes['operation_name']);
15✔
65
            $context = $normalization ? ($operation->getNormalizationContext() ?? []) : ($operation->getDenormalizationContext() ?? []);
15✔
66
            $context['operation_name'] = $operation->getName();
15✔
67
            $context['operation'] = $operation;
15✔
68
            $context['resource_class'] = $attributes['resource_class'];
15✔
69
            // TODO: 3.0 becomes true by default
70
            $context['skip_null_values'] = $context['skip_null_values'] ?? $this->shouldSkipNullValues($attributes['resource_class'], $context['operation_name']);
15✔
71
            // TODO: remove in 3.0, operation type will not exist anymore
72
            $context['operation_type'] = $operation instanceof CollectionOperationInterface ? OperationType::COLLECTION : OperationType::ITEM;
15✔
73
            $context['iri_only'] = $context['iri_only'] ?? false;
15✔
74
            $context['request_uri'] = $request->getRequestUri();
15✔
75
            $context['uri'] = $request->getUri();
15✔
76
            $context['input'] = $operation->getInput();
15✔
77
            $context['output'] = $operation->getOutput();
15✔
78

79
            // Special case as this is usually handled by our OperationContextTrait, here we want to force the IRI in the response
80
            if (!$operation instanceof CollectionOperationInterface && method_exists($operation, 'getItemUriTemplate') && $operation->getItemUriTemplate()) {
15✔
81
                $context['item_uri_template'] = $operation->getItemUriTemplate();
×
82
            }
83

84
            $context['types'] = $operation->getTypes();
15✔
85
            $context['uri_variables'] = [];
15✔
86

87
            foreach (array_keys($operation->getUriVariables() ?? []) as $parameterName) {
15✔
88
                $context['uri_variables'][$parameterName] = $request->attributes->get($parameterName);
3✔
89
            }
90

91
            if (!$normalization) {
15✔
92
                if (!isset($context['api_allow_update'])) {
5✔
93
                    $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
5✔
94

95
                    if ($context['api_allow_update'] && 'PATCH' === $method) {
5✔
96
                        $context['deep_object_to_populate'] = $context['deep_object_to_populate'] ?? true;
×
97
                    }
98
                }
99

100
                if ('csv' === $contentTypeFormat) {
5✔
101
                    $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
102
                }
103
            }
104

105
            return $context;
15✔
106
        }
107

108
        /** @var ResourceMetadata $resourceMetadata */
109
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
4✔
110
        $key = $normalization ? 'normalization_context' : 'denormalization_context';
4✔
111
        if (isset($attributes['collection_operation_name'])) {
4✔
112
            $operationKey = 'collection_operation_name';
2✔
113
            $operationType = OperationType::COLLECTION;
2✔
114
        } elseif (isset($attributes['item_operation_name'])) {
4✔
115
            $operationKey = 'item_operation_name';
4✔
116
            $operationType = OperationType::ITEM;
4✔
117
        } else {
118
            $operationKey = 'subresource_operation_name';
2✔
119
            $operationType = OperationType::SUBRESOURCE;
2✔
120
        }
121

122
        $context = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], $key, [], true);
4✔
123
        $context['operation_type'] = $operationType;
4✔
124
        $context[$operationKey] = $attributes[$operationKey];
4✔
125
        $context['iri_only'] = $resourceMetadata->getAttribute('normalization_context')['iri_only'] ?? false;
4✔
126
        $context['input'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'input', null, true);
4✔
127
        $context['output'] = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], 'output', null, true);
4✔
128

129
        if (!$normalization) {
4✔
130
            if (!isset($context['api_allow_update'])) {
4✔
131
                $context['api_allow_update'] = \in_array($method = $request->getMethod(), ['PUT', 'PATCH'], true);
4✔
132

133
                if ($context['api_allow_update'] && 'PATCH' === $method) {
4✔
134
                    $context['deep_object_to_populate'] = $context['deep_object_to_populate'] ?? true;
2✔
135
                }
136
            }
137

138
            if ('csv' === $contentTypeFormat) {
4✔
139
                $context[CsvEncoder::AS_COLLECTION_KEY] = false;
×
140
            }
141
        }
142

143
        $context['resource_class'] = $attributes['resource_class'];
4✔
144
        $context['request_uri'] = $request->getRequestUri();
4✔
145
        $context['uri'] = $request->getUri();
4✔
146

147
        if (isset($attributes['subresource_context'])) {
4✔
148
            $context['subresource_identifiers'] = [];
2✔
149

150
            foreach ($attributes['subresource_context']['identifiers'] as $parameterName => [$resourceClass]) {
2✔
151
                if (!isset($context['subresource_resources'][$resourceClass])) {
2✔
152
                    $context['subresource_resources'][$resourceClass] = [];
2✔
153
                }
154

155
                $context['subresource_identifiers'][$parameterName] = $context['subresource_resources'][$resourceClass][$parameterName] = $request->attributes->get($parameterName);
2✔
156
            }
157
        }
158

159
        if (isset($attributes['subresource_property'])) {
4✔
160
            $context['subresource_property'] = $attributes['subresource_property'];
×
161
            $context['subresource_resource_class'] = $attributes['subresource_resource_class'] ?? null;
×
162
        }
163

164
        unset($context[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]);
4✔
165

166
        if (isset($context['skip_null_values'])) {
4✔
167
            return $context;
×
168
        }
169

170
        // TODO: We should always use `skip_null_values` but changing this would be a BC break, for now use it only when `merge-patch+json` is activated on a Resource
171
        if (!$this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
4✔
172
            foreach ($resourceMetadata->getItemOperations() as $operation) {
4✔
173
                if ('PATCH' === ($operation['method'] ?? '') && \in_array('application/merge-patch+json', $operation['input_formats']['json'] ?? [], true)) {
2✔
174
                    $context['skip_null_values'] = true;
2✔
175

176
                    break;
2✔
177
                }
178
            }
179
        } else {
180
            $context['skip_null_values'] = $this->shouldSkipNullValues($attributes['resource_class'], $attributes['operation_name']);
×
181
        }
182

183
        return $context;
4✔
184
    }
185

186
    /**
187
     * TODO: remove in 3.0, this will have no impact and skip_null_values will be default, no more resourceMetadataFactory call in this class.
188
     */
189
    private function shouldSkipNullValues(string $class, string $operationName): bool
190
    {
191
        if (!$this->resourceMetadataFactory) {
15✔
192
            return false;
×
193
        }
194

195
        $collection = $this->resourceMetadataFactory->create($class);
15✔
196
        foreach ($collection as $metadata) {
15✔
197
            foreach ($metadata->getOperations() as $operation) {
15✔
198
                if ('PATCH' === ($operation->getMethod() ?? '') && \in_array('application/merge-patch+json', $operation->getInputFormats()['json'] ?? [], true)) {
15✔
199
                    return true;
×
200
                }
201
            }
202
        }
203

204
        return false;
15✔
205
    }
206
}
207

208
class_alias(SerializerContextBuilder::class, \ApiPlatform\Core\Serializer\SerializerContextBuilder::class);
×
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