From 87dfcfa17423fea79a510ccbfd0424fa613b6a78 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Thu, 28 Sep 2023 11:35:50 +0200 Subject: [PATCH] Use https://github.com/open-api-spex/open_api_spex/pull/425 due to https://github.com/open-api-spex/open_api_spex/issues/92 --- lib/plausible_web.ex | 2 +- lib/plausible_web/plugins/api/context/shared_links.ex | 4 ++-- .../plugins/api/controllers/shared_links.ex | 10 ++++++++-- lib/plausible_web/plugins/api/errors.ex | 2 +- lib/plausible_web/plugins/api/views/error.ex | 2 +- .../plugins/api/controllers/shared_links_test.exs | 4 +++- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/plausible_web.ex b/lib/plausible_web.ex index 7740fa7431c82..ab33753b41363 100644 --- a/lib/plausible_web.ex +++ b/lib/plausible_web.ex @@ -54,7 +54,7 @@ defmodule PlausibleWeb do alias PlausibleWeb.Plugins.API.Views alias PlausibleWeb.Plugins.API.Context - plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true) + plug(OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true, replace_params: false) use OpenApiSpex.ControllerSpecs end diff --git a/lib/plausible_web/plugins/api/context/shared_links.ex b/lib/plausible_web/plugins/api/context/shared_links.ex index 2956922e4e844..c666f0556d657 100644 --- a/lib/plausible_web/plugins/api/context/shared_links.ex +++ b/lib/plausible_web/plugins/api/context/shared_links.ex @@ -8,7 +8,7 @@ defmodule PlausibleWeb.Plugins.API.Context.SharedLinks do alias Plausible.Repo - @spec get_shared_links(Plausible.Site.t(), Keyword.t()) :: {:ok, Paginator.Page.t()} + @spec get_shared_links(Plausible.Site.t(), map()) :: {:ok, Paginator.Page.t()} def get_shared_links(site, params) do query = from l in Plausible.Site.SharedLink, @@ -27,7 +27,7 @@ defmodule PlausibleWeb.Plugins.API.Context.SharedLinks do get_by_name(site, name) end - @spec get_or_create(Plausible.Site.t(), String.t(), String.t()) :: + @spec get_or_create(Plausible.Site.t(), String.t(), String.t() | nil) :: {:ok, Plausible.Site.SharedLink.t()} def get_or_create(site, name, password \\ nil) do case get_by_name(site, name) do diff --git a/lib/plausible_web/plugins/api/controllers/shared_links.ex b/lib/plausible_web/plugins/api/controllers/shared_links.ex index 2e7b2d152c79c..a344b7e38b749 100644 --- a/lib/plausible_web/plugins/api/controllers/shared_links.ex +++ b/lib/plausible_web/plugins/api/controllers/shared_links.ex @@ -48,7 +48,13 @@ defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinks do @spec create(Plug.Conn.t(), map()) :: Plug.Conn.t() def create( - %{body_params: %Schemas.SharedLink.CreateRequest{name: name, password: password}} = conn, + %{ + private: %{ + open_api_spex: %{ + body_params: %Schemas.SharedLink.CreateRequest{name: name, password: password} + } + } + } = conn, _params ) do site = conn.assigns.authorized_site @@ -83,7 +89,7 @@ defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinks do ) @spec get(Plug.Conn.t(), map()) :: Plug.Conn.t() - def get(conn, %{id: id}) do + def get(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _params) do site = conn.assigns.authorized_site case Context.SharedLinks.get(site, id) do diff --git a/lib/plausible_web/plugins/api/errors.ex b/lib/plausible_web/plugins/api/errors.ex index 6251020f3af56..e23f438a6dd0c 100644 --- a/lib/plausible_web/plugins/api/errors.ex +++ b/lib/plausible_web/plugins/api/errors.ex @@ -12,7 +12,7 @@ defmodule PlausibleWeb.Plugins.API.Errors do |> error(:unauthorized, "Plugins API: unauthorized") end - @spec internal_server_error(Conn.t()) :: Conn.t() + @spec internal_server_error(Plug.Conn.t()) :: Plug.Conn.t() def internal_server_error(conn) do contact_support_note = if not Plausible.Release.selfhost?() do diff --git a/lib/plausible_web/plugins/api/views/error.ex b/lib/plausible_web/plugins/api/views/error.ex index e180921e39185..7c7ec6014bb85 100644 --- a/lib/plausible_web/plugins/api/views/error.ex +++ b/lib/plausible_web/plugins/api/views/error.ex @@ -8,7 +8,7 @@ defmodule PlausibleWeb.Plugins.API.Views.Error do render("500.json", assigns) end - @spec render(Phoenix.Template.name(), map) :: map | binary() + @spec render(String.t(), map) :: map | binary() def render("400.json", _assigns) do %{errors: [%{detail: "Bad request"}]} end diff --git a/test/plausible_web/plugins/api/controllers/shared_links_test.exs b/test/plausible_web/plugins/api/controllers/shared_links_test.exs index 17261d52761fb..7e128d2332e8c 100644 --- a/test/plausible_web/plugins/api/controllers/shared_links_test.exs +++ b/test/plausible_web/plugins/api/controllers/shared_links_test.exs @@ -108,7 +108,9 @@ defmodule PlausibleWeb.Plugins.API.Controllers.SharedLinksTest do assert resp.data.href =~ "http://localhost:8000/share/#{site.domain}?auth=" [location] = get_resp_header(conn, "location") - assert location == Routes.shared_links_url(base_uri(), :get, resp.data.id) + + assert location == + Routes.shared_links_url(base_uri(), :get, resp.data.id) assert ^resp = initial_conn