1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.OpenapiSpec do
7 # Load Pleroma application to get version info
8 Application.load(:pleroma)
10 spec_json = Pleroma.Web.ApiSpec.spec(server_specific: false) |> Jason.encode!()
11 # to get rid of the structs
12 spec_regened = spec_json |> Jason.decode!()
14 check_specs!(spec_regened)
16 File.write(path, spec_json)
19 defp check_specs!(spec) do
20 with :ok <- check_specs(spec) do
24 IO.puts(IO.ANSI.format([:red, :bright, "Spec check failed, errors:"]))
25 Enum.map(errors, &IO.puts/1)
27 raise "Spec check failed"
31 def check_specs(spec) do
34 |> Enum.flat_map(fn {path, %{} = endpoints} ->
37 fn {method, endpoint} ->
38 with :ok <- check_endpoint(spec, endpoint) do
42 "#{endpoint["operationId"]} (#{method} #{path}): #{error}"
46 |> Enum.reject(fn res -> res == :ok end)
56 defp check_endpoint(spec, endpoint) do
57 valid_tags = available_tags(spec)
59 with {_, [_ | _] = tags} <- {:tags, endpoint["tags"]},
60 {_, []} <- {:unavailable, Enum.reject(tags, &(&1 in valid_tags))} do
66 {:unavailable, tags} ->
67 "Tags #{inspect(tags)} not available. Please add it in \"x-tagGroups\" in Pleroma.Web.ApiSpec"
71 defp available_tags(spec) do
73 |> Enum.flat_map(fn %{"tags" => tags} -> tags end)