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

include schema into attributes by timestamp

ulferts77ecf0115c1b
diff --git a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
index 3d3eb521ec2..fc830dc5cc0 100644
--- a/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
+++ b/lib/api/v3/work_packages/work_package_at_timestamp_representer.rb
@@ -56,6 +56,9 @@ module API
SUPPORTED_PROPERTIES = (SUPPORTED_NON_LINK_PROPERTIES + SUPPORTED_LINK_PROPERTIES).freeze
+ STATIC_NON_LINK_PROPERTIES = %w[_meta].freeze
+ STATIC_LINK_PROPERTIES = ['links', :schema, :self].freeze
+
def initialize(model, current_user:)
super(model, current_user:, embed_links:, timestamps: [model.timestamp])
end
@@ -77,10 +80,10 @@ module API
def rendered_properties
@rendered_properties ||= begin
- properties = changed_properties_as_api_name.intersection(SUPPORTED_PROPERTIES) + ['_meta']
+ properties = changed_properties_as_api_name.intersection(SUPPORTED_PROPERTIES) + STATIC_NON_LINK_PROPERTIES
if represented.exists_at_timestamp?
- properties + ["links", :self]
+ properties + STATIC_LINK_PROPERTIES
else
properties
end
diff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rb
index f46c02bbfae..8133ba9fc4c 100644
--- a/lib/api/v3/work_packages/work_package_collection_representer.rb
+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb
@@ -50,7 +50,7 @@ module API
@timestamps = timestamps
@query = query
- if timestamps.present? && (timestamps.count > 1 or timestamps.first.historic?)
+ if timestamps_active?
query_params[:timestamps] ||= API::V3::Utilities::PathHelper::ApiV3Path.timestamps_to_param_value(timestamps)
end
@@ -206,9 +206,19 @@ module API
end
def schema_pairs
- @schema_pairs ||= represented
- .map { |work_package| [work_package.project, work_package.type, work_package.available_custom_fields] }
- .uniq
+ @schema_pairs ||= begin
+ work_packages = if timestamps_active?
+ represented
+ .flat_map(&:at_timestamps)
+ else
+ represented
+ end
+
+ work_packages
+ .select(&:persisted?)
+ .uniq { |work_package| [work_package.project_id, work_package.type_id] }
+ .map { |work_package| [work_package.project, work_package.type, work_package.available_custom_fields] }
+ end
end
def all_cfs_of_project
@@ -313,6 +323,10 @@ module API
mime_type: 'application/atom+xml'
end
+ def timestamps_active?
+ timestamps.present? && timestamps.any?(&:historic?)
+ end
+
attr_reader :project,
:total_sums,
:embed_schemas
diff --git a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb
index 1828a161aca..5521ee2e715 100644
--- a/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_at_timestamp_representer_rendering_spec.rb
@@ -177,6 +177,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
@@ -211,6 +214,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
@@ -238,6 +244,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
@@ -268,6 +277,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
@@ -320,6 +332,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
@@ -344,23 +359,25 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
# {
# "attributesByTimestamp"=>
# [
- # # The part rendered by this representer
- # {
- # "_meta"=>{"exists"=>true, "timestamp"=>"2015-01-01T00:00:00Z", "matchesFilters"=>true},
- # "subject"=>"The original work package",
- # "startDate"=>nil,
- # "dueDate"=>nil,
- # "_links"=>
- # {"self"=>{"href"=>"/api/v3/work_packages/101?timestamps=2015-01-01T00%3A00%3A00Z",
- # "title"=>"The original work package"},
- # "type"=>{"href"=>"/api/v3/types/63", "title"=>"None"},
- # "priority"=>{"href"=>"/api/v3/priorities/3", "title"=>"Priority 1"},
- # "project"=>{"href"=>"/api/v3/projects/85", "title"=>"My Project No. 2"},
- # "status"=>{"href"=>"/api/v3/statuses/3", "title"=>"status 1"},
- # "responsible"=>{"href"=>nil},
- # "assignee"=>{"href"=>"/api/v3/users/68", "title"=>"Bob Bobbit"},
- # "parent"=>{"href"=>"/api/v3/work_packages/102"}
- # "version"=>{"href"=>nil}}
+ # # The part rendered by this representer
+ # {
+ # "_meta"=>{"exists"=>true, "timestamp"=>"2015-01-01T00:00:00Z", "matchesFilters"=>true},
+ # "subject"=>"The original work package",
+ # "startDate"=>nil,
+ # "dueDate"=>nil,
+ # "_links"=> {
+ # "self"=>{"href"=>"/api/v3/work_packages/101?timestamps=2015-01-01T00%3A00%3A00Z",
+ # "title"=>"The original work package"},
+ # "schema"=>{"href"=>"/api/v3/work_packages/schemas/85-63"},
+ # "type"=>{"href"=>"/api/v3/types/63", "title"=>"None"},
+ # "priority"=>{"href"=>"/api/v3/priorities/3", "title"=>"Priority 1"},
+ # "project"=>{"href"=>"/api/v3/projects/85", "title"=>"My Project No. 2"},
+ # "status"=>{"href"=>"/api/v3/statuses/3", "title"=>"status 1"},
+ # "responsible"=>{"href"=>nil},
+ # "assignee"=>{"href"=>"/api/v3/users/68", "title"=>"Bob Bobbit"},
+ # "parent"=>{"href"=>"/api/v3/work_packages/102"}
+ # "version"=>{"href"=>nil}
+ # }
# },
# {
# "_meta"=>{"exists"=>false, "timestamp"=>"PT0S", "matchesFilters"=>false}
@@ -421,6 +438,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackageAtTimestampRepresenter, 'render
'self' => {
'href' => api_v3_paths.work_package(work_package.id, timestamps: timestamp),
'title' => work_package.subject
+ },
+ 'schema' => {
+ 'href' => api_v3_paths.work_package_schema(work_package.project_id, work_package.type_id)
}
}
}.to_json
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 0cdc51b9964..83ed4831ebf 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
@@ -30,9 +30,11 @@ require 'spec_helper'
RSpec.describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
include API::V3::Utilities::PathHelper
+ create_shared_association_defaults_for_work_package_factory
let(:self_base_link) { '/api/v3/example' }
let(:work_packages) { WorkPackage.all }
+ let(:first_wp) { work_packages.first }
let(:user) { build_stubbed(:user) }
let(:query_params) { {} }
@@ -285,7 +287,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
_type: 'WorkPackage',
_links: {
self: {
- href: api_v3_paths.work_package(work_packages.first.id)
+ href: api_v3_paths.work_package(first_wp.id)
}
}
}.to_json
@@ -304,11 +306,8 @@ RSpec.describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
end
it 'has a schemas link' do
- ids = work_packages.map do |wp|
- [wp.project_id, wp.type_id]
- end
-
- path = api_v3_paths.work_package_schemas *ids
+ # All work packages in the collection are from the same project and have the same type
+ path = api_v3_paths.work_package_schemas [first_wp.project_id, first_wp.type_id]
expect(collection)
.to be_json_eql(path.to_json)
@@ -534,13 +533,58 @@ RSpec.describe API::V3::WorkPackages::WorkPackageCollectionRepresenter do
context 'when passing schemas' do
let(:embed_schemas) { true }
- it 'embeds a schema collection' do
- expected_path = api_v3_paths.work_package_schema(work_packages[0].project.id,
- work_packages[0].type.id)
+ it 'embeds a schema collection', :aggregate_failures do
+ expected_path = api_v3_paths.work_package_schema(first_wp.project.id,
+ first_wp.type.id)
expect(collection)
.to be_json_eql(expected_path.to_json)
.at_path('_embedded/schemas/_embedded/elements/0/_links/self/href')
+
+ # All work packages share the same project and type
+ expect(collection)
+ .to have_json_size(1)
+ .at_path('_embedded/schemas/_embedded/elements')
+ end
+
+ context 'when having timestamps leads to a different schema at the baseline time (because of a different type)',
+ with_settings: { journal_aggregation_time_minutes: 0 } do
+ let(:other_type) { create(:type) }
+ let(:timestamps) { [Timestamp.parse("2023-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
+ let!(:member) do
+ create(:member,
+ user: current_user,
+ project: first_wp.project,
+ roles: create_list(:role, 1, permissions: [:view_work_packages]))
+ end
+
+ before do
+ # Update the timestamp of the first work package to a different type forcing the inclusion of
+ # another schema and set the creation date to be before the baseline time.
+ first_wp = WorkPackage.first
+ first_wp.update(subject: 'Some new subject', author: current_user)
+ first_wp.update_columns(created_at: "2022-01-01T00:00:00Z")
+ first_wp.journals.first.update_columns(created_at: "2022-01-01T00:00:00Z", updated_at: "2022-01-01T00:00:00Z")
+ first_wp.journals.first.data.update_columns(type_id: other_type.id)
+ end
+
+ it 'embeds a schema collection', :aggregate_failures do
+ expect(collection)
+ .to have_json_size(2)
+ .at_path('_embedded/schemas/_embedded/elements')
+
+ expected_former_path = api_v3_paths.work_package_schema(first_wp.project.id,
+ first_wp.type.id)
+ expect(collection)
+ .to be_json_eql(expected_former_path.to_json)
+ .at_path('_embedded/schemas/_embedded/elements/0/_links/self/href')
+
+ expected_current_path = api_v3_paths.work_package_schema(first_wp.project.id,
+ other_type.id)
+ expect(collection)
+ .to be_json_eql(expected_current_path.to_json)
+ .at_path('_embedded/schemas/_embedded/elements/1/_links/self/href')
+ end
end
end