Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/jsonapi/plugs/query_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule JSONAPI.QueryParser do

Enum.reduce(filter, config, fn {key, val}, acc ->
check_filter_validity!(opts_filter, key, config)
%{acc | filter: Keyword.put(acc.filter, String.to_atom(key), val)}
%{acc | filter: Keyword.put(acc.filter, String.to_atom(key), parse_filter_value(val))}
end)
end

Expand All @@ -123,6 +123,21 @@ defmodule JSONAPI.QueryParser do
end
end

defp parse_filter_value(value) do
:jsonapi
|> Application.get_env(:filter_values_separator, nil)
|> do_parse_filter_value(value)
end

defp do_parse_filter_value(nil, value), do: value

defp do_parse_filter_value(separator, value) do
case String.split(value, separator) do
[value] -> value
values -> values
end
end

@spec parse_fields(Config.t(), map()) :: Config.t() | no_return()
def parse_fields(%Config{} = config, fields) when fields == %{}, do: config

Expand Down
19 changes: 16 additions & 3 deletions test/jsonapi/plugs/query_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ defmodule JSONAPI.QueryParserTest do
end

setup do
previous_env = System.get_env()
Application.put_env(:jsonapi, :field_transformation, :underscore)

on_exit(fn ->
Application.delete_env(:jsonapi, :field_transformation)
end)
on_exit(fn -> System.put_env(previous_env) end)

{:ok, []}
end
Expand All @@ -67,6 +66,20 @@ defmodule JSONAPI.QueryParserTest do
assert filter[:name] == "jason"
end

test "parse_filter/2 handles multiple comma-separated filter values if configured" do
Application.put_env(:jsonapi, :filter_values_separator, ";")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be cleaned up in an on_exit hook?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! I've restored a previous commit where the whole env is backed up before tests and restored on exit.
This way we are sure we won't forget to clean up other variables.

config = struct(Config, opts: [filter: ~w(name)], view: MyView)
filter = parse_filter(config, %{"name" => "jason;api"}).filter
assert filter[:name] == ["jason", "api"]
end

test "parse_filter/2 does not does not parse comma-separated filter values if not configured" do
Application.delete_env(:jsonapi, :filter_values_separator)
config = struct(Config, opts: [filter: ~w(name)], view: MyView)
filter = parse_filter(config, %{"name" => "jason;api"}).filter
assert filter[:name] == "jason;api"
end

test "parse_filter/2 raises on invalid filters" do
config = struct(Config, opts: [], view: MyView)

Expand Down