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
robust handling of number prefixed project identifier on query parsing
ulferts99465836a9e8
diff --git a/lib/api/v3/queries/query_representer.rb b/lib/api/v3/queries/query_representer.rbindex bdd1db69cab..45473a00f35 100644--- a/lib/api/v3/queries/query_representer.rb+++ b/lib/api/v3/queries/query_representer.rb@@ -44,10 +44,13 @@ module APIsetter: ->(fragment:, **) {id = id_from_href "projects", fragment['href']- id = if id.to_i.nonzero?+ # In case an identifier is provided, which might+ # start with numbers, the id needs to be looked up+ # in the DB.+ id = if id.to_i.to_s == idid # return numerical IDelse- Project.where(identifier: id).pluck(:id).first # lookup Project by identifier+ Project.where(identifier: id).pick(:id) # lookup Project by identifierendrepresented.project_id = id if iddiff --git a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb b/spec/lib/api/v3/queries/query_representer_parsing_spec.rbindex 1ea0733acc0..74437cac2ad 100644--- a/spec/lib/api/v3/queries/query_representer_parsing_spec.rb+++ b/spec/lib/api/v3/queries/query_representer_parsing_spec.rb@@ -28,24 +28,23 @@require 'spec_helper'-describe ::API::V3::Queries::QueryRepresenter do+describe ::API::V3::Queries::QueryRepresenter, 'parsing' doinclude ::API::V3::Utilities::PathHelperlet(:query) { FactoryBot.build_stubbed(:query, project: project) }let(:project) { FactoryBot.build_stubbed(:project) }- let(:user) { double('current_user') }let(:representer) do- described_class.new(query, current_user: user, embed_links: true)+ described_class.new(query, current_user: current_user, embed_links: true)endlet(:permissions) { [] }let(:policy) do- policy_stub = double('policy stub')+ policy_stub = instance_double(QueryPolicy)allow(QueryPolicy).to receive(:new)- .with(user)+ .with(current_user).and_return(policy_stub)allow(policy_stub)@@ -60,13 +59,15 @@ describe ::API::V3::Queries::QueryRepresenter doendend+ current_user { FactoryBot.build_stubbed(:user) }+before dopolicyendsubject { representer.from_hash request_body }- describe 'parsing empty group_by (Regression #25606)' do+ describe 'empty group_by (Regression #25606)' dobefore doquery.group_by = 'project'end@@ -79,7 +80,7 @@ describe ::API::V3::Queries::QueryRepresenter do}end- it 'should unset group_by' do+ it 'unsets group_by' doexpect(query).to be_groupedexpect(query.group_by).to eq('project')@@ -87,7 +88,7 @@ describe ::API::V3::Queries::QueryRepresenter doendend- describe 'parsing highlighted_attributes', with_ee: [:conditional_highlighting] do+ describe 'highlighted_attributes', with_ee: [:conditional_highlighting] dolet(:request_body) do{'_links' => {@@ -96,12 +97,12 @@ describe ::API::V3::Queries::QueryRepresenter do}end- it 'should set highlighted_attributes' do+ it 'sets highlighted_attributes' doexpect(subject.highlighted_attributes).to eq(%i{type})endend- describe 'parsing ordered work packages' do+ describe 'ordered work packages' dolet(:request_body) do{'orderedWorkPackages' => {@@ -116,22 +117,81 @@ describe ::API::V3::Queries::QueryRepresenter doallow(query).to receive(:new_record?).and_return(new_record)end- context 'assuming query is new' do+ context 'if query is new' dolet(:new_record) { true }- it 'should set ordered_work_packages' do++ it 'sets ordered_work_packages' doorder = subject.ordered_work_packages.map { |el| [el.work_package_id, el.position] }expect(order).to match_array [[50, 0], [38, 1234], [102, 81234123]]endend- context 'assuming query is not new' do+ context 'if query is not new' dolet(:new_record) { false }- it 'should set ordered_work_packages' do- expect(query)- .not_to receive(:ordered_work_packages)+ it 'sets ordered_work_packages' do+ allow(query)+ .to receive(:ordered_work_packages)subject++ expect(query)+ .not_to have_received(:ordered_work_packages)+ end+ end+ end++ describe 'project' do+ let(:query) { FactoryBot.build_stubbed(:query, project: nil) }++ let(:request_body) do+ {+ '_links' => {+ 'project' => {+ 'href' => "/api/v3/projects/#{project_id}"+ }+ }+ }+ end++ before do+ scope = instance_double(ActiveRecord::Relation)++ allow(Project)+ .to receive(:where)+ .with(identifier: project_id)+ .and_return(scope)+ allow(scope)+ .to receive(:pick)+ .with(:id)+ .and_return(project.id)+ end++ context 'for a number only id' do+ let(:project_id) { project.id }++ it 'sets the project_id accordingly' do+ expect(subject.project_id)+ .to eql project.id+ end+ end++ context 'for a text only id (identifier)' do+ let(:project_id) { project.identifier }++ it 'deduces the id for the project_id accordingly' do+ expect(subject.project_id)+ .to eql project.id+ end+ end++ context 'for a text starting with numbers (identifier)' do+ let(:project) { FactoryBot.build_stubbed(:project, identifier: '5555-numbered-identifier') }+ let(:project_id) { project.identifier }++ it 'deduces the id for the project_id accordingly' do+ expect(subject.project_id)+ .to eql project.idendendend