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
use parser for query props parsing
ulferts970591292e3c
diff --git a/app/controllers/queries/params_parser.rb b/app/controllers/queries/params_parser.rbindex f59b8a2fa27..cf7ec90e7a7 100644--- a/app/controllers/queries/params_parser.rb+++ b/app/controllers/queries/params_parser.rb@@ -40,41 +40,102 @@ module Queriesprivatedef parse_filters_from_params(params)- filters = params[:filters].split(/(?<!\\)&/)+ FilterParser.new(params[:filters]).parse+ end++ def parse_orders_from_params(params)+ JSON.parse(params[:sortBy])+ .to_h+ .map { |k, v| { attribute: k, direction: v } }+ rescue JSON::ParserError+ [{ attribute: 'invalid', direction: 'asc' }]+ end+ end++ class FilterParser+ def initialize(string)+ @buffer = StringScanner.new(string)+ end- filters.map do |filter|- filter_parts = filter.split+ def parse+ filters = []- {- attribute: filter_parts[0],- operator: filter_parts[1],- values: parse_filter_value(filter_parts[2..].join(' '))- }+ while [email protected]?+ filters << parse_filterend++ filtersend- def parse_filter_value(values)- if values.start_with?("[") && values.end_with?("]")- values[1..-2].scan(/['"](.*?)['"]/).flatten.map { |v| escape_filter_value(v) }- else- [escape_filter_value(values.gsub('\&', '&'))]+ private++ def parse_filter+ consume_ampersand++ {+ attribute: parse_name,+ operator: parse_operator,+ values: parse_values+ }+ end++ def consume_ampersand+ case @buffer.peek(1)+ when '&', /\s/+ @buffer.getch+ consume_ampersandendend- def escape_filter_value(value)- if value.start_with?('"') && value.end_with?('"')- value[1..-2].gsub('\&', '&').gsub('\"', '"')+ def parse_name+ @buffer.scan_until(/\s|\z/).strip+ end++ def parse_operator+ @buffer.scan_until(/\s|\z/).strip+ end++ def parse_values+ case @buffer.peek(1)+ when '"'+ parse_doublequoted_value+ when "'"+ parse_singlequoted_value+ when '['+ parse_array_value+ when '&'+ []else- value.gsub('\&', '&')+ parse_unguarded_valueendend- def parse_orders_from_params(params)- JSON.parse(params[:sortBy])- .to_h- .map { |k, v| { attribute: k, direction: v } }- rescue JSON::ParserError- [{ attribute: 'invalid', direction: 'asc' }]+ def parse_doublequoted_value+ @buffer.getch+ [@buffer.scan_until(/(?<!\\)"|\z/).delete_suffix('"').delete("\\")]+ end++ def parse_singlequoted_value+ @buffer.getch+ [@buffer.scan_until(/(?<!\\)'|\z/).delete_suffix("'").delete("\\")]+ end++ def parse_unguarded_value+ value = @buffer+ .scan_until(/&|\z/)+ .delete_suffix('&')++ [value]+ end++ def parse_array_value+ @buffer+ .scan_until(/]|\z/)+ .delete_suffix(']')+ .delete_prefix('[')+ .scan(/(?:'([^']*)')|(?:"([^"]*)")/)+ .flatten+ .compactendendenddiff --git a/frontend/src/stimulus/controllers/dynamic/filters.controller.ts b/frontend/src/stimulus/controllers/dynamic/filters.controller.tsindex 701e9569f91..714c306bf83 100644--- a/frontend/src/stimulus/controllers/dynamic/filters.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/filters.controller.ts@@ -210,7 +210,7 @@ export default class FiltersController extends Controller {const parsedValue = this.parseFilterValue(filterName, filterType, parsedOperator) as string[]|null;if (parsedValue) {- const valuesString = parsedValue.length > 1 ? `[${parsedValue.map((v) => `"${this.replaceAmpersand(v)}"`).join(',')}]` : this.replaceAmpersand(parsedValue[0]);+ const valuesString = parsedValue.length > 1 ? `[${parsedValue.map((v) => `"${this.replaceDoubleQuotes(v)}"`).join(',')}]` : `"${this.replaceDoubleQuotes(parsedValue[0])}"`;filters.push(`${filterName} ${parsedOperator} ${valuesString}`);}@@ -220,8 +220,8 @@ export default class FiltersController extends Controller {return filters;}- private replaceAmpersand(value:string) {- return value && value.length > 0 ? value.replace(/&/g, '\\&') : '';+ private replaceDoubleQuotes(value:string) {+ return value && value.length > 0 ? value.replace(/"/g, '\\"') : '';}private readonly operatorsWithoutValues = ['*', '!*', 't', 'w'];diff --git a/spec/controllers/queries/params_parser_spec.rb b/spec/controllers/queries/params_parser_spec.rbindex cab1819b38b..04552ac960e 100644--- a/spec/controllers/queries/params_parser_spec.rb+++ b/spec/controllers/queries/params_parser_spec.rb@@ -84,16 +84,18 @@ RSpec.describe Queries::ParamsParser, type: :model doendend- context 'with a single filter with a single value with , and & (escaped)' do+ context 'with a single filter with a single value with , and &' dolet(:params) do{- filters: 'active = something, or another thing \& something else'+ filters: 'active = something, or another thing & something else'}endit 'returns the parsed filter' do+ # This returns invalid filters but they will then be marked as invalidexpect(subject[:filters])- .to contain_exactly({ attribute: 'active', operator: '=', values: ["something, or another thing & something else"] })+ .to contain_exactly({ attribute: 'active', operator: '=', values: ["something, or another thing "] },+ { attribute: 'something', operator: 'else', values: [''] })endend@@ -110,7 +112,20 @@ RSpec.describe Queries::ParamsParser, type: :model doendend- context 'with a single filter with a no value' do+ context 'with a single filter with a single value with \' (escaped)' do+ let(:params) do+ {+ filters: "active = 'something, or another thing \\' something else'"+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '=', values: ["something, or another thing ' something else"] })+ end+ end++ context 'with a single filter with no value' dolet(:params) do{filters: 'cf_512 !* '@@ -119,7 +134,87 @@ RSpec.describe Queries::ParamsParser, type: :model doit 'returns the parsed filter' doexpect(subject[:filters])- .to contain_exactly({ attribute: 'cf_512', operator: '!*', values: [""] })+ .to contain_exactly({ attribute: 'cf_512', operator: '!*', values: [''] })+ end+ end++ context 'with multiple filters with the first having no value' do+ let(:params) do+ {+ filters: "active !* & id = \"1\""+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '!*', values: [] },+ { attribute: 'id', operator: '=', values: ["1"] })+ end+ end++ context 'with multiple filters with ampersand as a filter value' do+ let(:params) do+ {+ filters: "active = \"t\" & name_and_identifier ~ \"abc & def\""+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '=', values: ["t"] },+ { attribute: 'name_and_identifier', operator: '~', values: ["abc & def"] })+ end+ end++ context 'with a corrupt filter only having a key' do+ let(:params) do+ {+ filters: "active"+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '', values: [""] })+ end+ end++ context 'with a corrupt filter having opening braces but no closing ones' do+ let(:params) do+ {+ filters: "active = [\"t\", \"f\""+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '=', values: %w[t f] })+ end+ end++ context 'with a corrupt filter having opening double quotes but no closing ones' do+ let(:params) do+ {+ filters: 'active = "t'+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '=', values: %w[t] })+ end+ end++ context 'with a corrupt filter having opening single quotes but no closing ones' do+ let(:params) do+ {+ filters: "active = 't"+ }+ end++ it 'returns the parsed filter' do+ expect(subject[:filters])+ .to contain_exactly({ attribute: 'active', operator: '=', values: %w[t] })endend