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 @@ defp get_schema(%Schema{} = schema), do: schema
defp get_schema(schema), do: schema.schema
defp server_sent_event_helper(name, description, type, payload, opts \\ []) do
payload_type = opts[:payload_type] || :json
has_stream = opts[:has_stream] || true
payload_type = Keyword.get(opts, :payload_type, :json)
has_stream = Keyword.get(opts, :has_stream, true)
stream_properties =
if has_stream do
@ -127,6 +127,24 @@ defp server_sent_event_helper(name, description, type, payload, opts \\ []) do
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{
type: :object,
title: name,
@ -141,22 +159,13 @@ defp server_sent_event_helper(name, description, type, payload, opts \\ []) do
required: true,
enum: [type]
},
payload:
if payload_type == :json do
%Schema{
title: "Event payload",
description: "JSON-encoded string of #{get_schema(payload).title}",
allOf: [payload]
}
else
payload
end
payload: payload_schema
}
|> Map.merge(stream_properties),
example:
%{
"event" => type,
"payload" => get_schema(payload).example |> Jason.encode!()
"payload" => payload_example
}
|> Map.merge(stream_example)
}
@ -262,7 +271,8 @@ defp delete_event do
allOf: [FlakeID],
example: "some-opaque-id"
},
payload_type: :string
payload_type: :string,
has_stream: false
)
end
@ -274,7 +284,7 @@ defp pleroma_respond_event do
%Schema{
type: :object,
title: "Results",
required: [:result],
required: [:result, :type],
properties: %{
result: %Schema{
type: :string,
@ -285,10 +295,15 @@ defp pleroma_respond_event do
type: :string,
title: "Error code",
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