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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 hits per line

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

96.67
/src/Serializer/Filter/PropertyFilter.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\Filter;
15

16
use ApiPlatform\OpenApi\Model\Parameter;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
20

21
/**
22
 * The property filter adds the possibility to select the properties to serialize (sparse fieldsets).
23
 *
24
 * Note: We strongly recommend using [Vulcain](https://vulcain.rocks/) instead of this filter. Vulcain is faster, allows a better hit rate, and is supported out of the box in the API Platform distribution.
25
 *
26
 * Syntax: `?properties[]=<property>&properties[<relation>][]=<property>`.
27
 *
28
 * You can add as many properties as you need.
29
 *
30
 * Three arguments are available to configure the filter:
31
 * - `parameterName` is the query parameter name (default: `properties`)
32
 * - `overrideDefaultProperties` allows to override the default serialization properties (default: `false`)
33
 * - `whitelist` properties whitelist to avoid uncontrolled data exposure (default: `null` to allow all properties)
34
 *
35
 * <div data-code-selector>
36
 *
37
 * ```php
38
 * <?php
39
 * // api/src/Entity/Book.php
40
 * use ApiPlatform\Metadata\ApiFilter;
41
 * use ApiPlatform\Metadata\ApiResource;
42
 * use ApiPlatform\Serializer\Filter\PropertyFilter;
43
 *
44
 * #[ApiResource]
45
 * #[ApiFilter(PropertyFilter::class, arguments: ['parameterName' => 'properties', 'overrideDefaultProperties' => false, 'whitelist' => ['allowed_property']])]
46
 * class Book
47
 * {
48
 *     // ...
49
 * }
50
 * ```
51
 *
52
 * ```yaml
53
 * # config/services.yaml
54
 * services:
55
 *     book.property_filter:
56
 *         parent: 'api_platform.serializer.property_filter'
57
 *         arguments: [ $parameterName: 'properties', $overrideDefaultGroups: false, $whitelist: ['allowed_property'] ]
58
 *         tags:  [ 'api_platform.filter' ]
59
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
60
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
61
 *         autowire: false
62
 *         autoconfigure: false
63
 *         public: false
64
 *
65
 * # api/config/api_platform/resources.yaml
66
 * resources:
67
 *     App\Entity\Book:
68
 *         - operations:
69
 *               ApiPlatform\Metadata\GetCollection:
70
 *                   filters: ['book.property_filter']
71
 * ```
72
 *
73
 * ```xml
74
 * <?xml version="1.0" encoding="UTF-8" ?>
75
 * <!-- api/config/services.xml -->
76
 * <?xml version="1.0" encoding="UTF-8" ?>
77
 * <container
78
 *         xmlns="http://symfony.com/schema/dic/services"
79
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
80
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
81
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
82
 *     <services>
83
 *         <service id="book.property_filter" parent="api_platform.serializer.property_filter">
84
 *             <argument key="parameterName">properties</argument>
85
 *             <argument key="overrideDefaultGroups">false</argument>
86
 *             <argument key="whitelist" type="collection">
87
 *                 <argument>allowed_property</argument>
88
 *             </argument>
89
 *             <tag name="api_platform.filter"/>
90
 *         </service>
91
 *     </services>
92
 * </container>
93
 * <!-- api/config/api_platform/resources.xml -->
94
 * <resources
95
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
96
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
97
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
98
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
99
 *     <resource class="App\Entity\Book">
100
 *         <operations>
101
 *             <operation class="ApiPlatform\Metadata\GetCollection">
102
 *                 <filters>
103
 *                     <filter>book.property_filter</filter>
104
 *                 </filters>
105
 *             </operation>
106
 *         </operations>
107
 *     </resource>
108
 * </resources>
109
 * ```
110
 *
111
 * </div>
112
 *
113
 * Given that the collection endpoint is `/books`, you can filter the serialization properties with the following query: `/books?properties[]=title&properties[]=author`. If you want to include some properties of the nested "author" document, use: `/books?properties[]=title&properties[author][]=name`.
114
 *
115
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
116
 */
117
final class PropertyFilter implements FilterInterface
118
{
119
    private ?array $whitelist;
120

121
    public function __construct(private readonly string $parameterName = 'properties', private readonly bool $overrideDefaultProperties = false, ?array $whitelist = null, private readonly ?NameConverterInterface $nameConverter = null)
122
    {
UNCOV
123
        $this->whitelist = null === $whitelist ? null : $this->formatWhitelist($whitelist);
949✔
124
    }
125

126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function apply(Request $request, bool $normalization, array $attributes, array &$context): void
130
    {
UNCOV
131
        if (null !== $propertyAttribute = $request->attributes->get('_api_filter_property')) {
505✔
UNCOV
132
            $properties = $propertyAttribute;
9✔
UNCOV
133
        } elseif (\array_key_exists($this->parameterName, $commonAttribute = $request->attributes->get('_api_filters', []))) {
496✔
134
            $properties = $commonAttribute[$this->parameterName];
×
135
        } else {
UNCOV
136
            $properties = $request->query->all()[$this->parameterName] ?? null;
496✔
137
        }
138

UNCOV
139
        if (!\is_array($properties)) {
505✔
UNCOV
140
            return;
483✔
141
        }
142

UNCOV
143
        $properties = $this->denormalizeProperties($properties);
55✔
144

UNCOV
145
        if (null !== $this->whitelist) {
55✔
UNCOV
146
            $properties = $this->getProperties($properties, $this->whitelist);
18✔
147
        }
148

UNCOV
149
        if (!$this->overrideDefaultProperties && isset($context[AbstractNormalizer::ATTRIBUTES])) {
55✔
UNCOV
150
            $properties = array_merge_recursive((array) $context[AbstractNormalizer::ATTRIBUTES], $properties);
9✔
151
        }
152

UNCOV
153
        $context[AbstractNormalizer::ATTRIBUTES] = $properties;
55✔
154
    }
155

156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getDescription(string $resourceClass): array
160
    {
UNCOV
161
        $example = \sprintf('%1$s[]={propertyName}&%1$s[]={anotherPropertyName}&%1$s[{nestedPropertyParent}][]={nestedProperty}',
672✔
UNCOV
162
            $this->parameterName
672✔
UNCOV
163
        );
672✔
164

UNCOV
165
        return [
672✔
UNCOV
166
            "$this->parameterName[]" => [
672✔
UNCOV
167
                'type' => 'string',
672✔
UNCOV
168
                'is_collection' => true,
672✔
UNCOV
169
                'required' => false,
672✔
UNCOV
170
                'description' => 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example,
672✔
UNCOV
171
                'openapi' => new Parameter(
672✔
UNCOV
172
                    in: 'query',
672✔
UNCOV
173
                    name: "$this->parameterName[]",
672✔
UNCOV
174
                    description: 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example,
672✔
UNCOV
175
                    schema: [
672✔
UNCOV
176
                        'type' => 'array',
672✔
UNCOV
177
                        'items' => [
672✔
UNCOV
178
                            'type' => 'string',
672✔
UNCOV
179
                        ],
672✔
UNCOV
180
                    ]
672✔
UNCOV
181
                ),
672✔
UNCOV
182
            ],
672✔
UNCOV
183
        ];
672✔
184
    }
185

186
    /**
187
     * Generate an array of whitelist properties to match the format that properties
188
     * will have in the request.
189
     *
190
     * @param array $whitelist the whitelist to format
191
     *
192
     * @return array An array containing the whitelist ready to match request parameters
193
     */
194
    private function formatWhitelist(array $whitelist): array
195
    {
UNCOV
196
        if (array_values($whitelist) === $whitelist) {
512✔
UNCOV
197
            return $whitelist;
512✔
198
        }
UNCOV
199
        foreach ($whitelist as $name => $value) {
512✔
UNCOV
200
            if (null === $value) {
512✔
UNCOV
201
                unset($whitelist[$name]);
512✔
UNCOV
202
                $whitelist[] = $name;
512✔
203
            }
204
        }
205

UNCOV
206
        return $whitelist;
512✔
207
    }
208

209
    private function getProperties(array $properties, ?array $whitelist = null): array
210
    {
UNCOV
211
        $whitelist ??= $this->whitelist;
18✔
UNCOV
212
        $result = [];
18✔
213

UNCOV
214
        foreach ($properties as $key => $value) {
18✔
UNCOV
215
            if (is_numeric($key)) {
18✔
UNCOV
216
                if (\in_array($propertyName = $this->denormalizePropertyName($value), $whitelist, true)) {
18✔
UNCOV
217
                    $result[] = $propertyName;
9✔
218
                }
219

UNCOV
220
                continue;
18✔
221
            }
222

UNCOV
223
            if (\is_array($value) && isset($whitelist[$key]) && $recursiveResult = $this->getProperties($value, $whitelist[$key])) {
9✔
UNCOV
224
                $result[$this->denormalizePropertyName($key)] = $recursiveResult;
3✔
225
            }
226
        }
227

UNCOV
228
        return $result;
18✔
229
    }
230

231
    private function denormalizeProperties(array $properties): array
232
    {
UNCOV
233
        if (null === $this->nameConverter || !$properties) {
55✔
234
            return $properties;
×
235
        }
236

UNCOV
237
        $result = [];
55✔
UNCOV
238
        foreach ($properties as $key => $value) {
55✔
UNCOV
239
            $result[$this->denormalizePropertyName((string) $key)] = \is_array($value) ? $this->denormalizeProperties($value) : $this->denormalizePropertyName($value);
55✔
240
        }
241

UNCOV
242
        return $result;
55✔
243
    }
244

245
    private function denormalizePropertyName($property): string
246
    {
UNCOV
247
        return null !== $this->nameConverter ? $this->nameConverter->denormalize($property) : $property;
55✔
248
    }
249
}
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