Skip to content

Commit

Permalink
fix: unable to limit fields on route_patterns (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
Whoops authored Aug 9, 2023
1 parent f9cb0c9 commit 632b256
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion apps/api_web/lib/api_web/api_controller_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ defmodule ApiWeb.ApiControllerHelpers do
end

defp view_module_for_type(type) do
view_name = String.capitalize(type) <> "View"
view_name = Macro.camelize(type) <> "View"

Module.safe_concat([ApiWeb, view_name])
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule ApiWeb.RoutePatternController do
#{@description}
""")

common_index_parameters(__MODULE__)
common_index_parameters(__MODULE__, :route_pattern)

include_parameters()

Expand Down Expand Up @@ -126,6 +126,8 @@ defmodule ApiWeb.RoutePatternController do
#{@description}
""")

common_show_parameters(:route_pattern)

parameter(:id, :path, :string, "Unique identifier for route_pattern")
include_parameters()

Expand Down
8 changes: 8 additions & 0 deletions apps/api_web/test/api_web/api_controller_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ defmodule ApiWeb.ApiControllerHelpersTest do
}
end

test "works with types with multiple words" do
params = %{"route_pattern" => "name,typicality,canonical"}

assert ApiWeb.ApiControllerHelpers.filter_valid_field_params(%Plug.Conn{}, params) == %{
"route_pattern" => [:name, :typicality, :canonical]
}
end

test "rejects invalid types" do
params = %{
"bad_type" => "name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,22 @@ defmodule ApiWeb.RoutePatternControllerTest do
assert json_response(response, 404)
assert validate_resp_schema(response, schema, "NotFound")
end

test "can limit returned fields", %{conn: conn} do
State.RoutePattern.new_state([
%RoutePattern{id: "rp1", sort_order: 1},
%RoutePattern{id: "rp2", sort_order: 2},
%RoutePattern{id: "rp3", sort_order: 3}
])

params = %{"fields" => %{"route_pattern" => "sort_order"}}
conn = get(conn, route_pattern_path(conn, :index, params))

response = json_response(conn, 200)
attrs = Enum.map(response["data"], fn rp -> rp["attributes"] end)

assert [%{"sort_order" => 1}, %{"sort_order" => 2}, %{"sort_order" => 3}] = attrs
end
end

describe "show" do
Expand Down Expand Up @@ -565,6 +581,32 @@ defmodule ApiWeb.RoutePatternControllerTest do
assert json_response(response, 404)
assert validate_resp_schema(response, schema, "NotFound")
end

test "allows limiting returned fields", %{conn: conn} do
route_pattern = %RoutePattern{
id: "route pattern id",
route_id: "route id",
direction_id: 0,
name: "route pattern name",
time_desc: nil,
typicality: 1,
sort_order: 101,
representative_trip_id: "trip id",
canonical: false
}

State.RoutePattern.new_state([route_pattern])

conn =
get(
conn,
route_pattern_path(conn, :show, route_pattern, %{
"fields[route_pattern]" => "sort_order"
})
)

assert json_response(conn, 200)["data"]["attributes"] == %{"sort_order" => 101}
end
end

describe "swagger_path" do
Expand Down

0 comments on commit 632b256

Please sign in to comment.