Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#60672] fix sql representer

Eric Schubert59c51d1993f6
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 112ef2d7d71..28c5ee2afbb 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -33,30 +33,24 @@ module API
class << self
def ctes(walker_result)
- # Currently, there does not appear to be a way to correctly transform the filter_scope into SQL
- # (e.g. by calling #to_sql) so that the extra call to the database is avoided. The main problem
- # is that LEFT JOINS added via includes are not part of the SQL generated by #to_sql.
- count = walker_result.filter_scope.count
- projection = if count == 0
- # The following processes need the projection to return a non-empty response.
- # If the count is 0, though, the projection would return nothing.
- "SELECT 1"
- else
- walker_result.projection_scope
- .limit(sql_limit(walker_result))
- .offset(sql_offset(walker_result))
- .to_sql
- end
-
{
- projection:,
- total: "SELECT #{count}"
+ projection: walker_result.projection_scope
+ .limit(sql_limit(walker_result))
+ .offset(sql_offset(walker_result))
+ .to_sql,
+ # Currently, there does not appear to be a way to correctly transform the filter_scope into SQL
+ # (e.g. by calling #to_sql) so that the extra call to the database is avoided. The main problem
+ # is that LEFT JOINS added via includes are not part of the SQL generated by #to_sql.
+ total: "SELECT #{walker_result.filter_scope.count}"
}
end
private
- def select_from(_walker_result)
+ def select_from(walker_result)
+ # Do not use `projection` if the select is for `total` only, as total is calculated separately.
+ return "(SELECT 1)" if walker_result.url_query[:select] == { "total" => {} }
+
"projection"
end
diff --git a/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
index 3b5aec2cafe..20bbd234017 100644
--- a/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
@@ -60,6 +60,12 @@ RSpec.describe API::V3::Projects::ProjectSqlCollectionRepresenter, "rendering" d
create(:user, member_with_roles: { project => role })
end
+ shared_examples_for "successful rendering" do
+ it "renders as expected" do
+ expect(json).to be_json_eql(expected)
+ end
+ end
+
context "when rendering everything" do
let(:expected) do
{
@@ -103,10 +109,7 @@ RSpec.describe API::V3::Projects::ProjectSqlCollectionRepresenter, "rendering" d
}.to_json
end
- it "renders as expected" do
- expect(json)
- .to be_json_eql(expected)
- end
+ it_behaves_like "successful rendering"
end
context "when rendering only collection attributes" do
@@ -137,16 +140,25 @@ RSpec.describe API::V3::Projects::ProjectSqlCollectionRepresenter, "rendering" d
}.to_json
end
- it "renders as expected" do
- expect(json)
- .to be_json_eql(expected)
+ it_behaves_like "successful rendering"
+ end
+
+ context "if rendering only total" do
+ let(:select) { { "total" => {} } }
+ let(:expected) { { total: 1 }.to_json }
+
+ it_behaves_like "successful rendering"
+
+ context "if total is zero" do
+ let(:scope) { Project.none }
+ let(:expected) { { total: 0 }.to_json }
+
+ it_behaves_like "successful rendering"
end
end
context "when not having a project to render" do
- let(:scope) do
- Project.none
- end
+ let(:scope) { Project.none }
let(:select) do
{ "*" => {} }
@@ -175,9 +187,6 @@ RSpec.describe API::V3::Projects::ProjectSqlCollectionRepresenter, "rendering" d
}.to_json
end
- it "renders as expected" do
- expect(json)
- .to be_json_eql(expected)
- end
+ it_behaves_like "successful rendering"
end
end