From: Andy Allan Date: Wed, 26 Jul 2023 15:45:00 +0000 (+0100) Subject: Merge pull request #3933 from AntonKhorev/api-changesets-limit X-Git-Tag: live~1150 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/925d12cc8176414d0f9531832862a5825783d132?hp=30201e9e7f5954c336912515e486d4aaa5c34252 Merge pull request #3933 from AntonKhorev/api-changesets-limit Add limit parameter to api changesets query --- diff --git a/app/controllers/api/changesets_controller.rb b/app/controllers/api/changesets_controller.rb index 56070951a..a08edff53 100644 --- a/app/controllers/api/changesets_controller.rb +++ b/app/controllers/api/changesets_controller.rb @@ -19,6 +19,9 @@ module Api # Helper methods for checking consistency include ConsistencyValidations + DEFAULT_QUERY_LIMIT = 100 + MAX_QUERY_LIMIT = 100 + ## # Return XML giving the basic info about the changeset. Does not # return anything about the nodes, ways and relations in the changeset. @@ -171,7 +174,7 @@ module Api changesets = conditions_ids(changesets, params["changesets"]) # sort and limit the changesets - changesets = changesets.order("created_at DESC").limit(100) + changesets = changesets.order("created_at DESC").limit(result_limit) # preload users, tags and comments, and render result @changesets = changesets.preload(:user, :changeset_tags, :comments) @@ -383,5 +386,19 @@ module Api changesets.where(:id => ids) end end + + ## + # Get the maximum number of results to return + def result_limit + if params[:limit] + if params[:limit].to_i.positive? && params[:limit].to_i <= MAX_QUERY_LIMIT + params[:limit].to_i + else + raise OSM::APIBadUserInput, "Changeset limit must be between 1 and #{MAX_QUERY_LIMIT}" + end + else + DEFAULT_QUERY_LIMIT + end + end end end diff --git a/test/controllers/api/changesets_controller_test.rb b/test/controllers/api/changesets_controller_test.rb index cb82b5664..40b8bc9f2 100644 --- a/test/controllers/api/changesets_controller_test.rb +++ b/test/controllers/api/changesets_controller_test.rb @@ -1938,6 +1938,31 @@ module Api assert_response :bad_request, "should be a bad request since changesets is empty" end + ## + # test the query functionality of changesets with the limit parameter + def test_query_limit + user = create(:user) + changeset1 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 1, 1, 0, 0, 0), :closed_at => Time.utc(2008, 1, 2, 0, 0, 0)) + changeset2 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 2, 1, 0, 0, 0), :closed_at => Time.utc(2008, 2, 2, 0, 0, 0)) + changeset3 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 3, 1, 0, 0, 0), :closed_at => Time.utc(2008, 3, 2, 0, 0, 0)) + changeset4 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 4, 1, 0, 0, 0), :closed_at => Time.utc(2008, 4, 2, 0, 0, 0)) + changeset5 = create(:changeset, :closed, :user => user, :created_at => Time.utc(2008, 5, 1, 0, 0, 0), :closed_at => Time.utc(2008, 5, 2, 0, 0, 0)) + + get changesets_path + assert_response :success + assert_changesets [changeset5, changeset4, changeset3, changeset2, changeset1] + + get changesets_path(:limit => "3") + assert_response :success + assert_changesets [changeset5, changeset4, changeset3] + + get changesets_path(:limit => "0") + assert_response :bad_request + + get changesets_path(:limit => "101") + assert_response :bad_request + end + ## # check that errors are returned if garbage is inserted # into query strings