Defect #34615

'Search' falsy parameters are not respected

Added by Jakub G 8 months ago. Updated 7 months ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:Go MAEDA% Done:

0%

Category:REST API
Target version:4.0.8
Resolution:Fixed Affected version:

Description

I'd like to implement search feature using REST API, but i noticed that this feature do not respect passed falsy values (booleans nor numbers). The same behavior can be observed on page.

What i mean is that if i pass 'false' or '0' as value, it's treated like truthy value anyway.

For example, logically, this query:

https://www.redmine.org/projects/redmine/search?q=issue&issues=0&news=0&changesets=0&wiki_pages=1&messages=false&redmine_plugins=0

should return results of only wiki pages with word 'issue' (because for other parameters i passed falsy values: '0' or 'false'). But those values are ignored. Even when i pass empty parameter (like '...&messages=&...') it's stil not working properly.

Why is it so big deal?

Well, since on page there is dedicated form for searching, it's not a problem, but when i'm using API eg. in Node.js, it's more logical and convinient way to pass all search parameters but with bool values (or eventually empty when use 'null' or 'undefined' JS type).

P.S.: Tested on Redmine version 4.1.1

0001-Ignore-from-search-module-params-sent-with-empty-val.patch Magnifier (1.67 KB) Marius BALTEANU, 2021-01-31 22:19

Associated revisions

Revision 20738
Added by Go MAEDA 7 months ago

Ignore from search module params sent with empty values (#34615).

Patch by Marius BALTEANU.

Revision 20740
Added by Go MAEDA 7 months ago

Merged r20738 from trunk to 4.1-stable (#34615).

Revision 20741
Added by Go MAEDA 7 months ago

Merged r20738 from trunk to 4.0-stable (#34615).

History

#1 Updated by Marius BALTEANU 8 months ago

  • Status changed from New to Confirmed

#2 Updated by Marius BALTEANU 8 months ago

Thanks Jakub for reporting this issue, I was able to confirm the strange behaviour.

#3 Updated by Marius BALTEANU 8 months ago

I took a look and it seems that some params (for example: all_words, titles_only) already supports passing a "falsy" value, but not by passing 0 (titles_only=0) or false (titles_only=false), but by passing an empty value (titles_only=). In a first phase, I think we can extend this behaviour to module params as well.

The change is quite small, but I'll post a patch in the following days after I check the tests.

diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index d6bb2dd94..1a4a12fcc 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -63,7 +63,7 @@ class SearchController < ApplicationController
       @object_types = @object_types.select {|o| User.current.allowed_to?("view_#{o}".to_sym, projects_to_search)}
     end

-    @scope = @object_types.select {|t| params[t]}
+    @scope = @object_types.select {|t| params[t].present?}
     @scope = @object_types if @scope.empty?

     fetcher = Redmine::Search::Fetcher.new(
(END)

Jakub, please let me know if this cover your needs.

#4 Updated by Marius BALTEANU 8 months ago

  • Assignee set to Marius BALTEANU
  • Target version set to Candidate for next major release

#5 Updated by Jakub G 8 months ago

Marius thanks for review. Would be great if in the future i could pass "0" and/or "false", but for now it's totally OK to pass empty value as well :) I'll just map false and 0 to an empty string in my code and that will do the trick :)

#6 Updated by Marius BALTEANU 8 months ago

Here is the patch with a test.

#7 Updated by Go MAEDA 8 months ago

  • Target version changed from Candidate for next major release to 4.0.8

Setting the target version to 4.0.8.

#8 Updated by Go MAEDA 7 months ago

  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch. thank you.

#9 Updated by Go MAEDA 7 months ago

  • Status changed from Resolved to Closed

Committed the patch. Thank you.

Also available in: Atom PDF