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

only embed direct relations in wp representer (#8831)

ulferts317d90444f7d
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 8596390a14a..84e5980a45f 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -535,6 +535,7 @@ module API
self_path = api_v3_paths.work_package_relations(represented.id)
visible_relations = represented
.visible_relations(current_user)
+ .direct
.non_hierarchy
.includes(::API::V3::Relations::RelationCollectionRepresenter.to_eager_load)
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index 7a3a73c98dc..3e7d0237885 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -1073,7 +1073,7 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
before do
allow(work_package)
- .to receive_message_chain(:visible_relations, :non_hierarchy, :includes)
+ .to receive_message_chain(:visible_relations, :direct, :non_hierarchy, :includes)
.and_return([relation])
end
diff --git a/spec/requests/api/v3/work_package_resource_spec.rb b/spec/requests/api/v3/work_package_resource_spec.rb
index 01db2aba6b6..e3daa475bd9 100644
--- a/spec/requests/api/v3/work_package_resource_spec.rb
+++ b/spec/requests/api/v3/work_package_resource_spec.rb
@@ -310,6 +310,35 @@ describe 'API v3 Work package resource',
.at_path('derivedDueDate')
end
end
+
+ describe 'relations' do
+ let(:directly_related_wp) do
+ FactoryBot.create(:work_package, project_id: project.id)
+ end
+ let(:transitively_related_wp) do
+ FactoryBot.create(:work_package, project_id: project.id)
+ end
+
+ let(:work_package) do
+ FactoryBot.create(:work_package,
+ project_id: project.id,
+ description: 'lorem ipsum').tap do |wp|
+
+ FactoryBot.create(:relation, relates: 1, from: wp, to: directly_related_wp)
+ FactoryBot.create(:relation, relates: 1, from: directly_related_wp, to: transitively_related_wp)
+ end
+ end
+
+ it 'embeds all direct relations' do
+ expect(subject)
+ .to be_json_eql(1.to_json)
+ .at_path('_embedded/relations/total')
+
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(directly_related_wp.id).to_json)
+ .at_path('_embedded/relations/_embedded/elements/0/_links/to/href')
+ end
+ end
end
context 'requesting nonexistent work package' do