Repository: falconry/falcon

Issue:
Also convert '1' and '0' as booleans from query string
I see that [tests](https://github.com/falconry/falcon/blob/master/tests/test_query_params.py#L194) specifically exclude `0` and `1` from valid values to be converted to boolean by `Request.get_param_as_bool` method.

I'd like to discuss this situation :)

I see no reason not to convert those values as they seem non ambiguous to me and there are a bunch of situations where 1 and 0 are de facto boolean (database outputs…), but we only deals with string in query string.

What is the rationale behind this?

Thanks! :)



Discussion:
+1 for supporting 1 and 0 for true and false.

It may not necessarily be standard (I'm not a Swift dev, so could be wrong either way), but I do see Swift apps sending 1 and 0 as ints for bool in HTTP GET requests.

I'm +1 on this also...FWIW, Facebook's Graph API also uses `1` and `0` for
true and false.

On Wed, Jan 13, 2016 at 7:00 AM, George Irwin notifications@github.com
wrote:

> +1 for supporting 1 and 0 for true and false.
> 
> It may not necessarily be standard (I'm not a Swift dev, so could be wrong
> either way), but I do see Swift apps sending 1 and 0 as ints for bool in
> HTTP GET requests.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/falconry/falcon/issues/684#issuecomment-171268937.

## 

.

+1 on that... I would also like to see the True and False python boolean types added as well (for use with the json module).


Resolving patch:
diff --git a/falcon/request.py b/falcon/request.py
--- a/falcon/request.py
+++ b/falcon/request.py
@@ -43,8 +43,8 @@
 DEFAULT_ERROR_LOG_FORMAT = (u'{0:%Y-%m-%d %H:%M:%S} [FALCON] [ERROR]'
                             u' {1} {2}{3} => ')
 
-TRUE_STRINGS = ('true', 'True', 'yes')
-FALSE_STRINGS = ('false', 'False', 'no')
+TRUE_STRINGS = ('true', 'True', 'yes', '1')
+FALSE_STRINGS = ('false', 'False', 'no', '0')
 WSGI_CONTENT_HEADERS = ('CONTENT_TYPE', 'CONTENT_LENGTH')
 
 
@@ -865,8 +865,8 @@ def get_param_as_bool(self, name, required=False, store=None,
 
         The following boolean strings are supported::
 
-            TRUE_STRINGS = ('true', 'True', 'yes')
-            FALSE_STRINGS = ('false', 'False', 'no')
+            TRUE_STRINGS = ('true', 'True', 'yes', '1')
+            FALSE_STRINGS = ('false', 'False', 'no', '0')
 
         Args:
             name (str): Parameter name, case-sensitive (e.g., 'detailed').


Test patch:
diff --git a/tests/test_query_params.py b/tests/test_query_params.py
--- a/tests/test_query_params.py
+++ b/tests/test_query_params.py
@@ -201,8 +201,8 @@ def test_int_neg(self):
             req.get_param_as_int, 'pos', min=0, max=10)
 
     def test_boolean(self):
-        query_string = ('echo=true&doit=false&bogus=0&bogus2=1&'
-                        't1=True&f1=False&t2=yes&f2=no&blank')
+        query_string = ('echo=true&doit=false&bogus=bar&bogus2=foo&'
+                        't1=True&f1=False&t2=yes&f2=no&blank&one=1&zero=0')
         self.simulate_request('/', query_string=query_string)
 
         req = self.resource.req
@@ -226,6 +226,8 @@ def test_boolean(self):
         self.assertEqual(req.get_param_as_bool('t2'), True)
         self.assertEqual(req.get_param_as_bool('f1'), False)
         self.assertEqual(req.get_param_as_bool('f2'), False)
+        self.assertEqual(req.get_param_as_bool('one'), True)
+        self.assertEqual(req.get_param_as_bool('zero'), False)
         self.assertEqual(req.get_param('blank'), None)
 
         store = {}