@@ -592,11 +592,9 @@ Symfony Validator constraints.
592592// api/src/Resource/User.php
593593use ApiPlatform\M etadata\A piResource;
594594use ApiPlatform\M etadata\G etCollection;
595- use ApiPlatform\M etadata\H eaderParameter;
596595use ApiPlatform\M etadata\Q ueryParameter;
596+ use ApiPlatform\M etadata\S calarHeaderParameter;
597597use Symfony\C omponent\V alidator\C onstraints as Assert;
598- use Symfony\C omponent\T ypeInfo\T ype\B uiltinType;
599- use Symfony\C omponent\T ypeInfo\T ypeIdentifier;
600598
601599#[ApiResource(operations: [
602600 new GetCollection(
@@ -606,11 +604,10 @@ use Symfony\Component\TypeInfo\TypeIdentifier;
606604 description: 'Filter by country code.',
607605 constraints: [new Assert\C ountry()]
608606 ),
609- 'X-Request-ID' => new HeaderParameter (
607+ 'X-Request-ID' => new ScalarHeaderParameter (
610608 description: 'A unique request identifier.',
611609 required: true,
612- constraints: [new Assert\U uid()],
613- nativeType: new BuiltinType(typeIdentifier: TypeIdentifier::STRING)
610+ constraints: [new Assert\U uid()]
614611 )
615612 ]
616613 )
@@ -621,8 +618,9 @@ class User {}
621618> [!NOTE] When `castToNativeType` is enabled, API Platform infers type validation from the JSON
622619> Schema.
623620
624- If `constraints` are used then a valid type needs to be passed using `nativeType` named argument.
625- Otherwise, the values will be passed as an array to each constraints.
621+ A `HeaderParameter` value is typed `string|list<string>` because a header can be repeated, so
622+ constraints receive an array. Use `ScalarHeaderParameter` (available since API Platform 5.1) to
623+ validate a single string value, or pass a `schema` or a `nativeType` yourself.
626624
627625The `ApiPlatform\Validator\Util\ParameterValidationConstraints` trait can be used to automatically
628626infer validation constraints from the JSON Schema and OpenAPI definitions of a parameter.
0 commit comments