Do not error on empty string custom prop value

This commit is contained in:
Uku Taht 2022-04-29 11:13:36 +03:00
parent 0de1dbcf9d
commit c92c548ca8
2 changed files with 36 additions and 20 deletions

View File

@ -200,23 +200,21 @@ defmodule PlausibleWeb.Api.ExternalController do
defp parse_meta(params) do
raw_meta = params["m"] || params["meta"] || params["p"] || params["props"]
with {:ok, parsed_json} <- decode_raw_props(raw_meta),
:ok <- validate_custom_props(parsed_json) do
parsed_json
else
_ -> %{}
case decode_raw_props(raw_meta) do
{:ok, parsed_json} ->
Enum.filter(parsed_json, fn
{_, ""} -> false
{_, val} when is_list(val) -> false
{_, val} when is_map(val) -> false
_ -> true
end)
|> Map.new()
_ ->
%{}
end
end
defp validate_custom_props(props) do
is_valid =
Enum.all?(props, fn {_key, val} ->
!is_list(val) && !is_map(val)
end)
if is_valid, do: :ok, else: :invalid_props
end
defp decode_raw_props(props) when is_map(props), do: {:ok, props}
defp decode_raw_props(raw_json) when is_binary(raw_json) do

View File

@ -509,7 +509,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
name: "Signup",
url: "http://gigride.live/",
domain: "custom-prop-test-4.com",
props: Jason.encode!(%{wat: ["some-thing"]})
props: Jason.encode!(%{wat: ["some-thing"], other: "key"})
}
conn = post(conn, "/api/event", params)
@ -518,8 +518,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("custom-prop-test-4.com")
assert Map.get(event, :"meta.key") == []
assert Map.get(event, :"meta.value") == []
assert Map.get(event, :"meta.key") == ["other"]
assert Map.get(event, :"meta.value") == ["key"]
end
test "ignores custom prop with map value", %{conn: conn} do
@ -527,7 +527,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
name: "Signup",
url: "http://gigride.live/",
domain: "custom-prop-test-5.com",
props: Jason.encode!(%{foo: %{bar: "baz"}})
props: Jason.encode!(%{foo: %{bar: "baz"}, other_key: 1})
}
conn = post(conn, "/api/event", params)
@ -536,8 +536,26 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("custom-prop-test-5.com")
assert Map.get(event, :"meta.key") == []
assert Map.get(event, :"meta.value") == []
assert Map.get(event, :"meta.key") == ["other_key"]
assert Map.get(event, :"meta.value") == ["1"]
end
test "ignores custom prop with empty string value", %{conn: conn} do
params = %{
name: "Signup",
url: "http://gigride.live/",
domain: "custom-prop-test-empty-string-val.com",
props: Jason.encode!(%{foo: "", other_key: true})
}
conn = post(conn, "/api/event", params)
assert conn.status == 202
event = get_event("custom-prop-test-empty-string-val.com")
assert Map.get(event, :"meta.key") == ["other_key"]
assert Map.get(event, :"meta.value") == ["true"]
end
test "ignores a malformed referrer URL", %{conn: conn} do