Skip to content

Commit

Permalink
Merge pull request #223 from askuity/master
Browse files Browse the repository at this point in the history
Fix bug where templated routes are sometimes matched before specific routes
  • Loading branch information
mbuhot committed Jul 3, 2019
2 parents 6869934 + 0479d90 commit 9724933
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/phoenix_swagger/conn_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ defmodule PhoenixSwagger.ConnValidator do
end

defp find_matching_path(conn) do
found = Enum.find(:ets.tab2list(@table), fn({path, base_path, _}) ->
found =
:ets.tab2list(@table)
|> Enum.sort()
|> Enum.find(fn({path, base_path, _}) ->
base_path_segments = String.split(base_path || "", "/") |> tl
path_segments = String.split(path, "/") |> tl
path_info_without_base = remove_base_path(conn.path_info, base_path_segments)
Expand Down
18 changes: 18 additions & 0 deletions test/test_spec/swagger_test_spec_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"swagger": "2.0",
"paths": {
"/pets/cats": {
"get": {
"summary": "",
"responses": {},
"parameters": [],
"description": ""
}
}
},
"info": {
"version": "1.0",
"title": "Simple App"
},
"definitions": {}
}
10 changes: 9 additions & 1 deletion test/validate_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule ValidatePlugTest do
@table :validator_table

setup do
schema = Validator.parse_swagger_schema(["test/test_spec/swagger_test_spec.json", "test/test_spec/swagger_test_spec_2.json"])
schema = Validator.parse_swagger_schema(["test/test_spec/swagger_test_spec.json", "test/test_spec/swagger_test_spec_2.json", "test/test_spec/swagger_test_spec_3.json"])
on_exit fn ->
:ets.delete_all_objects(@table)
end
Expand Down Expand Up @@ -84,6 +84,14 @@ defmodule ValidatePlugTest do
= sent_resp(test_conn)
end

test "validation matches specific route before templated route" do
test_conn = init_conn(:get, "/api/pets/cats", %{}, %{})
test_conn = Validate.call(test_conn, [])
assert is_nil test_conn.status
assert is_nil test_conn.resp_body
assert test_conn.private[:phoenix_swagger][:valid]
end

defp init_conn(verb, path, body_params \\ %{}, path_params \\ %{}) do
conn(verb, path)
|> Map.put(:body_params, body_params)
Expand Down

0 comments on commit 9724933

Please sign in to comment.