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

[#47207] added self links to storage file respresenter

Eric Schubert9d14b31895d0
diff --git a/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
index 6f710aebc1d..1a175c81dc0 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_file_representer.rb
@@ -30,8 +30,17 @@ module API::V3::StorageFiles
class StorageFileRepresenter < ::API::Decorators::Single
include API::Decorators::DateProperty
+ def initialize(model, storage, current_user:)
+ super(model, current_user:)
+
+ @storage_id = storage.id
+ end
+
link :self do
- { href: "#{::API::V3::URN_PREFIX}storages:storage_file:no_link_provided" }
+ {
+ href: api_v3_paths.storage_file(@storage_id, represented.id),
+ title: represented.name
+ }
end
property :id
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 753251eca2d..58908ed40c9 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -37,7 +37,7 @@ module API::V3::StorageFiles
get do
(files_query(@storage, current_user) >> execute_files_query(params[:parent]))
.match(
- on_success: ->(files) { API::V3::StorageFiles::StorageFilesRepresenter.new(files, current_user:) },
+ on_success: ->(files) { API::V3::StorageFiles::StorageFilesRepresenter.new(files, @storage, current_user:) },
on_failure: ->(error) { raise_error(error) }
)
end
@@ -46,7 +46,9 @@ module API::V3::StorageFiles
get do
(file_query(@storage, current_user) >> execute_files_query(params[:file_id]))
.match(
- on_success: ->(storage_file) { API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, current_user:) },
+ on_success: ->(storage_file) {
+ API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, @storage, current_user:)
+ },
on_failure: ->(error) { raise_error(error) }
)
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb b/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
index e460382a9b2..b392ca9a759 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_representer.rb
@@ -28,23 +28,33 @@
module API::V3::StorageFiles
class StorageFilesRepresenter < ::API::Decorators::Single
+ def initialize(model, storage, current_user:)
+ super(model, current_user:)
+
+ @storage = storage
+ end
+
link :self do
{ href: "#{::API::V3::URN_PREFIX}storages:storage_files:no_link_provided" }
end
collection :files,
getter: ->(*) do
- represented.files.map { |file| API::V3::StorageFiles::StorageFileRepresenter.new(file, current_user:) }
+ represented.files.map do |file|
+ API::V3::StorageFiles::StorageFileRepresenter.new(file, @storage, current_user:)
+ end
end,
exec_context: :decorator
property :parent,
- getter: ->(*) { API::V3::StorageFiles::StorageFileRepresenter.new(represented.parent, current_user:) },
+ getter: ->(*) { API::V3::StorageFiles::StorageFileRepresenter.new(represented.parent, @storage, current_user:) },
exec_context: :decorator
collection :ancestors,
getter: ->(*) do
- represented.ancestors.map { |file| API::V3::StorageFiles::StorageFileRepresenter.new(file, current_user:) }
+ represented.ancestors.map do |file|
+ API::V3::StorageFiles::StorageFileRepresenter.new(file, @storage, current_user:)
+ end
end,
exec_context: :decorator
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 6f0157d29ff..7aa17c78dc7 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -128,6 +128,10 @@ module OpenProject::Storages
"#{storage(storage_id)}/files"
end
+ add_api_path :storage_file do |storage_id, file_id|
+ "#{storage_files(storage_id)}/#{file_id}"
+ end
+
add_api_path :prepare_upload do |storage_id|
"#{storage(storage_id)}/files/prepare_upload"
end
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
index 57ddfc5c042..d8f8f421542 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
@@ -32,6 +32,7 @@ describe API::V3::StorageFiles::StorageFileRepresenter do
let(:user) { build_stubbed(:user) }
let(:created_at) { DateTime.now }
let(:last_modified_at) { DateTime.now }
+ let(:storage) { build_stubbed(:storage) }
let(:file) do
Storages::StorageFile.new(
42,
@@ -46,7 +47,7 @@ describe API::V3::StorageFiles::StorageFileRepresenter do
%i[readable writeable]
)
end
- let(:representer) { described_class.new(file, current_user: user) }
+ let(:representer) { described_class.new(file, storage, current_user: user) }
subject { representer.to_json }
@@ -95,4 +96,14 @@ describe API::V3::StorageFiles::StorageFileRepresenter do
let(:value) { file.permissions }
end
end
+
+ describe '_links' do
+ describe 'self' do
+ it_behaves_like 'has a titled link' do
+ let(:link) { 'self' }
+ let(:href) { "/api/v3/storages/#{storage.id}/files/#{file.id}" }
+ let(:title) { file.name }
+ end
+ end
+ end
end
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
index 5b31f1dcb8a..9b3f98132d6 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
@@ -30,6 +30,7 @@ require 'spec_helper'
describe API::V3::StorageFiles::StorageFilesRepresenter do
let(:user) { build_stubbed(:user) }
+ let(:storage) { build_stubbed(:storage) }
let(:created_at) { DateTime.now }
let(:last_modified_at) { DateTime.now }
@@ -82,7 +83,7 @@ describe API::V3::StorageFiles::StorageFilesRepresenter do
Storages::StorageFiles.new([file], parent, [ancestor])
end
- let(:representer) { described_class.new(files, current_user: user) }
+ let(:representer) { described_class.new(files, storage, current_user: user) }
subject { representer.to_json }
@@ -93,16 +94,20 @@ describe API::V3::StorageFiles::StorageFilesRepresenter do
it_behaves_like 'collection', :files do
let(:value) { files.files }
- let(:element_decorator) { API::V3::StorageFiles::StorageFileRepresenter }
+ let(:element_decorator) do
+ ->(value) { API::V3::StorageFiles::StorageFileRepresenter.new(value, storage, current_user: user) }
+ end
end
it_behaves_like 'collection', :ancestors do
let(:value) { files.ancestors }
- let(:element_decorator) { API::V3::StorageFiles::StorageFileRepresenter }
+ let(:element_decorator) do
+ ->(value) { API::V3::StorageFiles::StorageFileRepresenter.new(value, storage, current_user: user) }
+ end
end
it_behaves_like 'property', :parent do
- let(:value) { API::V3::StorageFiles::StorageFileRepresenter.new(files.parent, current_user: user) }
+ let(:value) { API::V3::StorageFiles::StorageFileRepresenter.new(files.parent, storage, current_user: user) }
end
end
end
diff --git a/spec/lib/api/v3/support/collection_examples.rb b/spec/lib/api/v3/support/collection_examples.rb
index e26b832190b..45c824f14ba 100644
--- a/spec/lib/api/v3/support/collection_examples.rb
+++ b/spec/lib/api/v3/support/collection_examples.rb
@@ -28,7 +28,7 @@
shared_examples_for 'collection' do |name|
it "has the #{name} property" do
- represented_elements = value.map { |v| element_decorator.new(v, current_user: user) }
+ represented_elements = value.map { |v| element_decorator.call(v) }
expect(subject)
.to be_json_eql(represented_elements.to_json)