From caaeb3ad2ea7f1a1ddaa741f5c9e66ec92ab0f73 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 8 Dec 2019 01:00:53 -0500 Subject: [PATCH] add parameter recoder --- .../controllers/api/json/tag_controller.ex | 1 + lib/philomena_web/controllers/tag_controller.ex | 2 ++ .../plugs/recode_parameter_plug.ex | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 lib/philomena_web/plugs/recode_parameter_plug.ex diff --git a/lib/philomena_web/controllers/api/json/tag_controller.ex b/lib/philomena_web/controllers/api/json/tag_controller.ex index 962925c2..d28619c9 100644 --- a/lib/philomena_web/controllers/api/json/tag_controller.ex +++ b/lib/philomena_web/controllers/api/json/tag_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Api.Json.TagController do alias Philomena.Tags.Tag + plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show] plug :load_resource, model: Tag, id_field: "slug", persisted: true, preload: [:aliased_tag, :aliases, :implied_tags, :implied_by_tags, :dnp_entries] def show(conn, _params) do diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index c4c49766..57288ce3 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -8,6 +8,8 @@ defmodule PhilomenaWeb.TagController do alias Philomena.Repo import Ecto.Query + plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show] + def index(conn, params) do query_string = params["tq"] || "*" diff --git a/lib/philomena_web/plugs/recode_parameter_plug.ex b/lib/philomena_web/plugs/recode_parameter_plug.ex new file mode 100644 index 00000000..c5b99405 --- /dev/null +++ b/lib/philomena_web/plugs/recode_parameter_plug.ex @@ -0,0 +1,17 @@ +defmodule PhilomenaWeb.RecodeParameterPlug do + def init(opts), do: opts + + def call(conn, [name: name]) do + fixed_value = + conn + |> Map.get(:params) + |> Map.get(name) + |> to_string() + |> URI.encode_www_form() + |> String.replace("%2B", "+") + + params = Map.put(conn.params, name, fixed_value) + + %{conn | params: params} + end +end \ No newline at end of file