CafeRacer

CafeRacer

OpenApiSpex.Plug.RenderSpec.init/1 is undefined

Trying to use open_api_spex in my phoenix project.

When I try compiling the project with mix a warning pops us
OpenApiTest.OpenApiSpex.Plug.RenderSpec.init/1 is undefined

I have my router defined as below:

defmodule OpenApiTestWeb.Router do
  use OpenApiTestWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
    plug OpenApiSpex.Plug.PutApiSpec, module: OpenApiTestWeb.ApiSpec
  end

  scope "/", OpenApiTestWeb do
    pipe_through :browser

    get "/", PageController, :index
    get "/payments", PaymentController, :create_payment
    get "/openapi", OpenApiSpex.Plug.RenderSpec, []
  end

  if Mix.env() in [:dev, :test] do
    import Phoenix.LiveDashboard.Router

    scope "/" do
      pipe_through :browser
      live_dashboard "/dashboard", metrics: OpenApiTestWeb.Telemetry
    end
  end
end

OpenApiSpex.Plug.RenderSpec module is present in the deps as below.

defmodule OpenApiSpex.Plug.RenderSpec do


      defmodule MyAppWeb.Router do
        use Phoenix.Router
        alias MyAppWeb.UserController

        pipeline :api do
          plug :accepts, ["json"]
          plug OpenApiSpex.Plug.PutApiSpec, module: MyAppWeb.ApiSpec
        end

        scope "/api" do
          pipe_through :api
          resources "/users", UserController, only: [:create, :index, :show]
          get "/openapi", OpenApiSpex.Plug.RenderSpec, []
        end
      end


  alias OpenApiSpex.Plug.PutApiSpec
  @behaviour Plug
  @json_encoder Enum.find([Jason, Poison], &Code.ensure_loaded?/1)

  @impl Plug
  def init(opts), do: opts

  @impl Plug
  def call(conn, _opts) do
    {spec, _} = PutApiSpec.get_spec_and_operation_lookup(conn)

    conn
    |> Plug.Conn.put_resp_content_type("application/json")
    |> Plug.Conn.send_resp(200, @json_encoder.encode!(spec))
  end
end

Could someone kindly help?

Marked As Solved

josevalim

josevalim

Creator of Elixir

Oh, I see. The issue is the scope. The scope ..., OpenApiTestWeb will nest everything inside under OpenApiTestWeb. Do this instead:

  scope "/api" do
    pipe_through :api
    get "/openapi", OpenApiSpex.Plug.RenderSpec, []  

    scope OpenApiTestWeb do
      ....
    end
  end

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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 47930 226
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement