Skip to content

Commit

Permalink
Allow multi fetch request parameter to contain only versioned entries
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Apr 2, 2024
1 parent 307ca6d commit 5fda3c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/controllers/api/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ class ElementsController < ApiController
def index
raise OSM::APIBadUserInput, "The parameter #{controller_name} is required, and must be of the form #{controller_name}=ID[vVER][,ID[vVER][,ID[vVER]...]]" unless params[controller_name]

id_ver_strings, id_strings = params[controller_name].split(",").partition { |iv| iv.include? "v" }
search_strings = params[controller_name].split(",")

raise OSM::APIBadUserInput, "No #{controller_name} were given to search for" if search_strings.empty?

id_ver_strings, id_strings = search_strings.partition { |iv| iv.include? "v" }
id_vers = id_ver_strings.map { |iv| iv.split("v", 2).map(&:to_i) }
ids = id_strings.map(&:to_i)

raise OSM::APIBadUserInput, "No #{controller_name} were given to search for" if ids.empty?

result = current_model.find(ids)
unless id_vers.empty?
result += old_model.find(id_vers)
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/api/nodes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,15 @@ def test_index_with_versions
node1 = create(:node)
node2 = create(:node, :with_history, :version => 2)

# test a working call only with versions
get nodes_path(:nodes => "#{node2.id}v1,#{node2.id}v2")
assert_response :success
assert_select "osm" do
assert_select "node", :count => 2
assert_select "node[id='#{node2.id}'][version='1']", :count => 1
assert_select "node[id='#{node2.id}'][version='2']", :count => 1
end

# test a working call
get nodes_path(:nodes => "#{node1.id},#{node2.id}v1,#{node2.id}v2")
assert_response :success
Expand Down

0 comments on commit 5fda3c2

Please sign in to comment.