Hello API Platform team,
API Platform version(s) affected: 4.3
Description
When using an ExactFilter, for integer property filtering, it's displayed as string type in the swagger doc. However, boolean filter (when providing a schema) and string filter are correctly displayed.
How to reproduce
Declare an ExactFilter with the following tested codes that doesn't work as intended :
// Example 1 :
parameters: [
'id' => new QueryParameter(filter: new ExactFilter(), property: 'id', schema: ["type" => "integer"])
]
// Example 2 :
parameters: [
'id' => new QueryParameter(filter: new ExactFilter(), property: 'id', schema: ["type" => "integer"], castToNativeType: true)
]
// Example 3 :
parameters: [
'id' => new QueryParameter(filter: new ExactFilter(), property: 'id', schema: ["type" => "integer"], castToNativeType: true, castToArray: true)
]
// Example 4 :
parameters: [
'id' => new QueryParameter(filter: new ExactFilter(), property: 'id', schema: ['type' => 'array', 'items' => ['type' => 'integer']], castToNativeType: true)
]
Inconsistent result observed in the swagger doc :
For example 1, example 2 (missing the array version of the filter as ExactFilter should create one automatically in this case) :
For example 3 (as castToArray true is enabled, this should produce a single array filter entry in the doc but it produces a single value filter ) :
For example 4 (2 arrays filters are generated with an inconsistent query parameter key) :
Expected result (like the old SearchFilter does) :
It should be possible to reproduce the SearchFilter behavior by specifying the schema: ["type" => "integer"] as we can do it for boolean properties.
Possible Solution
I think the bug location is inOpenApiFilterTrait but I'm unsure :
trait OpenApiFilterTrait
{
public function getOpenApiParameters(Parameter $parameter): OpenApiParameter|array|null
{
$schema = $parameter->getSchema();
// ---> The bug may be here as if a schema is provided to a Parameter using the ExactFilter, even when castToArray is true the trait will generate only one new OpenApiParameter
// Important : I'm unsure as this could be intended for another Filter, I don't know the whole context, beware to not introduce regressions
if (false === $parameter->getCastToArray() || (isset($schema['type']) && 'array' !== $schema['type'])) {
return new OpenApiParameter(name: $parameter->getKey(), in: 'query');
}
if ('array' === ($schema['type'] ?? null)) {
$arraySchema = $schema;
} else {
$arraySchema = ['type' => 'array', 'items' => $schema ?? ['type' => 'string']];
}
$arrayParameter = new OpenApiParameter(name: $parameter->getKey().'[]', in: 'query', style: 'deepObject', explode: true, schema: $arraySchema);
// When castToArray is null (default), both singular and array forms are accepted
if (null === $parameter->getCastToArray()) {
return [
new OpenApiParameter(name: $parameter->getKey(), in: 'query'),
$arrayParameter,
];
}
return $arrayParameter;
}
}
I suspect a bug in this Trait as the portion of code if (null === $parameter->getCastToArray()) {...} indicate the intention to allow generating the 2 filters in the Swagger doc when castToArray remains null but because of (isset($schema['type']) && 'array' !== $schema['type']) it will never reach the intended code when a schema is provided.
Additional Context
The type inconsistency concern only the swagger doc as the filter works well even when type "string" is displayed instead of "integer".
Hello API Platform team,
API Platform version(s) affected: 4.3
Description
When using an ExactFilter, for integer property filtering, it's displayed as string type in the swagger doc. However, boolean filter (when providing a schema) and string filter are correctly displayed.
How to reproduce
Declare an ExactFilter with the following tested codes that doesn't work as intended :
Inconsistent result observed in the swagger doc :
For example 1, example 2 (missing the array version of the filter as ExactFilter should create one automatically in this case) :
For example 3 (as castToArray true is enabled, this should produce a single array filter entry in the doc but it produces a single value filter ) :
For example 4 (2 arrays filters are generated with an inconsistent query parameter key) :
Expected result (like the old SearchFilter does) :
It should be possible to reproduce the SearchFilter behavior by specifying the schema: ["type" => "integer"] as we can do it for boolean properties.
Possible Solution
I think the bug location is in
OpenApiFilterTraitbut I'm unsure :I suspect a bug in this Trait as the portion of code
if (null === $parameter->getCastToArray()) {...}indicate the intention to allow generating the 2 filters in the Swagger doc when castToArray remains null but because of(isset($schema['type']) && 'array' !== $schema['type'])it will never reach the intended code when a schema is provided.Additional Context
The type inconsistency concern only the swagger doc as the filter works well even when type "string" is displayed instead of "integer".