Skip to content

Commit 2e623a9

Browse files
committed
document property filter back
1 parent fd44e8f commit 2e623a9

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

core/filters.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $q
154154
}
155155
```
156156

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`.
158161
159162
### Restricting Properties with `:property` Placeholders
160163

@@ -244,7 +247,8 @@ class Book {
244247

245248
This approach is recommended for new filters as it's more flexible and allows true property restriction via the parameter configuration.
246249

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.
248252
249253
## OpenAPI and JSON Schema
250254

@@ -338,7 +342,8 @@ use Symfony\Component\Validator\Constraints as Assert;
338342
class User {}
339343
```
340344

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.
342347
343348
The `ApiPlatform\Validator\Util\ParameterValidationConstraints` trait can be used to automatically infer validation constraints from the JSON Schema and OpenAPI definitions of a parameter.
344349

@@ -396,6 +401,49 @@ class StrictParameters {}
396401

397402
With this configuration, a request to `/strict_query_parameters?bar=test` will fail with a 400 error because `bar` is not a supported parameter.
398403

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).
413+
414+
Syntax: `?properties[]=<property>&properties[<relation>][]=<property>`
415+
416+
You can add as many properties as you need.
417+
418+
Enable the filter:
419+
420+
```php
421+
<?php
422+
// api/src/Entity/Book.php
423+
namespace App\Entity;
424+
425+
use ApiPlatform\Metadata\ApiFilter;
426+
use ApiPlatform\Metadata\ApiResource;
427+
use ApiPlatform\Serializer\Filter\PropertyFilter;
428+
429+
#[ApiResource(
430+
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+
399447
## Parameter Providers
400448

401449
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

Comments
 (0)