elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

Plug.Parsers.JSON should always wrap parsed content #1130

Closed amplexdenmark closed 1 year ago

amplexdenmark commented 1 year ago

Currently as documented non object JSON is wrapped in a Map with a "_json" key to the parsed content. But object JSON is returned as is.

This leads to the situation that parsing: {"_json":[]} cannot be distinguished from parsing: []

They both leads to the same result: %{"_json" => []}

The fix is not to handle objects specially, but always return the parsed content in a Map with "_json" key.

josevalim commented 1 year ago

Doing so would be a breaking page pretty much to all existing JSON apps out there but we will gladly accept a PR that makes it opt-in.

amplexdenmark commented 1 year ago

Ok, I will consider that. I'm not that familiar with the inner workings of Plug and how config is passed around, so it will probably be a while before the PR :)

josevalim commented 1 year ago

I believe the options given to Plug.Parsers is available to all parsers, so it could be something like nest_json_at: .... :)