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

add author and responsible to wp resource signaling

ulferts2fdfa45d3a14
diff --git a/lib/api/decorators/sql/hal.rb b/lib/api/decorators/sql/hal.rb
index d839cb9836a..59ad988d7f4 100644
--- a/lib/api/decorators/sql/hal.rb
+++ b/lib/api/decorators/sql/hal.rb
@@ -141,7 +141,7 @@ module API
SQL
else
<<-SQL.squish
- '#{name}', json_build_object(#{link_attributes.join(', ')})
+ '#{name}', '{ "href": null }'::jsonb || json_strip_nulls(json_build_object(#{link_attributes.join(', ')}))::jsonb
SQL
end
end
@@ -177,9 +177,9 @@ module API
def select_sql(select, walker_result)
<<~SELECT
- json_strip_nulls(json_build_object(
+ json_build_object(
#{json_object_string(select, walker_result)}
- ))
+ )
SELECT
end
@@ -280,9 +280,9 @@ module API
return if json_object.blank?
<<~SELECT
- '#{namespace}', json_strip_nulls(json_build_object(
+ '#{namespace}', json_build_object(
#{json_object}
- ))
+ )
SELECT
end
diff --git a/lib/api/decorators/sql/hal_associated_resource.rb b/lib/api/decorators/sql/hal_associated_resource.rb
index 255cb7bc414..4a7f702384c 100644
--- a/lib/api/decorators/sql/hal_associated_resource.rb
+++ b/lib/api/decorators/sql/hal_associated_resource.rb
@@ -49,7 +49,8 @@ module API
CASE #{table_name}_type
WHEN 'Group' THEN format('#{api_v3_paths.group('%s')}', #{column_name})
WHEN 'PlaceholderUser' THEN format('#{api_v3_paths.placeholder_user('%s')}', #{column_name})
- ELSE format('#{api_v3_paths.user('%s')}', #{column_name})
+ WHEN 'User' THEN format('#{api_v3_paths.user('%s')}', #{column_name})
+ ELSE NULL
END
SQL
}
@@ -63,11 +64,16 @@ module API
" || ' ' || "
end
+ user_format = User::USER_FORMATS_STRUCTURE[Setting.user_format]
+ .map { |column| "#{table_name}_#{column}"}
+ .join(join_string)
+
<<~SQL.squish
CASE #{table_name}_type
- WHEN 'Group' THEN lastname
- WHEN 'PlaceholderUser' THEN lastname
- ELSE #{User::USER_FORMATS_STRUCTURE[Setting.user_format].join(join_string)}
+ WHEN 'Group' THEN #{table_name}_lastname
+ WHEN 'PlaceholderUser' THEN #{table_name}_lastname
+ WHEN 'User' THEN #{user_format}
+ ELSE NULL
END
SQL
}
@@ -76,10 +82,10 @@ module API
def associated_user_link_join(table_name, column_name)
{ table: :users,
condition: "#{table_name}.id = #{column_name}",
- select: ["#{table_name}.firstname",
- "#{table_name}.lastname",
- "#{table_name}.login",
- "#{table_name}.mail",
+ select: ["#{table_name}.firstname #{table_name}_firstname",
+ "#{table_name}.lastname #{table_name}_lastname",
+ "#{table_name}.login #{table_name}_login",
+ "#{table_name}.mail #{table_name}_mail",
"#{table_name}.type #{table_name}_type"] }
end
end
diff --git a/lib/api/v3/work_packages/work_package_sql_representer.rb b/lib/api/v3/work_packages/work_package_sql_representer.rb
index 02b47a5f548..84b8f3ed012 100644
--- a/lib/api/v3/work_packages/work_package_sql_representer.rb
+++ b/lib/api/v3/work_packages/work_package_sql_representer.rb
@@ -44,9 +44,13 @@ module API
condition: 'projects.id = project_id',
select: ['projects.name project_name'] }
+ associated_user_link :author
+
associated_user_link :assignee,
column_name: :assigned_to_id
+ associated_user_link :responsible
+
property :_type,
representation: ->(*) { "'WorkPackage'" }
diff --git a/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
index d24b52ea88e..99d857908d8 100644
--- a/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_sql_representer_rendering_spec.rb
@@ -46,10 +46,14 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
let(:rendered_work_package) do
create(:work_package,
project: project,
- assigned_to: assignee)
+ assigned_to: assignee,
+ author: author,
+ responsible: responsible)
end
let(:project) { create(:project) }
- let(:assignee) { create(:user) }
+ let(:assignee) { nil }
+ let(:author) { create(:user) }
+ let(:responsible) { nil }
let(:select) { { '*' => {} } }
@@ -73,8 +77,14 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
title: project.name
},
assignee: {
- href: api_v3_paths.user(assignee.id),
- title: assignee.name
+ href: nil
+ },
+ responsible: {
+ href: nil
+ },
+ author: {
+ href: api_v3_paths.user(author.id),
+ title: author.name
}
}
}
@@ -86,16 +96,18 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
end
end
- describe 'assignee link' do
- let(:select) { { 'assignee' => {} } }
+ shared_examples_for 'principal link' do |link_name, only_user: false|
+ let(:select) { { link_name => {} } }
context 'with a user' do
+ let(:principal_object) { create(:user) }
+
let(:expected) do
{
_links: {
- assignee: {
- href: api_v3_paths.user(assignee.id),
- title: assignee.name
+ link_name => {
+ href: api_v3_paths.user(principal_object.id),
+ title: principal_object.name
}
}
}
@@ -107,44 +119,64 @@ describe ::API::V3::WorkPackages::WorkPackageSqlRepresenter, 'rendering' do
end
end
- context 'with a group' do
- let(:assignee) { create(:group) }
-
- let(:expected) do
- {
- _links: {
- assignee: {
- href: api_v3_paths.group(assignee.id),
- title: assignee.name
+ unless only_user
+ context 'with a group' do
+ let(:principal_object) { create(:group) }
+
+ let(:expected) do
+ {
+ _links: {
+ link_name => {
+ href: api_v3_paths.group(principal_object.id),
+ title: principal_object.name
+ }
}
}
- }
- end
+ end
- it 'renders as expected' do
- expect(json)
- .to be_json_eql(expected.to_json)
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
end
- end
- context 'with a placeholder user' do
- let(:assignee) { create(:placeholder_user) }
+ context 'with a placeholder user' do
+ let(:principal_object) { create(:placeholder_user) }
- let(:expected) do
- {
- _links: {
- assignee: {
- href: api_v3_paths.placeholder_user(assignee.id),
- title: assignee.name
+ let(:expected) do
+ {
+ _links: {
+ link_name => {
+ href: api_v3_paths.placeholder_user(principal_object.id),
+ title: principal_object.name
+ }
}
}
- }
- end
+ end
- it 'renders as expected' do
- expect(json)
- .to be_json_eql(expected.to_json)
+ it 'renders as expected' do
+ expect(json)
+ .to be_json_eql(expected.to_json)
+ end
end
end
end
+
+ describe 'assignee link' do
+ it_behaves_like 'principal link', 'assignee' do
+ let(:assignee) { principal_object }
+ end
+ end
+
+ describe 'responsible link' do
+ it_behaves_like 'principal link', 'responsible' do
+ let(:responsible) { principal_object }
+ end
+ end
+
+ describe 'author link' do
+ it_behaves_like 'principal link', 'author', only_user: true do
+ let(:author) { principal_object }
+ end
+ end
end