Skip to content

Commit

Permalink
Merge pull request #130 from rafaelcaricio/handle-boolean-types-in-de…
Browse files Browse the repository at this point in the history
…fault

Handle boolean values correctly in default property
  • Loading branch information
jmcs committed Feb 2, 2016
2 parents b70c953 + 465b52c commit 6a6144f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion connexion/utils.py
Expand Up @@ -131,7 +131,9 @@ def boolean(s):
>>> boolean('false')
False
'''
if not hasattr(s, 'lower'):
if isinstance(s, bool):
return s
elif not hasattr(s, 'lower'):
raise ValueError('Invalid boolean value')
elif s.lower() == 'true':
return True
Expand Down
10 changes: 10 additions & 0 deletions tests/fakeapi/api.yaml
Expand Up @@ -616,6 +616,16 @@ paths:
in: query
default: 1

/test-bool-param:
get:
summary: Test usage of boolean default value
operationId: fakeapi.hello.test_bool_default_param
parameters:
- name: thruthiness
type: boolean
in: query
default: false

definitions:
new_stack:
type: object
Expand Down
4 changes: 4 additions & 0 deletions tests/fakeapi/hello.py
Expand Up @@ -218,3 +218,7 @@ def test_default_integer_body(stack_version):

def test_falsy_param(falsy):
return falsy


def test_bool_default_param(thruthiness):
return thruthiness
10 changes: 10 additions & 0 deletions tests/test_app.py
Expand Up @@ -575,3 +575,13 @@ def test_falsy_param(app):
assert resp.status_code == 200
response = json.loads(resp.data.decode())
assert response == 1

def test_bool_as_default_param(app):
app_client = app.app.test_client()
resp = app_client.get('/v1.0/test-bool-param')
assert resp.status_code == 200

resp = app_client.get('/v1.0/test-bool-param', query_string={'thruthiness': True})
assert resp.status_code == 200
response = json.loads(resp.data.decode())
assert response == True
2 changes: 2 additions & 0 deletions tests/test_utils.py
Expand Up @@ -35,9 +35,11 @@ def test_boolean():
assert utils.boolean('true')
assert utils.boolean('True')
assert utils.boolean('TRUE')
assert utils.boolean(True)
assert not utils.boolean('false')
assert not utils.boolean('False')
assert not utils.boolean('FALSE')
assert not utils.boolean(False)

with pytest.raises(ValueError):
utils.boolean('foo')
Expand Down

0 comments on commit 6a6144f

Please sign in to comment.