diff --git a/lib/apical/path.ex b/lib/apical/path.ex index f2cbac9..e467881 100644 --- a/lib/apical/path.ex +++ b/lib/apical/path.ex @@ -75,7 +75,6 @@ defmodule Apical.Path do version = Keyword.fetch!(opts, :version) root = Keyword.fetch!(opts, :root) - function = Keyword.get(opts, :alias, String.to_atom(operation_id)) plug_opts = opts @@ -84,10 +83,22 @@ defmodule Apical.Path do ) |> Keyword.merge(path_parameters: path_parameters, path: path) - controller = + {controller, function} = case Keyword.fetch(opts, :controller) do {:ok, controller} when is_atom(controller) -> - controller + case Keyword.get(opts, :alias) do + nil -> + case String.split(operation_id, ".", trim: true) do + [operation_id] -> {controller, String.to_atom(operation_id)} + parts -> + {alias_fun, module_parts} = List.pop_at(parts, -1) + + {Module.concat([controller] ++ module_parts), String.to_atom(alias_fun)} + end + + alias_fun -> + {controller, alias_fun} + end {:ok, controller} -> raise CompileError, diff --git a/test/plug/operation_mf_test.exs b/test/plug/operation_mf_test.exs new file mode 100644 index 0000000..124fdf7 --- /dev/null +++ b/test/plug/operation_mf_test.exs @@ -0,0 +1,43 @@ +defmodule ApicalTest.Plug.Operation.Module do + use ApicalTest.EndpointCase, with: Plug + alias Plug.Conn + + use Apical.Plug.Controller + + def fun(conn, _params) do + Conn.send_resp(conn, 200, "OK") + end + + test "GET /" do + assert %{ + status: 200, + body: "OK" + } = Req.get!("http://localhost:#{@port}/") + end +end + +defmodule ApicalTest.Plug.Operation.Module.Router do + use Apical.Plug.Router + + require Apical + + Apical.router_from_string( + """ + openapi: 3.1.0 + info: + title: TestGet + version: 1.0.0 + paths: + "/": + get: + operationId: Module.fun + responses: + "200": + description: OK + """, + for: Plug, + root: "/", + controller: ApicalTest.Plug.Operation, + encoding: "application/yaml" + ) +end