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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#55381] The available_projects api endpoint displays custom project fields incorrectly
Dombi Attila4fbc00296696
diff --git a/lib/api/v3/projects/project_eager_loading_wrapper.rb b/lib/api/v3/projects/project_eager_loading_wrapper.rbindex 68d4e9c8dc1..c096b34a413 100644--- a/lib/api/v3/projects/project_eager_loading_wrapper.rb+++ b/lib/api/v3/projects/project_eager_loading_wrapper.rb@@ -37,12 +37,15 @@ module APIclass << selfdef wrap(projects)- custom_fields = projects.first.available_custom_fields if projects.present?- ancestors = ancestor_projects(projects) if projects.present?+ if projects.present?+ custom_fields_by_project_id = custom_fields_from_projects++ ancestors = ancestor_projects(projects)+ endsuper.each do |project|- project.available_custom_fields = custom_fields+ project.available_custom_fields = custom_fields_by_project_id[project.id]project.ancestors_from_root = ancestors.select { |a| a.is_ancestor_of?(project) }.sort_by(&:lft)endend@@ -59,6 +62,15 @@ module APIprojects_table[:lft].lt(project.lft).and(projects_table[:rgt].gt(project.rgt))end++ def custom_fields_from_projects+ ProjectCustomFieldProjectMapping+ .eager_load(:project_custom_field)+ .merge(ProjectCustomField.visible)+ .each_with_object(Hash.new { |h, k| h[k] = [] }) do |mapping, acc|+ acc[mapping.project_id] << mapping.project_custom_field+ end+ endendendenddiff --git a/spec/lib/api/v3/projects/project_eager_loading_wrapper_spec.rb b/spec/lib/api/v3/projects/project_eager_loading_wrapper_spec.rbnew file mode 100644index 00000000000..c8cffa5402b--- /dev/null+++ b/spec/lib/api/v3/projects/project_eager_loading_wrapper_spec.rb@@ -0,0 +1,64 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++require "spec_helper"++RSpec.describe API::V3::Projects::ProjectEagerLoadingWrapper do+ shared_let(:projects) { create_list(:project, 3) }++ describe ".wrap" do+ subject(:loaded_projects) { described_class.wrap(projects) }++ it "returns wrapped projects with relations eager loaded" do+ expect(loaded_projects.size).to eq(projects.size)+ association_names = %i[@available_custom_fields @ancestors_from_root]+ loaded_projects.each do |loaded_project|+ association_names.each do |association|+ expect(loaded_project.__getobj__.instance_variables).to include(association)+ end+ end+ end++ context "with available custom fields" do+ let!(:text_project_custom_field) do+ create :text_project_custom_field, projects: [projects.second, projects.third]+ end++ let!(:string_project_custom_field) do+ create :string_project_custom_field, projects: [projects.third]+ end++ it "returns available custom fields for each project separately" do+ expect(loaded_projects.first.available_custom_fields).to eq([])+ expect(loaded_projects.second.available_custom_fields).to eq([text_project_custom_field])+ expect(loaded_projects.third.available_custom_fields)+ .to eq([text_project_custom_field, string_project_custom_field])+ end+ end+ end+enddiff --git a/spec/requests/api/v3/versions/available_projects_resource_spec.rb b/spec/requests/api/v3/versions/available_projects_resource_spec.rbindex 608fca4b282..37fd068fecf 100644--- a/spec/requests/api/v3/versions/available_projects_resource_spec.rb+++ b/spec/requests/api/v3/versions/available_projects_resource_spec.rb@@ -29,7 +29,7 @@require "spec_helper"require "rack/test"-RSpec.describe "API v3 members available projects resource" do+RSpec.describe "API v3 versions available projects resource" doinclude Rack::Test::Methodsinclude API::V3::Utilities::PathHelper@@ -78,7 +78,7 @@ RSpec.describe "API v3 members available projects resource" dosubject(:response) { last_response }- describe "GET api/v3/members/available_projects" do+ describe "GET api/v3/versions/available_projects" dolet(:projects) { [manage_project, view_project, unauthorized_project] }let(:path) { api_v3_paths.versions_available_projects }