View Source JSONAPI.UnderscoreParameters (jsonapi v1.7.1)

Takes dasherized JSON:API params and converts them to underscored params. Add this to your API's pipeline to aid in dealing with incoming parameters such as query params or data.

By default the newly underscored params will only replace the existing params field of the Plug.Conn struct, but leave the query_params and body_params untouched. If you are using the JSONAPI.QueryParser and need to also have the query_params on the Plug.Conn updated, set the replace_query_params option to true.

Note that this Plug will only underscore parameters when the request's content type is for a JSON:API request (i.e. "application/vnd.api+json"). All other content types will be ignored.

Options

  • :replace_query_params - When true, it will replace the query_params field in the Plug.Conn struct. This is useful when you have downstream code which is expecting underscored fields in query_params, and not just in params. Defaults to false.

Example

%{ "data" => %{

"attributes" => %{
  "foo-bar" => true
}

} }

are transformed to:

%{ "data" => %{

"attributes" => %{
  "foo_bar" => true
}

} }

Moreover, with a GET request like:

GET /example?filters[dog-breed]=Corgi

Without this Plug your index action would look like:

def index(conn, %{"filters" => %{"dog-breed" => "Corgi"}})

And with this Plug:

def index(conn, %{"filters" => %{"dog_breed" => "Corgi"}})

Your API's pipeline might look something like this:

# e.g. a Phoenix app

pipeline :api do
  plug JSONAPI.EnforceSpec
  plug JSONAPI.UnderscoreParameters
end