Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] in is allowed operator for array field but does not filter for list values #525

Open
Techn1x opened this issue Dec 11, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Techn1x
Copy link

Techn1x commented Dec 11, 2024

Summary / Steps to reproduce

I've got a flop schema like this, with a field that is a string array

@derive {
    Flop.Schema,
    filterable: [
      :subscription_types
    ],
    ...

schema "school_classes" do
    field :subscription_types, {:array, :string}

Flop tells me that in is an allowed operator

iex(8)> Flop.Filter.allowed_operators(SchoolClass, :subscription_types)
[:==, :!=, :empty, :not_empty, :<=, :<, :>=, :>, :in, :not_in, :contains, :not_contains]

So I construct a filter like this, to filter records to only those that have reading or maths in their subscription_types. Flop tells me this filter is valid

iex(8)> Flop.validate(%{ filters: [%{ field: :subscription_types, op: :in, value: ["reading", "maths"] }] })
{:ok, %Flop{ ... }

But when I apply it, Flop tells me its invalid

Flop.validate_and_run(
  query,
  %{ filters: [%{ field: :subscription_types, op: :in, value: ["reading", "maths"] }] },
  for: SchoolClass
)
Invalid Flop: #Ecto.Changeset<action: :replace, changes: %{filters: [#Ecto.Changeset<action: :insert, changes: %{op: :in, field: :subscription_types}, errors: [value: {"is invalid", []}], data: #Flop.Filter<>, valid?: false, ...>], limit: 50, order_by: [:name], order_directions: [:asc]}, errors: [], data: #Flop<>, valid?: false, ...>

Is this expected or am I doing something wrong? If it's not supposed to work, should in be removed from the allowed operators? and/or is there a combination of filters that will allow me to achieve this with Flop filters?

Elixir/Flop/Ecto Version Info

click to expand

Erlang/OTP 26 [erts-14.2.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.17.1 (compiled with Erlang/OTP 25)

  • ecto 3.12.5 (Hex package) (mix)
  • ecto_sql 3.12.1 (Hex package) (mix)
    locked at 3.12.1 (ecto_sql) aff5b958
  • flop 0.26.1 (Hex package) (mix)
    locked at 0.26.1 (flop) 5fcab8a1
  • flop_phoenix 0.23.1 (Hex package) (mix)
    locked at 0.23.1 (flop_phoenix) e7d55a2e
@Techn1x Techn1x added the bug Something isn't working label Dec 11, 2024
@Techn1x Techn1x changed the title [Bug] in is allowed operator for array field but does not filter for list value [Bug] in is allowed operator for array field but does not filter for list values Dec 11, 2024
@Techn1x
Copy link
Author

Techn1x commented Dec 11, 2024

I'm somewhat confident that this is a bug since no reason is given for the invalid filter error
errors: [value: {"is invalid", []}]

whereas other mistakes tend to give a reason there

@Techn1x
Copy link
Author

Techn1x commented Dec 11, 2024

As a plan B I would write it as a "custom_field", though I receive the same error. in operator only accepts lists as a value, yet when I provide a list it errors. I am guessing this is to do with providing ecto_type: {:array, :string}, in the custom field schema, it ends up treated the same way and we hit the same bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant