Skip to content

Commit

Permalink
Merge pull request #56 from bonfire-networks/main
Browse files Browse the repository at this point in the history
support operationId that specifies a module name and function
  • Loading branch information
ityonemo authored Oct 23, 2024
2 parents 2b5aebf + a96c74b commit 992ad06
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
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

0 comments on commit 992ad06

Please sign in to comment.