From cdd89b98c571fbd2bcd9f1049a213fbc828b4407 Mon Sep 17 00:00:00 2001 From: Zoey Pessanha Date: Sat, 4 Oct 2025 19:06:49 -0300 Subject: [PATCH 1/2] fix: correctly handle different json libraries apis/decoding opts --- .gitignore | 2 ++ config/config.exs | 4 +++- lib/supabase.ex | 31 +++++++++++++++++++++++++- test/supabase/fetcher/request_test.exs | 3 ++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 138612e..16726ef 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,8 @@ result .elixir_ls/ .elixir-tools/ .lexical/ +.expert/ # Dialyzer /priv/plts/ + diff --git a/config/config.exs b/config/config.exs index 89ec9fd..6f3ec4d 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,6 +5,8 @@ if config_env() == :dev do base_url: System.fetch_env!("SUPABASE_URL"), api_key: System.fetch_env!("SUPABASE_KEY"), env: config_env() +end - config :supabase, json_library: JSON +if config_env() in [:dev, :test] do + config :supabase_potion, json_library: JSON end diff --git a/lib/supabase.ex b/lib/supabase.ex index a59f42c..b4d1865 100644 --- a/lib/supabase.ex +++ b/lib/supabase.ex @@ -166,8 +166,37 @@ defmodule Supabase do apply(__MODULE__, which, []) end - @json_library Application.compile_env(:supabase, :json_library, Jason) + @json_library Application.compile_env(:supabase_potion, :json_library, Jason) @doc "Returns the configured JSON encoding library for Supabase libraries." + @spec json_library :: Poison | Jason | JSON def json_library, do: @json_library + + @doc false + defguardp is_atom_opt(atom) when atom in ~w(atoms atoms!)a + + @doc false + def decode_json(term, opts) do + keys = Keyword.get(opts, :keys) + + with {:ok, decoded} <- json_library().decode(term) do + if is_atom_opt(keys), do: atom_keys(decoded, keys), else: decoded + end + end + + defp atom_keys(term, atom) when is_list(term) do + Enum.map(term, &atom_keys(&1, atom)) + end + + defp atom_keys(term, atom) when is_map(term) do + key = + if atom == :atoms!, + do: &String.to_existing_atom/1, + else: &String.to_atom/1 + + Map.new(term, fn + {k, v} when is_map(v) -> {key.(k), atom_keys(v, atom)} + {k, v} -> {key.(k), v} + end) + end end diff --git a/test/supabase/fetcher/request_test.exs b/test/supabase/fetcher/request_test.exs index 8055362..da83065 100644 --- a/test/supabase/fetcher/request_test.exs +++ b/test/supabase/fetcher/request_test.exs @@ -89,8 +89,9 @@ defmodule Supabase.Fetcher.RequestTest do test "sets a JSON-encoded body when given a map", %{client: client} do body = %{key: "value"} builder = Request.new(client) |> Request.with_body(body) + json = Supabase.json_library() - assert builder.body == Jason.encode_to_iodata!(%{"key" => "value"}) + assert builder.body == json.encode_to_iodata!(%{"key" => "value"}) end test "sets raw body when given a binary", %{client: client} do From b4d6464647db11e33e6eda739a95ad05f75df894 Mon Sep 17 00:00:00 2001 From: Zoey Pessanha Date: Sat, 4 Oct 2025 19:13:04 -0300 Subject: [PATCH 2/2] fix: well, JSON only on v1.18 --- config/config.exs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 6f3ec4d..e7ade12 100644 --- a/config/config.exs +++ b/config/config.exs @@ -5,8 +5,10 @@ if config_env() == :dev do base_url: System.fetch_env!("SUPABASE_URL"), api_key: System.fetch_env!("SUPABASE_KEY"), env: config_env() -end -if config_env() in [:dev, :test] do config :supabase_potion, json_library: JSON end + +if config_env() == :test do + config :supabase_potion, json_library: Jason +end