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

View File

@ -509,7 +509,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
name: "Signup", name: "Signup",
url: "http://gigride.live/", url: "http://gigride.live/",
domain: "custom-prop-test-4.com", 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) conn = post(conn, "/api/event", params)
@ -518,8 +518,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("custom-prop-test-4.com") event = get_event("custom-prop-test-4.com")
assert Map.get(event, :"meta.key") == [] assert Map.get(event, :"meta.key") == ["other"]
assert Map.get(event, :"meta.value") == [] assert Map.get(event, :"meta.value") == ["key"]
end end
test "ignores custom prop with map value", %{conn: conn} do test "ignores custom prop with map value", %{conn: conn} do
@ -527,7 +527,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
name: "Signup", name: "Signup",
url: "http://gigride.live/", url: "http://gigride.live/",
domain: "custom-prop-test-5.com", 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) conn = post(conn, "/api/event", params)
@ -536,8 +536,26 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
event = get_event("custom-prop-test-5.com") event = get_event("custom-prop-test-5.com")
assert Map.get(event, :"meta.key") == [] assert Map.get(event, :"meta.key") == ["other_key"]
assert Map.get(event, :"meta.value") == [] 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 end
test "ignores a malformed referrer URL", %{conn: conn} do test "ignores a malformed referrer URL", %{conn: conn} do