From 05ac0aeafe6d5bffe9ca44bb25e41d46f3ee8223 Mon Sep 17 00:00:00 2001 From: Noah Betzen Date: Tue, 4 Oct 2022 10:25:48 -0700 Subject: [PATCH 1/2] Add more clarity to scope/3 vs scope/4 when using RenderSpec --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 11634c04..a00cef4d 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,20 @@ scope "/api" do end ``` +Note that the above snippet uses [`scope/3`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3); some Phoenix projects will instead use [`scope/4`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3), which adds an alias to all modules declared in the scope: +```elixir +scope "/api", MyAppWeb do + pipe_through :api + # Note the lack of `MyAppWeb` below for `UserController`, because the alias is now implicit + resources "/users", UserController, only: [:create, :index, :show] + # However, the alias now applies to `RenderSpec` as well, which will cause errors + get "/openapi", OpenApiSpex.Plug.RenderSpec, [] +end +``` + +In this case, the `RenderSpec` module will be aliased as `MyAppWeb.OpenApiSpex.Plug.RenderSpec`, which will cause linter errors and fail at runtime. In order to prevent this, remove the alias from the `scope` invocation (`MyAppWeb`) and fully qualify the other controllers/functions (e.g. `MyAppWeb.UserController` instead of just `UserController`) + + In development, to ensure the rendered spec is refreshed, you should disable caching with: ```elixir From 241122b5163691d1d1d6240a037f5c5d22d93928 Mon Sep 17 00:00:00 2001 From: Noah Betzen Date: Wed, 23 Aug 2023 10:08:08 -0700 Subject: [PATCH 2/2] Clarify scope/3 and scope/4 usage --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a00cef4d..a4f9259a 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,7 @@ scope "/api" do end ``` -Note that the above snippet uses [`scope/3`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3); some Phoenix projects will instead use [`scope/4`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3), which adds an alias to all modules declared in the scope: +Note that the above snippet uses [`scope/3`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/3). Some Phoenix projects will instead use [`scope/4`](https://hexdocs.pm/phoenix/Phoenix.Router.html#scope/4) which adds an alias to all modules declared in the scope, as shown below: ```elixir scope "/api", MyAppWeb do pipe_through :api