Replace deprecated function builder_opts()

Hello, I’m struggling after upgrading Plug in a project to v1.14.2
In Plug is now a deprecated function called builder_opts()
Regarding the documentation you need to pass an atom to Plug.Builder
https://hexdocs.pm/plug/Plug.Builder.html#builder_opts/0

In the Plug.Router documentation it is explained, that all the opts passed in use Plug.Router will be passed to Plug.Builder

I tried several ways to give that argument somehow to the builder, but no way. My tests are still failing, because the assigns in the connection remain empty.

Maybe someone knows, how to solve that.

here is my current code which causes the deprecated warning.


defmodule MyApp.Endpoint do

  use Plug.Router

  plug(:match)
  plug(:dispatch, builder_opts())
...

Something like this should work

defmodule MyApp.Endpoint do
  use Plug.Router, copy_opts_to_assign: :builder_opts

  plug(:match)
  plug(:dispatch)

  def dispatch(conn, _) do
    super(conn, conn.assigns.builder_opts)
  end

Thank you very much! :pray:
It seems to work now. Even if it’s hard to understand how you get to this solution.

The new option makes builder opts available as an assign (on conn.assigns) with the given key. Then you need to pull out that value for that key to pass to dispatch (as before). You can either do that by wrapping dispatch in a custom plug or given dispatch/2 is defoverridable you can implement a custom function for it, which delegates to the generated one using super.