From ef8e90f22a11795fdf8c55caa0bed0505bdc5e42 Mon Sep 17 00:00:00 2001 From: laetitia Date: Sun, 16 Nov 2025 10:53:24 +0200 Subject: [PATCH 1/3] fix to support body nil for request with content-type json --- lib/req_api_logger.ex | 7 ++++--- test/req_api_logger_test.exs | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/req_api_logger.ex b/lib/req_api_logger.ex index be36ed9..c3f6ef8 100644 --- a/lib/req_api_logger.ex +++ b/lib/req_api_logger.ex @@ -130,9 +130,10 @@ defmodule AntlUtilsElixir.ReqApiLogger do defp format_request_body(%Request{} = req, hide_list) do case Request.get_header(req, "content-type") do ["application/json" <> _] -> - req.body - |> Jason.decode!() - |> hide(hide_list) + req.body && + req.body + |> Jason.decode!() + |> hide(hide_list) ["application/x-www-form-urlencoded"] -> req.body diff --git a/test/req_api_logger_test.exs b/test/req_api_logger_test.exs index 7a176c5..1f3d419 100644 --- a/test/req_api_logger_test.exs +++ b/test/req_api_logger_test.exs @@ -41,6 +41,17 @@ defmodule AntlUtilsElixir.ReqApiLoggerTest do ~r/api_name=foobarbaz api_request_id=[^ ]+ Sent GET/ end + test "be able to log json request with body nil" do + TestServer.add("/", via: :post) + + assert capture_log(fn -> + Req.new(url: url(), headers: [{"content-type", "application/json"}]) + |> ReqApiLogger.attach(api_name: :test) + |> Req.post() + end) =~ + ~r/body=nil/ + end + test "request_id changes for each request" do request_id_regex = ~r/ api_request_id=([^ ]+) Sent GET/ [_, id1] = request_id_regex |> Regex.run(capture_log(fn -> req_run_with_logger() end)) From a677e0fd91f89ccb29bd059db01c73dbe89969bc Mon Sep 17 00:00:00 2001 From: Joel Kociolek Date: Sun, 16 Nov 2025 12:51:17 +0200 Subject: [PATCH 2/3] found more possible invalid json cases : fixed --- lib/req_api_logger.ex | 9 +++++---- test/req_api_logger_test.exs | 39 ++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/req_api_logger.ex b/lib/req_api_logger.ex index c3f6ef8..e559260 100644 --- a/lib/req_api_logger.ex +++ b/lib/req_api_logger.ex @@ -130,10 +130,11 @@ defmodule AntlUtilsElixir.ReqApiLogger do defp format_request_body(%Request{} = req, hide_list) do case Request.get_header(req, "content-type") do ["application/json" <> _] -> - req.body && - req.body - |> Jason.decode!() - |> hide(hide_list) + with {:ok, decoded} <- Jason.decode("#{req.body}") do + hide(decoded, hide_list) + else + _ -> req.body + end ["application/x-www-form-urlencoded"] -> req.body diff --git a/test/req_api_logger_test.exs b/test/req_api_logger_test.exs index 1f3d419..f3e4fdf 100644 --- a/test/req_api_logger_test.exs +++ b/test/req_api_logger_test.exs @@ -41,17 +41,6 @@ defmodule AntlUtilsElixir.ReqApiLoggerTest do ~r/api_name=foobarbaz api_request_id=[^ ]+ Sent GET/ end - test "be able to log json request with body nil" do - TestServer.add("/", via: :post) - - assert capture_log(fn -> - Req.new(url: url(), headers: [{"content-type", "application/json"}]) - |> ReqApiLogger.attach(api_name: :test) - |> Req.post() - end) =~ - ~r/body=nil/ - end - test "request_id changes for each request" do request_id_regex = ~r/ api_request_id=([^ ]+) Sent GET/ [_, id1] = request_id_regex |> Regex.run(capture_log(fn -> req_run_with_logger() end)) @@ -189,4 +178,32 @@ defmodule AntlUtilsElixir.ReqApiLoggerTest do Logger.configure(level: :debug) end end + + defp req_get_with_json_header(body) do + Req.new(url: url(), headers: [{"content-type", "application/json"}], body: body) + |> ReqApiLogger.attach(api_name: :test) + |> Req.get() + end + + describe "ReqApiLogger regression tests : don't crash on invalid JSON request" do + setup do + TestServer.add("/") + :ok + end + + test "nil body" do + assert capture_log(fn -> req_get_with_json_header(nil) end) =~ + ~r/body=nil/ + end + + test "empty body" do + assert capture_log(fn -> req_get_with_json_header("") end) =~ + ~r/body=""/ + end + + test "malformed json" do + assert capture_log(fn -> req_get_with_json_header("{") end) =~ + ~r/body="{"/ + end + end end From eb1ca38b84dcb297ab929f80c198502e0a83eb8d Mon Sep 17 00:00:00 2001 From: Joel Kociolek Date: Sun, 16 Nov 2025 13:02:12 +0200 Subject: [PATCH 3/3] please linter --- lib/req_api_logger.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/req_api_logger.ex b/lib/req_api_logger.ex index e559260..f0b6693 100644 --- a/lib/req_api_logger.ex +++ b/lib/req_api_logger.ex @@ -130,7 +130,7 @@ defmodule AntlUtilsElixir.ReqApiLogger do defp format_request_body(%Request{} = req, hide_list) do case Request.get_header(req, "content-type") do ["application/json" <> _] -> - with {:ok, decoded} <- Jason.decode("#{req.body}") do + with {:ok, decoded} <- Jason.decode("#{req.body}") do hide(decoded, hide_list) else _ -> req.body