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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

63.24
/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 Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19

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

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

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

138
        if (!\is_array($properties)) {
20✔
139
            return;
20✔
140
        }
141

142
        $properties = $this->denormalizeProperties($properties);
×
143

144
        if (null !== $this->whitelist) {
×
145
            $properties = $this->getProperties($properties, $this->whitelist);
×
146
        }
147

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

152
        $context[AbstractNormalizer::ATTRIBUTES] = $properties;
×
153
    }
154

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

164
        return [
28✔
165
            "$this->parameterName[]" => [
28✔
166
                'property' => null,
28✔
167
                'type' => 'string',
28✔
168
                'is_collection' => true,
28✔
169
                'required' => false,
28✔
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,
28✔
171
                'swagger' => [
28✔
172
                    '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,
28✔
173
                    'name' => "$this->parameterName[]",
28✔
174
                    'type' => 'array',
28✔
175
                    'items' => [
28✔
176
                        'type' => 'string',
28✔
177
                    ],
28✔
178
                ],
28✔
179
                'openapi' => [
28✔
180
                    '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,
28✔
181
                    'name' => "$this->parameterName[]",
28✔
182
                    'schema' => [
28✔
183
                        'type' => 'array',
28✔
184
                        'items' => [
28✔
185
                            'type' => 'string',
28✔
186
                        ],
28✔
187
                    ],
28✔
188
                ],
28✔
189
            ],
28✔
190
        ];
28✔
191
    }
192

193
    /**
194
     * Generate an array of whitelist properties to match the format that properties
195
     * will have in the request.
196
     *
197
     * @param array $whitelist the whitelist to format
198
     *
199
     * @return array An array containing the whitelist ready to match request parameters
200
     */
201
    private function formatWhitelist(array $whitelist): array
202
    {
203
        if (array_values($whitelist) === $whitelist) {
20✔
204
            return $whitelist;
20✔
205
        }
206
        foreach ($whitelist as $name => $value) {
20✔
207
            if (null === $value) {
20✔
208
                unset($whitelist[$name]);
20✔
209
                $whitelist[] = $name;
20✔
210
            }
211
        }
212

213
        return $whitelist;
20✔
214
    }
215

216
    private function getProperties(array $properties, array $whitelist = null): array
217
    {
218
        $whitelist ??= $this->whitelist;
×
219
        $result = [];
×
220

221
        foreach ($properties as $key => $value) {
×
222
            if (is_numeric($key)) {
×
223
                if (\in_array($propertyName = $this->denormalizePropertyName($value), $whitelist, true)) {
×
224
                    $result[] = $propertyName;
×
225
                }
226

227
                continue;
×
228
            }
229

230
            if (\is_array($value) && isset($whitelist[$key]) && $recursiveResult = $this->getProperties($value, $whitelist[$key])) {
×
231
                $result[$this->denormalizePropertyName($key)] = $recursiveResult;
×
232
            }
233
        }
234

235
        return $result;
×
236
    }
237

238
    private function denormalizeProperties(array $properties): array
239
    {
240
        if (null === $this->nameConverter || !$properties) {
×
241
            return $properties;
×
242
        }
243

244
        $result = [];
×
245
        foreach ($properties as $key => $value) {
×
246
            $result[$this->denormalizePropertyName((string) $key)] = \is_array($value) ? $this->denormalizeProperties($value) : $this->denormalizePropertyName($value);
×
247
        }
248

249
        return $result;
×
250
    }
251

252
    private function denormalizePropertyName($property): string
253
    {
254
        return null !== $this->nameConverter ? $this->nameConverter->denormalize($property) : $property;
×
255
    }
256
}
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

© 2026 Coveralls, Inc