Fix some specs about server-sent events in streaming

This commit is contained in:
tusooa 2023-04-01 16:46:32 -04:00
parent 8829dcaee4
commit f393a15dd1
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51

View file

@ -113,8 +113,8 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
defp get_schema(schema), do: schema.schema defp get_schema(schema), do: schema.schema
defp server_sent_event_helper(name, description, type, payload, opts \\ []) do defp server_sent_event_helper(name, description, type, payload, opts \\ []) do
payload_type = opts[:payload_type] || :json payload_type = Keyword.get(opts, :payload_type, :json)
has_stream = opts[:has_stream] || true has_stream = Keyword.get(opts, :has_stream, true)
stream_properties = stream_properties =
if has_stream do if has_stream do
@ -127,6 +127,24 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
stream_required = if has_stream, do: [:stream], else: [] stream_required = if has_stream, do: [:stream], else: []
payload_schema =
if payload_type == :json do
%Schema{
title: "Event payload",
description: "JSON-encoded string of #{get_schema(payload).title}",
allOf: [payload]
}
else
payload
end
payload_example =
if payload_type == :json do
get_schema(payload).example |> Jason.encode!()
else
get_schema(payload).example
end
%Schema{ %Schema{
type: :object, type: :object,
title: name, title: name,
@ -141,22 +159,13 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
required: true, required: true,
enum: [type] enum: [type]
}, },
payload: payload: payload_schema
if payload_type == :json do
%Schema{
title: "Event payload",
description: "JSON-encoded string of #{get_schema(payload).title}",
allOf: [payload]
}
else
payload
end
} }
|> Map.merge(stream_properties), |> Map.merge(stream_properties),
example: example:
%{ %{
"event" => type, "event" => type,
"payload" => get_schema(payload).example |> Jason.encode!() "payload" => payload_example
} }
|> Map.merge(stream_example) |> Map.merge(stream_example)
} }
@ -262,7 +271,8 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
allOf: [FlakeID], allOf: [FlakeID],
example: "some-opaque-id" example: "some-opaque-id"
}, },
payload_type: :string payload_type: :string,
has_stream: false
) )
end end
@ -274,7 +284,7 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
%Schema{ %Schema{
type: :object, type: :object,
title: "Results", title: "Results",
required: [:result], required: [:result, :type],
properties: %{ properties: %{
result: %Schema{ result: %Schema{
type: :string, type: :string,
@ -285,10 +295,15 @@ defmodule Pleroma.Web.ApiSpec.StreamingOperation do
type: :string, type: :string,
title: "Error code", title: "Error code",
description: "An error identifier. Only appears if `result` is `error`." description: "An error identifier. Only appears if `result` is `error`."
},
type: %Schema{
type: :string,
description: "Type of the request."
} }
}, },
example: %{"result" => "success"} example: %{"result" => "success", "type" => "pleroma:authenticate"}
} },
has_stream: false
) )
end end