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: make `attributesByTimestamp` an array rather than a hash

Sebastian Fiedlschuster2611fc968f0c
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index 05a42d6cae2..8d40b7ce702 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -341,7 +341,17 @@ module API
# last given timestamp (commonly the current time) match the filters of the
# query. https://github.com/opf/openproject/pull/11783
#
- 'matchesFilters': matches_query_filters_at_timestamp?(attributes_by_timestamp.keys.last)
+ 'matchesFilters': matches_query_filters_at_timestamp?(timestamps.last),
+
+ # This meta property states whether the work package exists at the last given
+ # timestamp (commonly the current time).
+ # https://github.com/opf/openproject/pull/11783#issuecomment-1374897874
+ #
+ 'exists': exists_at_timestamps.include?(timestamps.last),
+
+ # This meta property holds the timestamp of the data of the work package.
+ #
+ 'timestamp': timestamps.last.to_s
}
}
@@ -497,7 +507,7 @@ module API
as: :attributesByTimestamp,
if: ->(*) { respond_to?(:attributes_by_timestamp) and attributes_by_timestamp.present? },
getter: ->(*) do
- timestamps.to_h do |timestamp|
+ timestamps.collect do |timestamp|
attrs = attributes_by_timestamp[timestamp.to_s].to_h
if exists_at_timestamps.include?(timestamp)
attrs = attrs.merge({
@@ -511,11 +521,12 @@ module API
end
attrs = attrs.merge({
'_meta': {
+ 'timestamp': timestamp.to_s,
'matchesFilters': matches_query_filters_at_timestamp?(timestamp),
'exists': exists_at_timestamps.include?(timestamp)
}.compact
})
- [timestamp, attrs]
+ attrs
end
end,
embedded: 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 21f00737fd7..caa1639607e 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
@@ -552,13 +552,13 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
it 'embeds the properties of the baseline work package in attributesByTimestamp' do
expect(collection)
.to be_json_eql("The original work package".to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/subject")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/subject")
end
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}/_links/self/href")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/self/href")
end
it 'embeds the properties of the baseline work package in baselineAttributes' do
@@ -662,10 +662,10 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter do
it 'states whether the work package matches the query filters at the timestamp' do
expect(subject)
.to be_json_eql(true.to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps[0]}/_meta/matchesFilters")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters")
expect(subject)
.to be_json_eql(false.to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps[1]}/_meta/matchesFilters")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters")
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 74e13c6acc7..51c84dcea81 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
@@ -1375,38 +1375,38 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
describe 'attributesByTimestamp' do
- it 'has a hash with timestamp strings as keys' do
- expect(JSON.parse(subject)['_embedded']['attributesByTimestamp'].keys).to eq timestamps.map(&:to_s)
+ it 'has an array' do
+ expect(JSON.parse(subject)['_embedded']['attributesByTimestamp']).to be_an Array
end
it 'has the historic attributes for each timestamp when they differ from the current attributes' do
expect(subject)
.to be_json_eql('The original work package'.to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/subject")
+ .at_path("_embedded/attributesByTimestamp/0/subject")
end
it 'skips the historic attributes when they are the same as the current attributes' do
expect(subject)
- .to have_json_path("_embedded/attributesByTimestamp/#{timestamps[1]}")
+ .to have_json_path("_embedded/attributesByTimestamp/1")
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/#{timestamps[1]}/subject")
+ .not_to have_json_path("_embedded/attributesByTimestamp/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")
+ .at_path("_embedded/attributesByTimestamp/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")
+ .at_path("_embedded/attributesByTimestamp/1/_links/self/href")
end
it 'has no information about whether the work package matches the query filters at the timestamp' \
'because there are no filters without a query' do
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/#{timestamps[0]}/_meta/matchesFilters")
+ .not_to have_json_path("_embedded/attributesByTimestamp/0/_meta/matchesFilters")
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/#{timestamps[1]}/_meta/matchesFilters")
+ .not_to have_json_path("_embedded/attributesByTimestamp/1/_meta/matchesFilters")
end
end
@@ -1449,10 +1449,10 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
it 'states whether the work package matches the query filters at the timestamp' do
expect(subject)
.to be_json_eql(true.to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/_meta/matchesFilters")
+ .at_path("_embedded/attributesByTimestamp/0/_meta/matchesFilters")
expect(subject)
.to be_json_eql(false.to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps[1]}/_meta/matchesFilters")
+ .at_path("_embedded/attributesByTimestamp/1/_meta/matchesFilters")
end
end
diff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rb
index 60fbd842753..26dc0d8dfc4 100644
--- a/spec/requests/api/v3/work_packages/index_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/index_resource_spec.rb
@@ -297,16 +297,16 @@ describe 'API v3 Work package resource',
it 'embeds the attributesByTimestamp' do
expect(subject.body)
.to be_json_eql("The original work package".to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/subject")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/subject")
expect(subject.body)
- .to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.last}")
+ .to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/1")
end
it 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' do
expect(subject.body)
- .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/description")
+ .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/1/description")
expect(subject.body)
- .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.last}/description")
+ .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/2/description")
end
it 'has the current attributes as attributes' do
@@ -318,7 +318,7 @@ describe 'API v3 Work package resource',
it 'has an embedded link to the baseline work package' do
expect(subject.body)
.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')
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_links/self/href')
end
it 'has the absolute timestamps within the self links of the elements' do
@@ -366,16 +366,50 @@ describe 'API v3 Work package resource',
.to be_json_eql(true.to_json)
.at_path('_embedded/elements/0/_meta/matchesFilters')
end
+
+ it 'marks the work package as existing today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_meta/exists')
+ end
end
end
- describe "baselineAttributes" do
+ describe "attributesByTimestamp/0 (baseline attributes)" do
describe "_meta" do
describe "matchesFilters" do
it 'marks the work package as not matching the filters at the baseline time' do
expect(subject.body)
.to be_json_eql(false.to_json)
- .at_path('_embedded/elements/0/_embedded/baselineAttributes/_meta/matchesFilters')
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters')
+ end
+ end
+
+ describe "exists" do
+ it 'marks the work package as existing at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/exists')
+ end
+ end
+ end
+ end
+
+ describe "attributesByTimestamp/1 (current attributes)" do
+ describe "_meta" do
+ describe "matchesFilters" do
+ it 'marks the work package as matching the filters today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters')
+ end
+ end
+
+ describe "exists" do
+ it 'marks the work package as existing today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/exists')
end
end
end
@@ -397,6 +431,52 @@ describe 'API v3 Work package resource',
.to be_json_eql(false.to_json)
.at_path('_embedded/elements/0/_meta/matchesFilters')
end
+
+ it 'marks the work package as existing today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_meta/exists')
+ end
+ end
+
+ describe "attributesByTimestamp/0 (baseline attributes)" do
+ describe "_meta" do
+ describe "matchesFilters" do
+ it 'marks the work package as matching the filters at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters')
+ end
+ end
+
+ describe "exists" do
+ it 'marks the work package as existing at the baseline time' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/exists')
+ end
+ end
+ end
+ end
+
+ describe "attributesByTimestamp/1 (current attributes)" do
+ describe "_meta" do
+ describe "matchesFilters" do
+ it 'marks the work package as not matching the filters today' do
+ expect(subject.body)
+ .to be_json_eql(false.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters')
+ end
+ end
+
+ describe "exists" do
+ it 'marks the work package as existing today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/exists')
+ end
+ end
+ end
end
describe "baselineAttributes" do
@@ -445,13 +525,21 @@ describe 'API v3 Work package resource',
end
describe "attributesByTimestamp" do
- describe "baseline time" do
+ describe "0 (baseline attributes)" do
describe "_meta" do
+ describe "timestamp" do
+ it 'has the baseline timestamp, which is the first timestmap' do
+ expect(subject.body)
+ .to be_json_eql(timestamps.first.to_s.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/timestamp')
+ end
+ end
+
describe "exists" do
it 'marks the work package as not existing at the baseline time' do
expect(subject.body)
.to be_json_eql(false.to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_meta/exists")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/exists")
end
end
@@ -459,7 +547,7 @@ describe 'API v3 Work package resource',
it 'marks the work package as not matching the filters at the baseline time' do
expect(subject.body)
.to be_json_eql(false.to_json)
- .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_meta/matchesFilters")
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/_meta/matchesFilters")
end
end
end
@@ -467,7 +555,43 @@ describe 'API v3 Work package resource',
describe "_links" do
it 'is not present' do
expect(subject.body)
- .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/#{timestamps.first}/_links")
+ .not_to have_json_path("_embedded/elements/0/_embedded/attributesByTimestamp/0/_links")
+ end
+ end
+ end
+
+ describe "1 (current attributes)" do
+ describe "_meta" do
+ describe "timestamp" do
+ it 'has the current timestamp, which is the second timestamp' do
+ expect(subject.body)
+ .to be_json_eql(timestamps.last.to_s.to_json)
+ .at_path('_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/timestamp')
+ end
+ end
+
+ describe "exists" do
+ it 'marks the work package as existing today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/exists")
+ end
+ end
+
+ describe "matchesFilters" do
+ it 'marks the work package as matching the filters today' do
+ expect(subject.body)
+ .to be_json_eql(true.to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/1/_meta/matchesFilters")
+ end
+ end
+ end
+
+ describe "_links" do
+ it 'has a self link' do
+ expect(subject.body)
+ .to be_json_eql(api_v3_paths.work_package(work_package.id).to_json)
+ .at_path("_embedded/elements/0/_embedded/attributesByTimestamp/1/_links/self/href")
end
end
end
diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rb
index dce82b77c85..705f34b847f 100644
--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb
+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb
@@ -274,16 +274,16 @@ describe 'API v3 Work package resource',
it 'embeds the attributesByTimestamp' do
expect(subject)
.to be_json_eql("The original work package".to_json)
- .at_path("_embedded/attributesByTimestamp/#{timestamps.first}/subject")
+ .at_path("_embedded/attributesByTimestamp/0/subject")
expect(subject)
- .to have_json_path("_embedded/attributesByTimestamp/#{timestamps.last}")
+ .to have_json_path("_embedded/attributesByTimestamp/1")
end
it 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' do
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/#{timestamps.first}/description")
+ .not_to have_json_path("_embedded/attributesByTimestamp/0/description")
expect(subject)
- .not_to have_json_path("_embedded/attributesByTimestamp/#{timestamps.last}/description")
+ .not_to have_json_path("_embedded/attributesByTimestamp/1/description")
end
it 'has the current attributes as attributes' do
@@ -295,7 +295,7 @@ describe 'API v3 Work package resource',
it 'has an embedded 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')
+ .at_path('_embedded/attributesByTimestamp/0/_links/self/href')
end
it 'has the absolute timestamps within the self link' do