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

reducing the interface of the sql walker

ulfertsa2fee5953c61
diff --git a/lib/api/decorators/sql_collection_representer.rb b/lib/api/decorators/sql_collection_representer.rb
index 43bc30e62c7..ce837305cda 100644
--- a/lib/api/decorators/sql_collection_representer.rb
+++ b/lib/api/decorators/sql_collection_representer.rb
@@ -50,7 +50,33 @@ module API
end
def href_query(walker_results, overrides)
- walker_results.url_query.merge(overrides).to_query
+ query_params = walker_results.url_query.merge(overrides)
+
+ if (select_params = query_params.delete(:select))
+ query_params[:select] = select_href_params(select_params).flatten.join(',')
+ end
+
+ # Embedding is not supported yet but parts of the functionality is already in place.
+ query_params.delete(:embed)
+
+ query_params.to_query
+ end
+
+ # Turns the nested values for select and embed into the external representation
+ # of a comma separated string. E.g.
+ # { '*' => {}, 'elements' => { '*' => {} } }
+ # is turned into
+ # '*,elements/*'
+ def select_href_params(params)
+ params.map do |key, value|
+ if value.empty?
+ key
+ else
+ select_href_params(value).map do |sub_value|
+ "#{key}/#{sub_value}"
+ end
+ end
+ end
end
end
diff --git a/lib/api/v3/utilities/endpoints/sql_fallbacked_index.rb b/lib/api/v3/utilities/endpoints/sql_fallbacked_index.rb
index cfae0ae3821..49274dc33f4 100644
--- a/lib/api/v3/utilities/endpoints/sql_fallbacked_index.rb
+++ b/lib/api/v3/utilities/endpoints/sql_fallbacked_index.rb
@@ -37,8 +37,6 @@ module API
if resulting_params[:select]
::API::V3::Utilities::SqlRepresenterWalker
.new(results,
- embed: { 'elements' => {} },
- select: resulting_params[:select],
current_user: User.current,
self_path: self_path,
url_query: resulting_params)
diff --git a/lib/api/v3/utilities/endpoints/sql_index.rb b/lib/api/v3/utilities/endpoints/sql_index.rb
index bfca63ae36c..1b42524d67c 100644
--- a/lib/api/v3/utilities/endpoints/sql_index.rb
+++ b/lib/api/v3/utilities/endpoints/sql_index.rb
@@ -38,8 +38,6 @@ module API
::API::V3::Utilities::SqlRepresenterWalker
.new(results,
- embed: { 'elements' => {} },
- select: resulting_params[:select],
current_user: User.current,
self_path: self_path,
url_query: resulting_params)
diff --git a/lib/api/v3/utilities/endpoints/sql_show.rb b/lib/api/v3/utilities/endpoints/sql_show.rb
index 12c0989cc1d..6085a761e19 100644
--- a/lib/api/v3/utilities/endpoints/sql_show.rb
+++ b/lib/api/v3/utilities/endpoints/sql_show.rb
@@ -60,9 +60,8 @@ module API
def render(scope)
::API::V3::Utilities::SqlRepresenterWalker
.new(scope.limit(1),
- embed: {},
- select: { '*' => {} },
- current_user: User.current)
+ current_user: User.current,
+ url_query: { select: { '*' => {} } })
.walk(render_representer)
end
diff --git a/lib/api/v3/utilities/sql_representer_walker.rb b/lib/api/v3/utilities/sql_representer_walker.rb
index 6aee56425a6..0735024d3e2 100644
--- a/lib/api/v3/utilities/sql_representer_walker.rb
+++ b/lib/api/v3/utilities/sql_representer_walker.rb
@@ -37,15 +37,12 @@ module API
def initialize(scope,
current_user:,
url_query: {},
- embed: {},
- select: {},
self_path: nil)
self.scope = scope
self.current_user = current_user
- self.embed = embed
- self.select = select
self.self_path = self_path
- self.url_query = url_query
+ # Hard wiring the properties ot embed is a work around until signaling the properties to embed is implemented
+ self.url_query = url_query.merge(embed: { 'elements' => {} })
end
def walk(start)
@@ -63,7 +60,7 @@ module API
result.projection_scope = current_representer.joins(select_for(stack), result.projection_scope)
end
- embedded_depth_first([], start) do |_, stack, current_representer|
+ embedded_depth_first([], start) do |_, _, current_representer|
result.ctes.merge!(current_representer.ctes(result))
end
@@ -80,12 +77,18 @@ module API
attr_accessor :scope,
:current_user,
- :embed,
- :select,
:sql,
:url_query,
:self_path
+ def embed
+ url_query[:embed]
+ end
+
+ def select
+ url_query[:select]
+ end
+
def embedded_depth_first(stack, current_representer, &block)
up_map = {}
diff --git a/spec/lib/api/v3/actions/action_sql_respresenter_rendering_spec.rb b/spec/lib/api/v3/actions/action_sql_respresenter_rendering_spec.rb
index 785728ca72a..0c883690dca 100644
--- a/spec/lib/api/v3/actions/action_sql_respresenter_rendering_spec.rb
+++ b/spec/lib/api/v3/actions/action_sql_respresenter_rendering_spec.rb
@@ -46,9 +46,8 @@ describe ::API::V3::Actions::ActionSqlRepresenter, 'rendering' do
subject(:json) do
::API::V3::Utilities::SqlRepresenterWalker
.new(scope,
- embed: {},
- select: { 'id' => {}, '_type' => {}, 'self' => {} },
- current_user: current_user)
+ current_user: current_user,
+ url_query: { select: { 'id' => {}, '_type' => {}, 'self' => {} } })
.walk(API::V3::Actions::ActionSqlRepresenter)
.to_json
end
diff --git a/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb b/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb
index ff4b2e406a2..8ecaf34be98 100644
--- a/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/capabilities/capability_sql_representer_rendering_spec.rb
@@ -58,10 +58,11 @@ describe ::API::V3::Capabilities::CapabilitySqlRepresenter, 'rendering' do
subject(:json) do
::API::V3::Utilities::SqlRepresenterWalker
- .new(scope,
- embed: {},
- select: { 'id' => {}, '_type' => {}, 'self' => {}, 'action' => {}, 'context' => {}, 'principal' => {} },
- current_user: current_user)
+ .new(
+ scope,
+ current_user: current_user,
+ url_query: { select: { 'id' => {}, '_type' => {}, 'self' => {}, 'action' => {}, 'context' => {}, 'principal' => {} } }
+ )
.walk(described_class)
.to_json
end
diff --git a/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
index 447311b7029..4fa365fe99b 100644
--- a/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_sql_collection_representer_rendering_spec.rb
@@ -32,11 +32,9 @@ describe ::API::V3::Projects::ProjectSqlCollectionRepresenter, 'rendering' do
subject(:json) do
::API::V3::Utilities::SqlRepresenterWalker
.new(scope,
- embed: { 'elements' => {} },
- select: select,
current_user: current_user,
self_path: 'some_path',
- url_query: { offset: 1, pageSize: 5 })
+ url_query: { offset: 1, pageSize: 5, select: select })
.walk(described_class)
.to_json
end
@@ -88,15 +86,14 @@ describe ::API::V3::Projects::ProjectSqlCollectionRepresenter, 'rendering' do
},
_links: {
self: {
- # TODO: reflect selections
- href: "some_path?offset=1&pageSize=5"
+ href: "some_path?offset=1&pageSize=5&select=%2A%2Celements%2F%2A"
},
changeSize: {
- href: "some_path?offset=1&pageSize=%7Bsize%7D",
+ href: "some_path?offset=1&pageSize=%7Bsize%7D&select=%2A%2Celements%2F%2A",
templated: true
},
jumpTo: {
- href: "some_path?offset=%7Boffset%7D&pageSize=5",
+ href: "some_path?offset=%7Boffset%7D&pageSize=5&select=%2A%2Celements%2F%2A",
templated: true
}
}
@@ -125,15 +122,14 @@ describe ::API::V3::Projects::ProjectSqlCollectionRepresenter, 'rendering' do
_embedded: {},
_links: {
self: {
- # TODO: reflect selections
- href: "some_path?offset=1&pageSize=5"
+ href: "some_path?offset=1&pageSize=5&select=%2A"
},
changeSize: {
- href: "some_path?offset=1&pageSize=%7Bsize%7D",
+ href: "some_path?offset=1&pageSize=%7Bsize%7D&select=%2A",
templated: true
},
jumpTo: {
- href: "some_path?offset=%7Boffset%7D&pageSize=5",
+ href: "some_path?offset=%7Boffset%7D&pageSize=5&select=%2A",
templated: true
}
}
diff --git a/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb b/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
index cae929cc517..8a9e3b92bbc 100644
--- a/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/projects/project_sql_representer_rendering_spec.rb
@@ -32,9 +32,8 @@ describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
subject(:json) do
::API::V3::Utilities::SqlRepresenterWalker
.new(scope,
- embed: {},
- select: select,
- current_user: current_user)
+ current_user: current_user,
+ url_query: { select: select })
.walk(described_class)
.to_json
end
@@ -50,7 +49,7 @@ describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
let(:role) { create(:role) }
- let(:select) { { 'id' => {}, '_type' => {}, 'self' => {}, 'name' => {}, 'ancestors' => {} } }
+ let(:select) { { '*' => {} } }
current_user do
create(:user,
@@ -58,7 +57,7 @@ describe ::API::V3::Projects::ProjectSqlRepresenter, 'rendering' do
member_through_role: role)
end
- context 'without an ancestor' do
+ context 'when rendering all supported properties' do
it 'renders as expected' do
expect(json)
.to be_json_eql(
diff --git a/spec/requests/api/v3/capability_resource_spec.rb b/spec/requests/api/v3/capability_resource_spec.rb
index 798f932520e..0fde0b1794f 100644
--- a/spec/requests/api/v3/capability_resource_spec.rb
+++ b/spec/requests/api/v3/capability_resource_spec.rb
@@ -131,7 +131,14 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
'values' => [other_user.id.to_s]
} }]
end
- let(:path) { "#{api_v3_paths.path_for(:capabilities, filters: filters, sort_by: [%i(id asc)])}&pageSize=2&offset=2" }
+ let(:path) do
+ api_v3_paths.path_for(:capabilities,
+ filters: filters,
+ sort_by: [%i(id asc)],
+ select: '*,elements/*',
+ page_size: 2,
+ offset: 2)
+ end
it 'returns a slice of the visible memberships' do
expect(subject.body)