Skip to content

Commit a9e3554

Browse files
committed
Use Plug helpers to parse variables from routes
1 parent 5471dbc commit a9e3554

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

lib/open_api_spex/operation.ex

+15-11
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,33 @@ defmodule OpenApiSpex.Operation do
6060
"""
6161
@spec from_route(PathItem.route) :: {:ok, t} | {:error, String.t()}
6262
def from_route(route) do
63-
operation = from_plug(route.plug, route.opts)
64-
check_all_path_params_declared(operation, route.path)
63+
operation = from_plug(route.plug, Map.get(route, :opts, []))
64+
65+
case route do
66+
%{path: path} -> check_all_path_params_declared(operation, path)
67+
_ -> {:ok, operation}
68+
end
6569
end
6670

6771
defp check_all_path_params_declared(operation, route_path) do
68-
pattern = ~r/{(.*?)}/
69-
expected_path_params =
70-
pattern
71-
|> Regex.scan(route_path)
72-
|> Enum.map(fn [_match, param] -> param end)
72+
{expected_path_params, _} = Plug.Router.Utils.build_path_match(route_path)
7373

7474
actual_path_params =
7575
operation.parameters
76-
|> Enum.filter(& &1.in == :path)
77-
|> Enum.map(& &1.name |> to_string)
76+
|> Enum.filter(&(&1.in == :path))
77+
|> Enum.map(& &1.name)
7878

7979
missing_params = expected_path_params -- actual_path_params
80+
8081
case missing_params do
81-
[] -> {:ok, operation}
82+
[] ->
83+
{:ok, operation}
84+
8285
_ ->
8386
message =
8487
"Operation for route: #{inspect(route_path)} " <>
85-
"did not declare path parameters: #{inspect(missing_params)}"
88+
"did not declare path parameters: #{inspect(missing_params)}"
89+
8690
{:error, message}
8791
end
8892
end

lib/open_api_spex/paths.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ defmodule OpenApiSpex.Paths do
1919
@spec from_router(module) :: {:ok, t} | {:error, String.t()}
2020
def from_router(router) do
2121
router.__routes__()
22-
|> Enum.map(fn route -> %{route | path: open_api_path(route.path)} end)
23-
|> Enum.group_by(fn route -> route.path end)
22+
|> Enum.group_by(fn route -> open_api_path(route.path) end)
2423
|> Enum.map(fn {path, routes} -> {path, PathItem.from_routes(routes)} end)
2524
|> Enum.reduce_while(%{}, fn
2625
{_path, {:error, reason}}, _acc -> {:halt, {:error, reason}}

test/operation_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ defmodule OpenApiSpex.OperationTest do
55

66
describe "Operation.from_route" do
77
test "succeeds when all path params present" do
8-
route = %{plug: UserController, opts: :show, path: "/users/{id}"}
8+
route = %{plug: UserController, opts: :show, path: "/users/:id"}
99
assert Operation.from_route(route) == {:ok, UserController.show_operation()}
1010
end
1111

1212
test "Fails on missing path params" do
13-
path = "/teams/{missing_team_id_param}/users/{id}"
13+
path = "/teams/:missing_team_id_param/users/:id"
1414
route = %{plug: UserController, opts: :show, path: path}
1515
assert {:error, message} = Operation.from_route(route)
1616
assert message =~ "missing_team_id_param"

0 commit comments

Comments
 (0)