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
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.rbindex 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 APIas: :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_hif exists_at_timestamps.include?(timestamp)attrs = attrs.merge({@@ -511,11 +521,12 @@ module APIendattrs = attrs.merge({'_meta': {+ 'timestamp': timestamp.to_s,'matchesFilters': matches_query_filters_at_timestamp?(timestamp),'exists': exists_at_timestamps.include?(timestamp)}.compact})- [timestamp, attrs]+ attrsendend,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.rbindex 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 doit 'embeds the properties of the baseline work package in attributesByTimestamp' doexpect(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")endit 'embeds the link to the baseline work package in attributesByTimestamp' doexpect(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")endit 'embeds the properties of the baseline work package in baselineAttributes' do@@ -662,10 +662,10 @@ describe ::API::V3::WorkPackages::WorkPackageCollectionRepresenter doit 'states whether the work package matches the query filters at the timestamp' doexpect(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")endendenddiff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rbindex 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 doenddescribe '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 Arrayendit 'has the historic attributes for each timestamp when they differ from the current attributes' doexpect(subject).to be_json_eql('The original work package'.to_json)- .at_path("_embedded/attributesByTimestamp/#{timestamps[0]}/subject")+ .at_path("_embedded/attributesByTimestamp/0/subject")endit 'skips the historic attributes when they are the same as the current attributes' doexpect(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")endit 'has a link to the work package at the timestamp' doexpect(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")endit 'has no information about whether the work package matches the query filters at the timestamp' \'because there are no filters without a query' doexpect(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")endend@@ -1449,10 +1449,10 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter doit 'states whether the work package matches the query filters at the timestamp' doexpect(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")endenddiff --git a/spec/requests/api/v3/work_packages/index_resource_spec.rb b/spec/requests/api/v3/work_packages/index_resource_spec.rbindex 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' doexpect(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")endit 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' doexpect(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")endit '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' doexpect(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')endit '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')+ endendend- describe "baselineAttributes" do+ describe "attributesByTimestamp/0 (baseline attributes)" dodescribe "_meta" dodescribe "matchesFilters" doit 'marks the work package as not matching the filters at the baseline time' doexpect(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')endendend@@ -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+ endenddescribe "baselineAttributes" do@@ -445,13 +525,21 @@ describe 'API v3 Work package resource',enddescribe "attributesByTimestamp" do- describe "baseline time" do+ describe "0 (baseline attributes)" dodescribe "_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" doit 'marks the work package as not existing at the baseline time' doexpect(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")endend@@ -459,7 +547,7 @@ describe 'API v3 Work package resource',it 'marks the work package as not matching the filters at the baseline time' doexpect(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")endendend@@ -467,7 +555,43 @@ describe 'API v3 Work package resource',describe "_links" doit 'is not present' doexpect(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")endendenddiff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rbindex 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' doexpect(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")endit 'does not embed the attributes in attributesByTimestamp if they are the same as the current attributes' doexpect(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")endit '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' doexpect(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')endit 'has the absolute timestamps within the self link' do