Skip to content

Issue #1130 added nest_all option to nest all parsed json into a _jso… #1132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/plug/parsers/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ defmodule Plug.Parsers.JSON do

So by default, `Plug.Parsers` will read 1_000_000 bytes at a time from the
socket with an overall limit of 8_000_000 bytes.

The option `:nest_all_json`, when true, specifies all parsed JSON (including maps)
are parsed into a `"_json"` key.
"""

@behaviour Plug.Parsers
Expand Down Expand Up @@ -69,7 +72,7 @@ defmodule Plug.Parsers.JSON do
@impl true
def parse(conn, "application", subtype, _headers, {{mod, fun, args}, decoder, opts}) do
if subtype == "json" or String.ends_with?(subtype, "+json") do
apply(mod, fun, [conn, opts | args]) |> decode(decoder)
apply(mod, fun, [conn, opts | args]) |> decode(decoder, opts)
else
{:next, conn}
end
Expand All @@ -79,33 +82,35 @@ defmodule Plug.Parsers.JSON do
{:next, conn}
end

defp decode({:ok, "", conn}, _decoder) do
defp decode({:ok, "", conn}, _decoder, _opts) do
{:ok, %{}, conn}
end

defp decode({:ok, body, conn}, {module, fun, args}) do
defp decode({:ok, body, conn}, {module, fun, args}, opts) do
nest_all = Keyword.get(opts, :nest_all_json, false)

try do
apply(module, fun, [body | args])
rescue
e -> raise Plug.Parsers.ParseError, exception: e
else
terms when is_map(terms) ->
terms when is_map(terms) and not nest_all ->
{:ok, terms, conn}

terms ->
{:ok, %{"_json" => terms}, conn}
end
end

defp decode({:more, _, conn}, _decoder) do
defp decode({:more, _, conn}, _decoder, _opts) do
{:error, :too_large, conn}
end

defp decode({:error, :timeout}, _decoder) do
defp decode({:error, :timeout}, _decoder, _opts) do
raise Plug.TimeoutError
end

defp decode({:error, _}, _decoder) do
defp decode({:error, _}, _decoder, _opts) do
raise Plug.BadRequestError
end
end
15 changes: 15 additions & 0 deletions test/plug/parsers/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ defmodule Plug.Parsers.JSONTest do
def decode!(~s(null)) do
nil
end

def decode!("[]") do
[]
end

def decode!("{_json: []}") do
%{"_json" => []}
end

def decode!(_) do
raise "oops"
Expand Down Expand Up @@ -186,4 +194,11 @@ defmodule Plug.Parsers.JSONTest do

assert Plug.Exception.status(exception) == 400
end

test "nests all json when nest_all_json is true" do
conn_object = "{_json: []}" |> json_conn() |> parse(nest_all_json: true)
conn_array = "[]" |> json_conn() |> parse(nest_all_json: true)
assert conn_object.params == %{"_json" => %{"_json" => []}}
assert conn_array.params == %{"_json" => []}
end
end