[Laravel] OrFilter throws when an array query parameter has exactly one value
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 88/100
Línea de trabajo
Comienza en ApiPlatform\Laravel\Eloquent\Filter\OrFilter::apply() y compara el manejo de sus entradas con FilterQueryExtension::apply(). Reproduce el problema con uno y varios valores de tags y, después, verifica que ambas solicitudes filtren correctamente sin el error de foreach.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
API Platform version(s) affected: 4.3.19
Description
ApiPlatform\Laravel\Eloquent\Filter\OrFilter::apply() always iterates its $values argument with foreach ($values as $value).
ApiPlatform\Laravel\Eloquent\Extension\FilterQueryExtension::apply(), flattens a single-element list array down to a scalar before calling any filter:
// most eloquent filters work with only a single value
if (\is_array($values) && array_is_list($values) && 1 === \count($values)) {
$values = current($values);
}
That's true for most filters, which take a single value directly. It isn't true for OrFilter, which always expects an array to iterate. So a QueryParameter using OrFilter throws the moment a request supplies exactly one value for the array-typed parameter, instead of filtering on it.
How to reproduce
'tags' => new QueryParameter(
schema: ['type' => 'array', 'items' => ['type' => 'integer']],
filter: new OrFilter(new EqualsFilter),
property: 'tag_id',
),
GET /books?tags[]=1&tags[]=2works.GET /books?tags[]=1(a single value) throws:
ErrorException: foreach() argument must be of type array|object, string given
at vendor/api-platform/laravel/Eloquent/Filter/OrFilter.php:36
Possible Solution
Normalize the input before iterating in ApiPlatform\Laravel\Eloquent\Filter\OrFilter::apply():
return $builder->where(function (Builder $builder) use ($values, $parameter, $context): void {
foreach (\Illuminate\Support\Arr::wrap($values) as $value) {
$this->filter->apply($builder, $value, $parameter, ['whereClause' => 'orWhere'] + $context);
}
});
rather than relying on every caller of FilterQueryExtension to never send OrFilter a single value.
- Lenguaje dominante
- PHP
- Estrellas
- 2.6k
- Forks
- 982
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 67
Preparar el entorno
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de api-platform/core
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
api-platform/core#8573 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
api-platform/core#8564 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
api-platform/core#8495 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
api-platform/core#8471 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
api-platform/core#8475 ·
Los mantenedores suelen responder en 1 día
Todos los issues de api-platform/core
Issues similares
-
sync-en
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 2 días
-
P2 testing
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
Los mantenedores suelen responder en 1 día
-
1.severity: security
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Automattic/static-site-importer#1879 ·
Los mantenedores suelen responder en 1 día
-
bug Installation / Upgrade
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día