Fix OpenAPI spec

This commit is contained in:
Egor Kislitsyn 2020-05-14 16:18:30 +04:00
parent 099e314a1b
commit 0f885b4b86
No known key found for this signature in database
GPG Key ID: 1B49CB15B71E7805
3 changed files with 40 additions and 33 deletions

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.ApiSpec.MediaOperation do
alias OpenApiSpex.Schema alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Helpers alias Pleroma.Web.ApiSpec.Helpers
alias Pleroma.Web.ApiSpec.Schemas.ApiError alias Pleroma.Web.ApiSpec.Schemas.ApiError
alias Pleroma.Web.ApiSpec.Schemas.Attachment
def open_api_operation(action) do def open_api_operation(action) do
operation = String.to_existing_atom("#{action}_operation") operation = String.to_existing_atom("#{action}_operation")
@ -22,24 +23,24 @@ def create_operation do
security: [%{"oAuth" => ["write:media"]}], security: [%{"oAuth" => ["write:media"]}],
requestBody: Helpers.request_body("Parameters", create_request()), requestBody: Helpers.request_body("Parameters", create_request()),
responses: %{ responses: %{
200 => 200 => Operation.response("Media", "application/json", Attachment),
Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
401 => Operation.response("Media", "application/json", ApiError), 401 => Operation.response("Media", "application/json", ApiError),
422 => Operation.response("Media", "application/json", ApiError) 422 => Operation.response("Media", "application/json", ApiError)
} }
} }
end end
defp create_request() do defp create_request do
%Schema{ %Schema{
title: "MediaCreateRequest", title: "MediaCreateRequest",
description: "POST body for creating an attachment", description: "POST body for creating an attachment",
type: :object, type: :object,
required: [:file],
properties: %{ properties: %{
file: %Schema{ file: %Schema{
type: :binary, type: :string,
description: "The file to be attached, using multipart form data.", format: :binary,
required: true description: "The file to be attached, using multipart form data."
}, },
description: %Schema{ description: %Schema{
type: :string, type: :string,
@ -60,29 +61,26 @@ def update_operation do
description: "Creates an attachment to be used with a new status.", description: "Creates an attachment to be used with a new status.",
operationId: "MediaController.update", operationId: "MediaController.update",
security: [%{"oAuth" => ["write:media"]}], security: [%{"oAuth" => ["write:media"]}],
parameters: [id_param()],
requestBody: Helpers.request_body("Parameters", update_request()), requestBody: Helpers.request_body("Parameters", update_request()),
responses: %{ responses: %{
200 => 200 => Operation.response("Media", "application/json", Attachment),
Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment), 400 => Operation.response("Media", "application/json", ApiError),
401 => Operation.response("Media", "application/json", ApiError), 401 => Operation.response("Media", "application/json", ApiError),
422 => Operation.response("Media", "application/json", ApiError) 422 => Operation.response("Media", "application/json", ApiError)
} }
} }
end end
defp update_request() do defp update_request do
%Schema{ %Schema{
title: "MediaCreateRequest", title: "MediaUpdateRequest",
description: "POST body for creating an attachment", description: "POST body for updating an attachment",
type: :object, type: :object,
properties: %{ properties: %{
id: %Schema{
type: :string,
description: "The id of the Attachment entity to be updated",
required: true
},
file: %Schema{ file: %Schema{
type: :binary, type: :string,
format: :binary,
description: "The file to be attached, using multipart form data." description: "The file to be attached, using multipart form data."
}, },
description: %Schema{ description: %Schema{
@ -102,10 +100,10 @@ def show_operation do
tags: ["media"], tags: ["media"],
summary: "Show Uploaded media attachment", summary: "Show Uploaded media attachment",
operationId: "MediaController.show", operationId: "MediaController.show",
parameters: [id_param()],
security: [%{"oAuth" => ["read:media"]}], security: [%{"oAuth" => ["read:media"]}],
responses: %{ responses: %{
200 => 200 => Operation.response("Media", "application/json", Attachment),
Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
401 => Operation.response("Media", "application/json", ApiError), 401 => Operation.response("Media", "application/json", ApiError),
422 => Operation.response("Media", "application/json", ApiError) 422 => Operation.response("Media", "application/json", ApiError)
} }
@ -121,11 +119,14 @@ def create2_operation do
security: [%{"oAuth" => ["write:media"]}], security: [%{"oAuth" => ["write:media"]}],
requestBody: Helpers.request_body("Parameters", create_request()), requestBody: Helpers.request_body("Parameters", create_request()),
responses: %{ responses: %{
202 => 202 => Operation.response("Media", "application/json", Attachment),
Operation.response("Media", "application/json", Pleroma.Web.ApiSpec.Schemas.Attachment),
422 => Operation.response("Media", "application/json", ApiError), 422 => Operation.response("Media", "application/json", ApiError),
500 => Operation.response("Media", "application/json", ApiError) 500 => Operation.response("Media", "application/json", ApiError)
} }
} }
end end
defp id_param do
Operation.parameter(:id, :path, :string, "The ID of the Attachment entity")
end
end end

View File

@ -19,12 +19,12 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MediaOperation
@doc "POST /api/v1/media" @doc "POST /api/v1/media"
def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
with {:ok, object} <- with {:ok, object} <-
ActivityPub.upload( ActivityPub.upload(
file, file,
actor: User.ap_id(user), actor: User.ap_id(user),
description: Map.get(data, "description") description: Map.get(data, :description)
) do ) do
attachment_data = Map.put(object.data, "id", object.id) attachment_data = Map.put(object.data, "id", object.id)
@ -35,12 +35,12 @@ def create(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
def create(_conn, _data), do: {:error, :bad_request} def create(_conn, _data), do: {:error, :bad_request}
@doc "POST /api/v2/media" @doc "POST /api/v2/media"
def create2(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn, _) do
with {:ok, object} <- with {:ok, object} <-
ActivityPub.upload( ActivityPub.upload(
file, file,
actor: User.ap_id(user), actor: User.ap_id(user),
description: Map.get(data, "description") description: Map.get(data, :description)
) do ) do
attachment_data = Map.put(object.data, "id", object.id) attachment_data = Map.put(object.data, "id", object.id)
@ -53,7 +53,9 @@ def create2(%{assigns: %{user: user}} = conn, %{"file" => file} = data) do
def create2(_conn, _data), do: {:error, :bad_request} def create2(_conn, _data), do: {:error, :bad_request}
@doc "PUT /api/v1/media/:id" @doc "PUT /api/v1/media/:id"
def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => description}) def update(%{assigns: %{user: user}, body_params: %{description: description}} = conn, %{
id: id
})
when is_binary(description) do when is_binary(description) do
with %Object{} = object <- Object.get_by_id(id), with %Object{} = object <- Object.get_by_id(id),
true <- Object.authorize_mutation(object, user), true <- Object.authorize_mutation(object, user),
@ -67,7 +69,7 @@ def update(%{assigns: %{user: user}} = conn, %{"id" => id, "description" => desc
def update(_conn, _data), do: {:error, :bad_request} def update(_conn, _data), do: {:error, :bad_request}
@doc "GET /api/v1/media/:id" @doc "GET /api/v1/media/:id"
def show(conn, %{"id" => id}) do def show(conn, %{id: id}) do
with %Object{data: data, id: object_id} <- Object.get_by_id(id) do with %Object{data: data, id: object_id} <- Object.get_by_id(id) do
attachment_data = Map.put(data, "id", object_id) attachment_data = Map.put(data, "id", object_id)

View File

@ -30,8 +30,9 @@ test "/api/v1/media", %{conn: conn, image: image} do
media = media =
conn conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/media", %{"file" => image, "description" => desc}) |> post("/api/v1/media", %{"file" => image, "description" => desc})
|> json_response(:ok) |> json_response_and_validate_schema(:ok)
assert media["type"] == "image" assert media["type"] == "image"
assert media["description"] == desc assert media["description"] == desc
@ -46,15 +47,16 @@ test "/api/v2/media", %{conn: conn, image: image} do
response = response =
conn conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v2/media", %{"file" => image, "description" => desc}) |> post("/api/v2/media", %{"file" => image, "description" => desc})
|> json_response(202) |> json_response_and_validate_schema(202)
assert media_id = response["id"] assert media_id = response["id"]
media = media =
conn conn
|> get("/api/v1/media/#{media_id}") |> get("/api/v1/media/#{media_id}")
|> json_response(200) |> json_response_and_validate_schema(200)
assert media["type"] == "image" assert media["type"] == "image"
assert media["description"] == desc assert media["description"] == desc
@ -85,8 +87,9 @@ test "/api/v2/media", %{conn: conn, image: image} do
test "/api/v1/media/:id good request", %{conn: conn, object: object} do test "/api/v1/media/:id good request", %{conn: conn, object: object} do
media = media =
conn conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{"description" => "test-media"}) |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
|> json_response(:ok) |> json_response_and_validate_schema(:ok)
assert media["description"] == "test-media" assert media["description"] == "test-media"
assert refresh_record(object).data["name"] == "test-media" assert refresh_record(object).data["name"] == "test-media"
@ -95,8 +98,9 @@ test "/api/v1/media/:id good request", %{conn: conn, object: object} do
test "/api/v1/media/:id bad request", %{conn: conn, object: object} do test "/api/v1/media/:id bad request", %{conn: conn, object: object} do
media = media =
conn conn
|> put_req_header("content-type", "multipart/form-data")
|> put("/api/v1/media/#{object.id}", %{}) |> put("/api/v1/media/#{object.id}", %{})
|> json_response(400) |> json_response_and_validate_schema(400)
assert media == %{"error" => "bad_request"} assert media == %{"error" => "bad_request"}
end end
@ -124,7 +128,7 @@ test "/api/v1/media/:id", %{conn: conn, object: object} do
media = media =
conn conn
|> get("/api/v1/media/#{object.id}") |> get("/api/v1/media/#{object.id}")
|> json_response(:ok) |> json_response_and_validate_schema(:ok)
assert media["description"] == "test-media" assert media["description"] == "test-media"
assert media["type"] == "image" assert media["type"] == "image"