OpenApi style=form not possible anymore
API Platform version(s) affected: 4.2.6
Description
After an upgrade to api-platform/core 4.2.6 our openapi query parameter definitions don't work as expected anymore
We get a validation error with style=form
How to reproduce
parameters: [
'tags' => new QueryParameter(
key: 'tags',
schema: [
'type' => 'array',
'items' => [
'type' => 'string',
'uniqueItems' => true,
'enum' => ['A', 'B', 'C'],
],
],
openApi: new Parameter(
'tags',
'query',
description: 'Only return activities with the given comma-seperated tag(s)',
required: false,
style: 'form',
explode: false,
example: 'A,B
),
nativeType: new BuiltinType(TypeIdentifier::ARRAY),
),
],
This is called with ?tags=A,B and throws this exception:
request.DEBUG: Uncaught PHP Exception ApiPlatform\Validator\Exception\ValidationException: "tags: This value should be of type array." at ParameterValidatorProvider.php line 98 {"exception":"[object] (ApiPlatform\\Validator\\Exception\\ValidationException(code: 0): tags: This value should be of type array. at .../vendor/api-platform/core/src/Symfony/Validator/State/ParameterValidatorProvider.php:98)
Possible Solution
Is there a way to get style=form serialization?
Ok, got it working by moving the schema from the QueryParameter to the Parameter. and changing the native type to string. Can someone explain this?
parameters: [
'tags' => new QueryParameter(
key: 'tags',
openApi: new Parameter(
'tags',
'query',
description: 'Only return reasons with the given comma-seperated tag(s)',
required: false,
schema: [
'type' => 'array',
'items' => [
'type' => 'string',
'uniqueItems' => true,
'enum' => ['carhouse', 'roll_shelf', 'roll_shelf_deliver', 'roll_shelf_pickup'],
],
],
style: 'form',
explode: false,
example: 'carhouse,roll_shelf',
),
nativeType: new BuiltinType(TypeIdentifier::STRING),
),
],
Hi! Indeed the parameter native php type infers from your json schema. It's a very interesting use case, maybe that there's a bug and that with the form style we should force a nativeType to string + castToArray to false? Or even better we could parse that form style, how is your filter working ? could you share that?
https://github.com/api-platform/core/blob/main/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php#L156