Namit

Namit

How to combine paths from multiple routers for Openapispex (swagger) documentation?

defmodule KayaanPrintsWeb.Router.AdminRouter do
  use KayaanPrintsWeb, :router

  pipeline :api do
    plug :accepts, ["json"]
    plug :fetch_session
  end

  scope "/v1" do
    pipe_through :api

    scope "/", KayaanPrintsWeb do
      get "/", DefaultController, :index

      forward "/medias/", Router.MediaRouter
      forward "/products/", Router.ProductRouter
      forward "/collections/", Router.CollectionRouter
    end
  end
end

This is the admin router which forwards requests to other routers. I want to create documentation for all of the endpoints of these 3 routers on a single page. so I have defined this module:

defmodule KayaanPrintsWeb.SuperAdminApiSpec do
  alias OpenApiSpex.{Components, Contact, Info, OpenApi, Paths, Server, SecurityScheme}
  alias KayaanPrintsWeb.Endpoint
  alias KayaanPrintsWeb.Router.AdminRouter

  @behaviour OpenApi

  @impl OpenApi
  def spec do
    %OpenApi{
      components: %Components{
        securitySchemes: %{
          "authorization" => %SecurityScheme{
            type: "http",
            scheme: "bearer",
            bearerFormat: "JWT"
          }
        }
      },
      servers: [
        Server.from_endpoint(Endpoint)
      ],
      info: %Info{
        title: "Kayaan Prints Super Admin API",
        description: "API documentation for Kayaan Prints Super Admin Management",
        contact: %Contact{
          name: "name",
          email: "email"
        },
        version: to_string(Application.spec(:kayaan_prints, :vsn))
      },
      paths: merge_paths(AdminRouter)
    }
    |> OpenApiSpex.resolve_schema_modules()
  end
end

And I have defined the super-admin router which contains the endpoint for swaggerui. Now when I open the swaggerui page, it says: No operations defined in spec!. So it’s not working for forwarded endpoints.

Then, I tried merging the paths of all the 3 routers like this:

....
paths: merge_paths([ProductRouter, MediaRouter, CollectionRouter])
....
defp merge_paths(routers) do
    routers
    |> Enum.flat_map(& &1.__routes__())
    |> Paths.from_routes()
  end

now it’s overwriting the operations with the same name. for example, ProductController and MediaController contains index function and hence :index operation. So the index operation from product controller will be overwritten by the index operation from the media controller. and at the end I get doc for only 5 endpoints out of many. Furthermore, they do not contain the correct endpoint prefix as seen in the image I have attached.

So is there any ideal way of listing all the endpoints from multiple routers altogether?

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement