Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Laravel] OrFilter throws when an array query parameter has exactly one value

Đã đóng Phù hợp với người mới
#8,588 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
88/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
laravel, php
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu trong ApiPlatform\Laravel\Eloquent\Filter\OrFilter::apply() và so sánh cách xử lý đầu vào của nó với FilterQueryExtension::apply(). Tái hiện vấn đề với một và nhiều giá trị tags, sau đó xác minh rằng cả hai request đều lọc thành công mà không gặp lỗi foreach.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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[]=2 works.
  • 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.

Ngôn ngữ chính
PHP
Star
2.6k
Fork
984
Merge trung bình
1 ngày 17 giờ
Pull request đã merge (30 ngày)
84

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của api-platform/core

Tất cả issue của api-platform/core

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.