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

support operationId that specifies a module name and function #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions lib/apical/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions test/plug/operation_mf_test.exs
Original file line number Diff line number Diff line change
@@ -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