Skip to content

Commit 1413167

Browse files
authored
feat: Tesla.Middleware.JSON: Add support for Elixir 1.18's JSON module (#747)
1 parent 2f6b2a6 commit 1413167

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

lib/tesla/middleware/json.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ defmodule Tesla.Middleware.JSON do
1212
mix deps.compile tesla
1313
```
1414
15+
> #### Using built-in `JSON` from Elixir 1.18 {: .info}
16+
>
17+
> This middleware supports the built-in `JSON` module introduced in ELixir 1.18, but for historical
18+
> reasons is it not the default. To use it, set it as the `:engine`:
19+
>
20+
> {Tesla.Middleware.JSON, engine: JSON}
21+
>
22+
> For more advanced usage using custom encoders/decodes, provide the `:encode` and `:decode` anonymous functions instead.
23+
1524
If you only need to encode the request body or decode the response body,
1625
you can use `Tesla.Middleware.EncodeJson` or `Tesla.Middleware.DecodeJson` directly instead.
1726
@@ -24,6 +33,8 @@ defmodule Tesla.Middleware.JSON do
2433
# use jason engine
2534
Tesla.Middleware.JSON,
2635
# or
36+
{Tesla.Middleware.JSON, engine: JSON}
37+
# or
2738
{Tesla.Middleware.JSON, engine: JSX, engine_opts: [strict: [:comments]]},
2839
# or
2940
{Tesla.Middleware.JSON, engine: Poison, engine_opts: [keys: :atoms]},
@@ -39,7 +50,7 @@ defmodule Tesla.Middleware.JSON do
3950
- `:decode` - decoding function
4051
- `:encode` - encoding function
4152
- `:encode_content_type` - content-type to be used in request header
42-
- `:engine` - encode/decode engine, e.g `Jason`, `Poison` or `JSX` (defaults to Jason)
53+
- `:engine` - encode/decode engine, e.g `JSON`, `Jason`, `Poison` or `JSX` (defaults to Jason)
4354
- `:engine_opts` - optional engine options
4455
- `:decode_content_types` - list of additional decodable content-types
4556
"""
@@ -172,7 +183,14 @@ defmodule Tesla.Middleware.JSON do
172183
if fun = opts[op] do
173184
fun.(data)
174185
else
175-
engine = Keyword.get(opts, :engine, @default_engine)
186+
engine =
187+
case Keyword.fetch(opts, :engine) do
188+
# Special case for JSON, which doesn't have encode/2 nor return {:ok, json}
189+
{:ok, JSON} -> Tesla.Middleware.JSON.JSONAdapter
190+
{:ok, engine} -> engine
191+
:error -> @default_engine
192+
end
193+
176194
opts = Keyword.get(opts, :engine_opts, [])
177195

178196
apply(engine, op, [data, opts])
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if Code.ensure_loaded?(JSON) do
2+
defmodule Tesla.Middleware.JSON.JSONAdapter do
3+
@moduledoc false
4+
# An adapter for Elixir's built-in JSON module introduced in Elixir 1.18
5+
# that adjusts for Tesla's assumptions about a JSON engine, which are not satisfied by
6+
# Elixir's JSON module. The assumptions are:
7+
# - the module provides encode/2 and decode/2 functions
8+
# - the 2nd argument to the functions is opts
9+
# - the functions return {:ok, json} or {:error, reason} - not the case for JSON.encode!/2
10+
#
11+
# We do not support custom encoders and decoders.
12+
# The purpose of this adapter is to allow `engine: JSON` to be set easily. If more advanced
13+
# customization is required, the `:encode` and `:decode` functions can be supplied to the
14+
# middleware instead of the `:engine` option.
15+
def encode(data, _opts), do: {:ok, JSON.encode!(data)}
16+
def decode(binary, _opts), do: JSON.decode(binary)
17+
end
18+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if Code.ensure_loaded?(JSON) do
2+
defmodule Tesla.Middleware.JSON.JSONAdapterTest do
3+
use ExUnit.Case, async: true
4+
5+
alias Tesla.Middleware.JSON.JSONAdapter
6+
7+
describe "encode/2" do
8+
test "encodes as expected with default encoder" do
9+
assert {:ok, ~S({"hello":"world"})} == JSONAdapter.encode(%{hello: "world"}, [])
10+
end
11+
end
12+
13+
describe "decode/2" do
14+
test "returns {:ok, term} on success" do
15+
assert {:ok, %{"hello" => "world"}} = JSONAdapter.decode(~S({"hello":"world"}), [])
16+
end
17+
18+
test "returns {:error, reason} on failure" do
19+
assert {:error, {:invalid_byte, _, _}} = JSONAdapter.decode("invalid_json", [])
20+
end
21+
end
22+
end
23+
end

test/tesla/middleware/json_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,52 @@ defmodule Tesla.Middleware.JsonTest do
299299
assert env.body == %{"value" => 123}
300300
end
301301
end
302+
303+
if Code.ensure_loaded?(JSON) do
304+
describe "Engine: JSON" do
305+
defmodule JSONClient do
306+
use Tesla
307+
308+
plug Tesla.Middleware.JSON, engine: JSON
309+
310+
adapter fn env ->
311+
assert env.body == ~S({"hello":"world"})
312+
313+
{:ok,
314+
%{
315+
env
316+
| status: 200,
317+
headers: [{"content-type", "application/json"}],
318+
body: ~s|{"value": 123}|
319+
}}
320+
end
321+
end
322+
323+
test "encodes/decodes as expected" do
324+
assert {:ok, env} = JSONClient.post("/json", %{hello: "world"})
325+
assert env.body == %{"value" => 123}
326+
end
327+
328+
defmodule Decoder do
329+
use Tesla
330+
331+
plug Tesla.Middleware.DecodeJson, engine: JSON
332+
333+
adapter fn env ->
334+
{:ok,
335+
%{
336+
env
337+
| status: 200,
338+
headers: [{"content-type", "application/json"}],
339+
body: ~s|{"value": 123}|
340+
}}
341+
end
342+
end
343+
344+
test "decodes as expected" do
345+
assert {:ok, env} = Decoder.get("/json")
346+
assert env.body == %{"value" => 123}
347+
end
348+
end
349+
end
302350
end

0 commit comments

Comments
 (0)