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

baseline api: add self link to embedded baseline attributes

Sebastian Fiedlschuster72fe57940bfa
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index d2866d66cb2..7ac17647d60 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -484,6 +484,12 @@ module API
resources :work_package, except: :schema
+ def self.work_package(id, timestamps: nil)
+ "#{root}/work_packages/#{id}" + if timestamps.present?
+ "?" + {timestamps: timestamps_to_param_value(timestamps)}.to_query
+ end.to_s
+ end
+
def self.work_package_schema(project_id, type_id)
"#{root}/work_packages/schemas/#{project_id}-#{type_id}"
end
@@ -537,7 +543,8 @@ module API
end
def self.timestamps_to_param_value(timestamps)
- if timestamps.present? and timestamps.is_a? Array
+ timestamps = [timestamps] if timestamps.is_a? Timestamp
+ if timestamps.present? and timestamps.kind_of? Array
timestamps.collect { |timestamp| timestamp.absolute.iso8601 }.join(",")
end
end
diff --git a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
index 62801f667d1..20c89dad043 100644
--- a/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
+++ b/lib/api/v3/work_packages/eager_loading/historic_attributes.rb
@@ -35,6 +35,7 @@ module API::V3::WorkPackages::EagerLoading
work_package.attributes = work_package_with_historic_attributes.attributes.except('timestamp')
work_package.baseline_attributes = work_package_with_historic_attributes.baseline_attributes
work_package.attributes_by_timestamp = work_package_with_historic_attributes.attributes_by_timestamp
+ work_package.baseline_timestamp = work_package_with_historic_attributes.baseline_timestamp
end
def self.module
@@ -56,7 +57,7 @@ module API::V3::WorkPackages::EagerLoading
extend ActiveSupport::Concern
included do
- attr_accessor :baseline_attributes, :attributes_by_timestamp
+ attr_accessor :baseline_attributes, :attributes_by_timestamp, :baseline_timestamp
end
end
end
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 68cf3e5427a..52e42755872 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -52,6 +52,10 @@ module API
super(model, current_user:, embed_links:)
end
+ def self_v3_path(path, id_attribute)
+ api_v3_paths.work_package(represented.id, timestamps:)
+ end
+
self_link title_getter: ->(*) { represented.subject }
link :update,
@@ -446,12 +450,32 @@ module API
property :baseline_attributes,
as: :baselineAttributes,
if: ->(*) { respond_to?(:baseline_attributes) and baseline_attributes.present? },
+ getter: ->(*) do
+ baseline_attributes.to_h.merge({
+ "_links": {
+ "self": {
+ "href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: baseline_timestamp)
+ }
+ }
+ })
+ end,
embedded: true,
uncachable: true
property :attributes_by_timestamp,
as: :attributesByTimestamp,
if: ->(*) { respond_to?(:attributes_by_timestamp) and attributes_by_timestamp.present? },
+ getter: ->(*) do
+ attributes_by_timestamp.to_a.collect do |timestamp, attributes|
+ [timestamp, attributes.to_h.merge({
+ "_links": {
+ "self": {
+ "href": API::V3::Utilities::PathHelper::ApiV3Path.work_package(id, timestamps: Timestamp.parse(timestamp.dup))
+ }
+ }
+ })]
+ end.to_h
+ end,
embedded: true,
uncachable: true
diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
index 31200a7f05e..826903f0ded 100644
--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb
@@ -84,9 +84,11 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
let(:absolute_timestamps_query_param) { { timestamps: absolute_timestamp_strings.join(",") }.to_query }
it 'has the absolute timestamps within the self link' do
- is_expected
- .to include_json(absolute_timestamps_query_param.to_json)
- .at_path('_links/self/href')
+ Timecop.freeze do
+ is_expected
+ .to include_json(absolute_timestamps_query_param.to_json)
+ .at_path('_links/self/href')
+ end
end
end
end
@@ -539,22 +541,48 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
.to be_json_eql("The original work package".to_json)
.at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first.to_s}/subject")
end
- it 'embed the properties of the baseline work package in baselineAttributes' do
+
+ it 'embeds the link to the baseline work package in attributesByTimestamp' do
+ expect(collection)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first.to_s}/_links/self/href")
+ end
+
+ it 'embeds the properties of the baseline work package in baselineAttributes' do
expect(collection)
.to be_json_eql("The original work package".to_json)
.at_path("_embedded/elements/0/_embedded/baselineAttributes/subject")
end
+
+ it 'embeds the link to the baseline work package in baselineAttributes' do
+ expect(collection)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: timestamps.first).to_json)
+ .at_path("_embedded/elements/0/_embedded/baselineAttributes/_links/self/href")
+ end
+ end
+
+ shared_examples_for 'has the absolute timestamps within the self link' do
+ let(:absolute_timestamp_strings) { timestamps.collect { |timestamp| timestamp.absolute.iso8601 } }
+ let(:absolute_timestamps_query_param) { { timestamps: absolute_timestamp_strings.join(",") }.to_query }
+
+ it 'has the absolute timestamps within the self link' do
+ is_expected
+ .to include_json(absolute_timestamps_query_param.to_json)
+ .at_path('_embedded/elements/0/_links/self/href')
+ end
end
context 'baseline and current' do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
it_behaves_like 'includes the properties of the current work package'
it_behaves_like 'embeds the properties of the baseline work package'
+ it_behaves_like 'has the absolute timestamps within the self link'
end
context 'current only' do
let(:timestamps) { [Timestamp.parse("PT0S")] }
it_behaves_like 'includes the properties of the current work package'
+ it_behaves_like 'has the absolute timestamps within the self link'
end
context 'baseline only' do
@@ -564,11 +592,18 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
.to be_json_eql("The original work package".to_json)
.at_path('_embedded/elements/0/subject')
end
+ it_behaves_like 'has the absolute timestamps within the self link'
end
context 'empty' do
let(:timestamps) { [] }
it_behaves_like 'includes the properties of the current work package'
+
+ it 'has no timestamps within the self link' do
+ is_expected
+ .not_to include_json("timestamps".to_json)
+ .at_path('_embedded/elements/0/_links/self/href')
+ end
end
end
end
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 27383ddd52a..e6a1d554ae9 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
@@ -1345,6 +1345,12 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
.to be_json_eql('The original work package'.to_json)
.at_path('_embedded/baselineAttributes/subject')
end
+
+ it 'has a link to the baseline work package' do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: [timestamps.first]).to_json)
+ .at_path('_embedded/baselineAttributes/_links/self/href')
+ end
end
describe 'attributesByTimestamp' do
@@ -1360,6 +1366,15 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
.to be_json_eql('The current work package'.to_json)
.at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/subject")
end
+
+ it 'has a link to the work package at the timestamp' do
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: [timestamps[0]]).to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/_links/self/href")
+ expect(subject)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id, timestamps: [timestamps[1]]).to_json)
+ .at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/_links/self/href")
+ end
end
end
end
diff --git a/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb b/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb
index 9a9a7a154f0..f497076ef69 100644
--- a/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb
+++ b/spec/support/api/v3/work_packages/work_package_representer_eager_loading.rb
@@ -45,6 +45,10 @@ shared_context 'eager loaded work package representer' do
"PT0S" => {subject: "The current work package"}
})
+ allow(work_package)
+ .to receive(:baseline_timestamp)
+ .and_return(Timestamp.parse("2022-01-01T00:00:00+00:00"))
+
work_package
end
end