@@ -15,6 +15,7 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do | |||||
@behaviour Plug | @behaviour Plug | ||||
alias OpenApiSpex.Plug.PutApiSpec | |||||
alias Plug.Conn | alias Plug.Conn | ||||
@impl Plug | @impl Plug | ||||
@@ -25,12 +26,10 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do | |||||
end | end | ||||
@impl Plug | @impl Plug | ||||
def call(%{private: %{open_api_spex: private_data}} = conn, %{ | |||||
operation_id: operation_id, | |||||
render_error: render_error | |||||
}) do | |||||
spec = private_data.spec | |||||
operation = private_data.operation_lookup[operation_id] | |||||
def call(conn, %{operation_id: operation_id, render_error: render_error}) do | |||||
{spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn) | |||||
operation = operation_lookup[operation_id] | |||||
content_type = | content_type = | ||||
case Conn.get_req_header(conn, "content-type") do | case Conn.get_req_header(conn, "content-type") do | ||||
@@ -43,8 +42,7 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do | |||||
"application/json" | "application/json" | ||||
end | end | ||||
private_data = Map.put(private_data, :operation_id, operation_id) | |||||
conn = Conn.put_private(conn, :open_api_spex, private_data) | |||||
conn = Conn.put_private(conn, :operation_id, operation_id) | |||||
case cast_and_validate(spec, operation, conn, content_type, strict?()) do | case cast_and_validate(spec, operation, conn, content_type, strict?()) do | ||||
{:ok, conn} -> | {:ok, conn} -> | ||||
@@ -64,25 +62,22 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do | |||||
private: %{ | private: %{ | ||||
phoenix_controller: controller, | phoenix_controller: controller, | ||||
phoenix_action: action, | phoenix_action: action, | ||||
open_api_spex: private_data | |||||
open_api_spex: %{spec_module: spec_module} | |||||
} | } | ||||
} = conn, | } = conn, | ||||
opts | opts | ||||
) do | ) do | ||||
{spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn) | |||||
operation = | operation = | ||||
case private_data.operation_lookup[{controller, action}] do | |||||
case operation_lookup[{controller, action}] do | |||||
nil -> | nil -> | ||||
operation_id = controller.open_api_operation(action).operationId | operation_id = controller.open_api_operation(action).operationId | ||||
operation = private_data.operation_lookup[operation_id] | |||||
operation = operation_lookup[operation_id] | |||||
operation_lookup = | |||||
private_data.operation_lookup | |||||
|> Map.put({controller, action}, operation) | |||||
operation_lookup = Map.put(operation_lookup, {controller, action}, operation) | |||||
OpenApiSpex.Plug.Cache.adapter().put( | |||||
private_data.spec_module, | |||||
{private_data.spec, operation_lookup} | |||||
) | |||||
OpenApiSpex.Plug.Cache.adapter().put(spec_module, {spec, operation_lookup}) | |||||
operation | operation | ||||
@@ -59,7 +59,7 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do | |||||
Operation.response( | Operation.response( | ||||
"Status. When `scheduled_at` is present, ScheduledStatus is returned instead", | "Status. When `scheduled_at` is present, ScheduledStatus is returned instead", | ||||
"application/json", | "application/json", | ||||
%Schema{oneOf: [Status, ScheduledStatus]} | |||||
%Schema{anyOf: [Status, ScheduledStatus]} | |||||
), | ), | ||||
422 => Operation.response("Bad Request / MRF Rejection", "application/json", ApiError) | 422 => Operation.response("Bad Request / MRF Rejection", "application/json", ApiError) | ||||
} | } | ||||
@@ -3,6 +3,7 @@ | |||||
# SPDX-License-Identifier: AGPL-3.0-only | # SPDX-License-Identifier: AGPL-3.0-only | ||||
defmodule Pleroma.Web.ApiSpec.Schemas.BooleanLike do | defmodule Pleroma.Web.ApiSpec.Schemas.BooleanLike do | ||||
alias OpenApiSpex.Cast | |||||
alias OpenApiSpex.Schema | alias OpenApiSpex.Schema | ||||
require OpenApiSpex | require OpenApiSpex | ||||
@@ -27,10 +28,13 @@ defmodule Pleroma.Web.ApiSpec.Schemas.BooleanLike do | |||||
%Schema{type: :boolean}, | %Schema{type: :boolean}, | ||||
%Schema{type: :string}, | %Schema{type: :string}, | ||||
%Schema{type: :integer} | %Schema{type: :integer} | ||||
] | |||||
], | |||||
"x-validate": __MODULE__ | |||||
}) | }) | ||||
def after_cast(value, _schmea) do | |||||
{:ok, Pleroma.Web.ControllerHelper.truthy_param?(value)} | |||||
def cast(%Cast{value: value} = context) do | |||||
context | |||||
|> Map.put(:value, Pleroma.Web.ControllerHelper.truthy_param?(value)) | |||||
|> Cast.ok() | |||||
end | end | ||||
end | end |
@@ -5,7 +5,7 @@ | |||||
defmodule Pleroma.Web.MastodonAPI.InstanceController do | defmodule Pleroma.Web.MastodonAPI.InstanceController do | ||||
use Pleroma.Web, :controller | use Pleroma.Web, :controller | ||||
plug(OpenApiSpex.Plug.CastAndValidate) | |||||
plug(Pleroma.Web.ApiSpec.CastAndValidate) | |||||
plug( | plug( | ||||
:skip_plug, | :skip_plug, | ||||
@@ -10,7 +10,7 @@ defmodule Pleroma.Web.PleromaAPI.BackupController do | |||||
action_fallback(Pleroma.Web.MastodonAPI.FallbackController) | action_fallback(Pleroma.Web.MastodonAPI.FallbackController) | ||||
plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action in [:index, :create]) | plug(OAuthScopesPlug, %{scopes: ["read:accounts"]} when action in [:index, :create]) | ||||
plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError) | |||||
plug(Pleroma.Web.ApiSpec.CastAndValidate) | |||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaBackupOperation | defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PleromaBackupOperation | ||||
@@ -38,7 +38,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do | |||||
%{scopes: ["read:chats"]} when action in [:messages, :index, :index2, :show] | %{scopes: ["read:chats"]} when action in [:messages, :index, :index2, :show] | ||||
) | ) | ||||
plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError) | |||||
plug(Pleroma.Web.ApiSpec.CastAndValidate) | |||||
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation | defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation | ||||
@@ -15,7 +15,7 @@ defmodule Pleroma.Web.PleromaAPI.UserImportController do | |||||
plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks) | plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks) | ||||
plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes) | plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes) | ||||
plug(OpenApiSpex.Plug.CastAndValidate) | |||||
plug(Pleroma.Web.ApiSpec.CastAndValidate) | |||||
defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation | defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation | ||||
def follow(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do | def follow(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do | ||||
@@ -195,9 +195,7 @@ defmodule Pleroma.Mixfile do | |||||
{:majic, | {:majic, | ||||
git: "https://git.pleroma.social/pleroma/elixir-libraries/majic.git", | git: "https://git.pleroma.social/pleroma/elixir-libraries/majic.git", | ||||
ref: "289cda1b6d0d70ccb2ba508a2b0bd24638db2880"}, | ref: "289cda1b6d0d70ccb2ba508a2b0bd24638db2880"}, | ||||
{:open_api_spex, | |||||
git: "https://git.pleroma.social/pleroma/elixir-libraries/open_api_spex.git", | |||||
ref: "f296ac0924ba3cf79c7a588c4c252889df4c2edd"}, | |||||
{:open_api_spex, "~> 3.10"}, | |||||
## dev & test | ## dev & test | ||||
{:ex_doc, "~> 0.22", only: :dev, runtime: false}, | {:ex_doc, "~> 0.22", only: :dev, runtime: false}, | ||||
@@ -82,7 +82,7 @@ | |||||
"nimble_pool": {:hex, :nimble_pool, "0.1.0", "ffa9d5be27eee2b00b0c634eb649aa27f97b39186fec3c493716c2a33e784ec6", [:mix], [], "hexpm", "343a1eaa620ddcf3430a83f39f2af499fe2370390d4f785cd475b4df5acaf3f9"}, | "nimble_pool": {:hex, :nimble_pool, "0.1.0", "ffa9d5be27eee2b00b0c634eb649aa27f97b39186fec3c493716c2a33e784ec6", [:mix], [], "hexpm", "343a1eaa620ddcf3430a83f39f2af499fe2370390d4f785cd475b4df5acaf3f9"}, | ||||
"nodex": {:git, "https://git.pleroma.social/pleroma/nodex", "cb6730f943cfc6aad674c92161be23a8411f15d1", [ref: "cb6730f943cfc6aad674c92161be23a8411f15d1"]}, | "nodex": {:git, "https://git.pleroma.social/pleroma/nodex", "cb6730f943cfc6aad674c92161be23a8411f15d1", [ref: "cb6730f943cfc6aad674c92161be23a8411f15d1"]}, | ||||
"oban": {:hex, :oban, "2.3.4", "ec7509b9af2524d55f529cb7aee93d36131ae0bf0f37706f65d2fe707f4d9fd8", [:mix], [{:ecto_sql, ">= 3.4.3", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c70ca0434758fd1805422ea4446af5e910ddc697c0c861549c8f0eb0cfbd2fdf"}, | "oban": {:hex, :oban, "2.3.4", "ec7509b9af2524d55f529cb7aee93d36131ae0bf0f37706f65d2fe707f4d9fd8", [:mix], [{:ecto_sql, ">= 3.4.3", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c70ca0434758fd1805422ea4446af5e910ddc697c0c861549c8f0eb0cfbd2fdf"}, | ||||
"open_api_spex": {:git, "https://git.pleroma.social/pleroma/elixir-libraries/open_api_spex.git", "f296ac0924ba3cf79c7a588c4c252889df4c2edd", [ref: "f296ac0924ba3cf79c7a588c4c252889df4c2edd"]}, | |||||
"open_api_spex": {:hex, :open_api_spex, "3.10.0", "94e9521ad525b3fcf6dc77da7c45f87fdac24756d4de588cb0816b413e7c1844", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 3.1", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "2dbb2bde3d2b821f06936e8dfaf3284331186556291946d84eeba3750ac28765"}, | |||||
"p1_utils": {:hex, :p1_utils, "1.0.18", "3fe224de5b2e190d730a3c5da9d6e8540c96484cf4b4692921d1e28f0c32b01c", [:rebar3], [], "hexpm", "1fc8773a71a15553b179c986b22fbeead19b28fe486c332d4929700ffeb71f88"}, | "p1_utils": {:hex, :p1_utils, "1.0.18", "3fe224de5b2e190d730a3c5da9d6e8540c96484cf4b4692921d1e28f0c32b01c", [:rebar3], [], "hexpm", "1fc8773a71a15553b179c986b22fbeead19b28fe486c332d4929700ffeb71f88"}, | ||||
"parse_trans": {:git, "https://github.com/uwiger/parse_trans.git", "76abb347c3c1d00fb0ccf9e4b43e22b3d2288484", [tag: "3.3.0"]}, | "parse_trans": {:git, "https://github.com/uwiger/parse_trans.git", "76abb347c3c1d00fb0ccf9e4b43e22b3d2288484", [tag: "3.3.0"]}, | ||||
"pbkdf2_elixir": {:hex, :pbkdf2_elixir, "1.2.1", "9cbe354b58121075bd20eb83076900a3832324b7dd171a6895fab57b6bb2752c", [:mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}], "hexpm", "d3b40a4a4630f0b442f19eca891fcfeeee4c40871936fed2f68e1c4faa30481f"}, | "pbkdf2_elixir": {:hex, :pbkdf2_elixir, "1.2.1", "9cbe354b58121075bd20eb83076900a3832324b7dd171a6895fab57b6bb2752c", [:mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}], "hexpm", "d3b40a4a4630f0b442f19eca891fcfeeee4c40871936fed2f68e1c4faa30481f"}, | ||||
@@ -67,13 +67,11 @@ defmodule Pleroma.Web.ConnCase do | |||||
end | end | ||||
defp json_response_and_validate_schema( | defp json_response_and_validate_schema( | ||||
%{ | |||||
private: %{ | |||||
open_api_spex: %{operation_id: op_id, operation_lookup: lookup, spec: spec} | |||||
} | |||||
} = conn, | |||||
%{private: %{operation_id: op_id}} = conn, | |||||
status | status | ||||
) do | ) do | ||||
{spec, lookup} = OpenApiSpex.Plug.PutApiSpec.get_spec_and_operation_lookup(conn) | |||||
content_type = | content_type = | ||||
conn | conn | ||||
|> Plug.Conn.get_resp_header("content-type") | |> Plug.Conn.get_resp_header("content-type") | ||||