logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

openapi_spec.ex (1926B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Tasks.Pleroma.OpenapiSpec do
  5. def run([path]) do
  6. # Load Pleroma application to get version info
  7. Application.load(:pleroma)
  8. spec_json = Pleroma.Web.ApiSpec.spec(server_specific: false) |> Jason.encode!()
  9. # to get rid of the structs
  10. spec_regened = spec_json |> Jason.decode!()
  11. check_specs!(spec_regened)
  12. File.write(path, spec_json)
  13. end
  14. defp check_specs!(spec) do
  15. with :ok <- check_specs(spec) do
  16. :ok
  17. else
  18. {_, errors} ->
  19. IO.puts(IO.ANSI.format([:red, :bright, "Spec check failed, errors:"]))
  20. Enum.map(errors, &IO.puts/1)
  21. raise "Spec check failed"
  22. end
  23. end
  24. def check_specs(spec) do
  25. errors =
  26. spec["paths"]
  27. |> Enum.flat_map(fn {path, %{} = endpoints} ->
  28. Enum.map(
  29. endpoints,
  30. fn {method, endpoint} ->
  31. with :ok <- check_endpoint(spec, endpoint) do
  32. :ok
  33. else
  34. error ->
  35. "#{endpoint["operationId"]} (#{method} #{path}): #{error}"
  36. end
  37. end
  38. )
  39. |> Enum.reject(fn res -> res == :ok end)
  40. end)
  41. if errors == [] do
  42. :ok
  43. else
  44. {:error, errors}
  45. end
  46. end
  47. defp check_endpoint(spec, endpoint) do
  48. valid_tags = available_tags(spec)
  49. with {_, [_ | _] = tags} <- {:tags, endpoint["tags"]},
  50. {_, []} <- {:unavailable, Enum.reject(tags, &(&1 in valid_tags))} do
  51. :ok
  52. else
  53. {:tags, _} ->
  54. "No tags specified"
  55. {:unavailable, tags} ->
  56. "Tags #{inspect(tags)} not available. Please add it in \"x-tagGroups\" in Pleroma.Web.ApiSpec"
  57. end
  58. end
  59. defp available_tags(spec) do
  60. spec["x-tagGroups"]
  61. |> Enum.flat_map(fn %{"tags" => tags} -> tags end)
  62. end
  63. end