Skip to content

Minor inconsistency in ExactFilter when filtering integer property compared to SearchFilter behavioir in the Swagger UI doc #8406

Description

@ES-Six

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

Image

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

Image

For example 4 (2 arrays filters are generated with an inconsistent query parameter key) :

Image

Expected result (like the old SearchFilter does) :

Image

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions