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

[chore] fix hierarchy item depth representation

Eric Schubertbfdd90b214c0
diff --git a/lib/api/v3/custom_fields/hierarchy/hierarchical_item_aggregate.rb b/lib/api/v3/custom_fields/hierarchy/hierarchical_item_aggregate.rb
new file mode 100644
index 00000000000..d710c9297f2
--- /dev/null
+++ b/lib/api/v3/custom_fields/hierarchy/hierarchical_item_aggregate.rb
@@ -0,0 +1,50 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+module API
+ module V3
+ module CustomFields
+ module Hierarchy
+ class HierarchicalItemAggregate
+ attr_accessor :depth
+
+ delegate :id, :label, :short, :parent, :children, :root?, to: :item
+
+ def initialize(item:, depth:)
+ @item = item
+ @depth = depth
+ end
+
+ private
+
+ attr_accessor :item
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/custom_fields/hierarchy/hierarchy_item_representer.rb b/lib/api/v3/custom_fields/hierarchy/hierarchy_item_representer.rb
index fddbbd2a3f4..656218ef512 100644
--- a/lib/api/v3/custom_fields/hierarchy/hierarchy_item_representer.rb
+++ b/lib/api/v3/custom_fields/hierarchy/hierarchy_item_representer.rb
@@ -44,7 +44,10 @@ module API
property :short, render_nil: true
- property :depth
+ property :depth,
+ render_nil: true,
+ exec_context: :decorator,
+ getter: ->(*) { represented.depth < 0 ? nil : represented.depth }
link :parent do
next if represented.root?
diff --git a/lib/api/v3/custom_fields/hierarchy/item_api.rb b/lib/api/v3/custom_fields/hierarchy/item_api.rb
index 1231b9fb57a..ab2574ec16b 100644
--- a/lib/api/v3/custom_fields/hierarchy/item_api.rb
+++ b/lib/api/v3/custom_fields/hierarchy/item_api.rb
@@ -42,7 +42,9 @@ module API
get &::API::V3::Utilities::Endpoints::Show
.new(model: CustomField::Hierarchy::Item,
render_representer: HierarchyItemRepresenter,
- instance_generator: ->(*) { @custom_field_item })
+ instance_generator: ->(*) do
+ HierarchicalItemAggregate.new(item: @custom_field_item, depth: @custom_field_item.depth - 1)
+ end)
.mount
mount ItemBranchAPI
diff --git a/lib/api/v3/custom_fields/hierarchy/item_branch_api.rb b/lib/api/v3/custom_fields/hierarchy/item_branch_api.rb
index 68e7e98f37e..823b092c195 100644
--- a/lib/api/v3/custom_fields/hierarchy/item_branch_api.rb
+++ b/lib/api/v3/custom_fields/hierarchy/item_branch_api.rb
@@ -40,6 +40,7 @@ module API
::CustomFields::Hierarchy::HierarchicalItemService
.new
.get_branch(item: @custom_field_item)
+ .fmap { |items| items.map { |item| HierarchicalItemAggregate.new(item:, depth: item.depth - 1) } }
.either(
->(items) do
self_link = api_v3_paths.custom_field_item(@custom_field_item.id)
diff --git a/lib/api/v3/custom_fields/hierarchy/items_api.rb b/lib/api/v3/custom_fields/hierarchy/items_api.rb
index 253717ba4f7..b10e7e29c00 100644
--- a/lib/api/v3/custom_fields/hierarchy/items_api.rb
+++ b/lib/api/v3/custom_fields/hierarchy/items_api.rb
@@ -30,28 +30,13 @@ module API
module V3
module CustomFields
module Hierarchy
- class HierarchicalItemAggregate
- attr_accessor :depth
-
- delegate :id, :label, :short, :parent, :children, :root?, to: :item
-
- def initialize(item:, depth:)
- @item = item
- @depth = depth
- end
-
- private
-
- attr_accessor :item
- end
-
class ItemsAPI < ::API::OpenProjectAPI
include Dry::Monads[:result]
helpers do
def flatten_tree_hash(hash)
flat_list = []
- queue = [hash.merge({ depth: 0 })]
+ queue = [hash.merge({ depth: -1 })]
# From the service we get a hashed tree like this:
# {:a => {:b => {:c1 => {:d1 => {}}, :c2 => {:d2 => {}}}, :b2 => {}}}