Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more clarity to scope/3 vs scope/4 when using RenderSpec #507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/4) which adds an alias to all modules declared in the scope, as shown below:
```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
Expand Down