From b527e63a0c5bb0a7131687bd2d4daa25e6801863 Mon Sep 17 00:00:00 2001 From: soyuka Date: Fri, 16 Oct 2026 10:51:12 +0200 Subject: [PATCH] docs(core): use ScalarHeaderParameter for header validation --- core/filters.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/core/filters.md b/core/filters.md index fbf442ee2c9..07ff9d5fc99 100644 --- a/core/filters.md +++ b/core/filters.md @@ -592,11 +592,9 @@ Symfony Validator constraints. // api/src/Resource/User.php use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\GetCollection; -use ApiPlatform\Metadata\HeaderParameter; use ApiPlatform\Metadata\QueryParameter; +use ApiPlatform\Metadata\ScalarHeaderParameter; use Symfony\Component\Validator\Constraints as Assert; -use Symfony\Component\TypeInfo\Type\BuiltinType; -use Symfony\Component\TypeInfo\TypeIdentifier; #[ApiResource(operations: [ new GetCollection( @@ -606,11 +604,10 @@ use Symfony\Component\TypeInfo\TypeIdentifier; description: 'Filter by country code.', constraints: [new Assert\Country()] ), - 'X-Request-ID' => new HeaderParameter( + 'X-Request-ID' => new ScalarHeaderParameter( description: 'A unique request identifier.', required: true, - constraints: [new Assert\Uuid()], - nativeType: new BuiltinType(typeIdentifier: TypeIdentifier::STRING) + constraints: [new Assert\Uuid()] ) ] ) @@ -621,8 +618,9 @@ class User {} > [!NOTE] When `castToNativeType` is enabled, API Platform infers type validation from the JSON > Schema. -If `constraints` are used then a valid type needs to be passed using `nativeType` named argument. -Otherwise, the values will be passed as an array to each constraints. +A `HeaderParameter` value is typed `string|list` because a header can be repeated, so +constraints receive an array. Use `ScalarHeaderParameter` (available since API Platform 5.1) to +validate a single string value, or pass a `schema` or a `nativeType` yourself. The `ApiPlatform\Validator\Util\ParameterValidationConstraints` trait can be used to automatically infer validation constraints from the JSON Schema and OpenAPI definitions of a parameter.