Skip to content

Commit e893575

Browse files
committed
document property filter back
1 parent fd44e8f commit e893575

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

core/filters.md

Lines changed: 53 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,51 @@ 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+
410+
411+
> [!NOTE]
412+
> When unsing JSON:API check out the [specific SparseFieldset and Sort filters](./content-negotiation/#jsonapi-sparse-fieldset-and-sort-parameters)
413+
414+
The property filter adds the possibility to select the properties to serialize (sparse fieldsets).
415+
416+
Syntax: `?properties[]=<property>&properties[<relation>][]=<property>`
417+
418+
You can add as many properties as you need.
419+
420+
Enable the filter:
421+
422+
```php
423+
<?php
424+
// api/src/Entity/Book.php
425+
namespace App\Entity;
426+
427+
use ApiPlatform\Metadata\ApiFilter;
428+
use ApiPlatform\Metadata\ApiResource;
429+
use ApiPlatform\Serializer\Filter\PropertyFilter;
430+
431+
#[ApiResource(
432+
parameters: ['properties' => new QueryParameter(filter: PropertyFilter::class)]
433+
)]
434+
class Book
435+
{
436+
// ...
437+
}
438+
```
439+
440+
Three arguments are available to configure the filter:
441+
442+
- `parameterName` is the query parameter name (default `properties`)
443+
- `overrideDefaultProperties` allows to override the default serialization properties (default `false`)
444+
- `whitelist` properties whitelist to avoid uncontrolled data exposure (default `null` to allow all properties)
445+
446+
Given that the collection endpoint is `/books`, you can filter the serialization properties with the following query: `/books?properties[]=title&properties[]=author`.
447+
If you want to include some properties of the nested "author" document, use: `/books?properties[]=title&properties[author][]=name`.
448+
399449
## Parameter Providers
400450

401451
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)