You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core/filters.md
+51-3Lines changed: 51 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -154,7 +154,10 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
154
154
}
155
155
```
156
156
157
-
Note that we're using `api_platform.doctrine.orm.search_filter.instance` (exists also for ODM). Indeed this is a special instance of the search filter where `properties` can be changed during runtime. This is considered as "legacy filter" below, in API Platform 4.0 we'll recommend to create a custom filter or to use the `PartialSearchFilter`.
157
+
> [!NOTE]
158
+
> We are using `api_platform.doctrine.orm.search_filter.instance` (exists also for ODM).
159
+
> Indeed this is a special instance of the search filter where `properties` can be changed during runtime.
160
+
> This is considered as "legacy filter" below, in API Platform 4.0 we'll recommend to create a custom filter or to use the `PartialSearchFilter`.
158
161
159
162
### Restricting Properties with `:property` Placeholders
160
163
@@ -244,7 +247,8 @@ class Book {
244
247
245
248
This approach is recommended for new filters as it's more flexible and allows true property restriction via the parameter configuration.
246
249
247
-
Note that invalid values are usually ignored by our filters, use [validation](#parameter-validation) to trigger errors for wrong parameter values.
250
+
> [!NOTE]
251
+
> Invalid values are usually ignored by our filters, use [validation](#parameter-validation) to trigger errors for wrong parameter values.
248
252
249
253
## OpenAPI and JSON Schema
250
254
@@ -338,7 +342,8 @@ use Symfony\Component\Validator\Constraints as Assert;
338
342
class User {}
339
343
```
340
344
341
-
Note that when `castToNativeType` is enabled, API Platform infers type validation from the JSON Schema.
345
+
> [!NOTE]
346
+
> When `castToNativeType` is enabled, API Platform infers type validation from the JSON Schema.
342
347
343
348
The `ApiPlatform\Validator\Util\ParameterValidationConstraints` trait can be used to automatically infer validation constraints from the JSON Schema and OpenAPI definitions of a parameter.
344
349
@@ -396,6 +401,49 @@ class StrictParameters {}
396
401
397
402
With this configuration, a request to `/strict_query_parameters?bar=test` will fail with a 400 error because `bar` is not a supported parameter.
398
403
404
+
### Property filter
405
+
406
+
> [!NOTE]
407
+
> We strongly recommend using [Vulcain](https://vulcain.rocks) instead of this filter.
408
+
> Vulcain is faster, allows a better hit rate, and is supported out of the box in the API Platform distribution.
409
+
> [!NOTE]
410
+
> When unsing JSON:API check out the [specific SparseFieldset and Sort filters](./content-negotiation/#jsonapi-sparse-fieldset-and-sort-parameters)
411
+
412
+
The property filter adds the possibility to select the properties to serialize (sparse fieldsets).
parameters: ['properties' => new QueryParameter(filter: PropertyFilter::class)]
431
+
)]
432
+
class Book
433
+
{
434
+
// ...
435
+
}
436
+
```
437
+
438
+
Three arguments are available to configure the filter:
439
+
440
+
-`parameterName` is the query parameter name (default `properties`)
441
+
-`overrideDefaultProperties` allows to override the default serialization properties (default `false`)
442
+
-`whitelist` properties whitelist to avoid uncontrolled data exposure (default `null` to allow all properties)
443
+
444
+
Given that the collection endpoint is `/books`, you can filter the serialization properties with the following query: `/books?properties[]=title&properties[]=author`.
445
+
If you want to include some properties of the nested "author" document, use: `/books?properties[]=title&properties[author][]=name`.
446
+
399
447
## Parameter Providers
400
448
401
449
Parameter Providers are powerful services that can inspect, transform, or provide values for parameters. They can even modify the current `Operation` metadata on the fly. A provider is a class that implements `ApiPlatform\State\ParameterProviderInterface`.
0 commit comments