Skip to content

Commit

Permalink
RESTAPI: treat 'false' as False in url parsing (#5573)
Browse files Browse the repository at this point in the history
fixes #3635

The parser function that we use to parse the query string doesn't parse booleans as expected.
When passing `false` in URL it will pared as `True` which is not correct.
This PR fixes it by comparing the `true` or `false` attribute in url directly with the string `'true'` and return bool type.

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Cherry-pick: 7772adc
  • Loading branch information
eimrek authored and sphuber committed Jul 13, 2022
1 parent 6d22744 commit aac8e43
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aiida/restapi/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ def parse_query_string(self, query_string):
)
# Value types
value_num = ppc.number
value_bool = (Literal('true') | Literal('false')).addParseAction(lambda toks: bool(toks[0]))
value_bool = (Literal('true') | Literal('false')).addParseAction(lambda toks: toks[0] == 'true')
value_string = QuotedString('"', escQuote='""')
value_orderby = Combine(Optional(Word('+-', exact=1)) + key)

Expand Down
5 changes: 3 additions & 2 deletions tests/restapi/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,16 @@ def test_structure_download(self):
structure_data = load_node(node_uuid)._exportcontent('xsf')[0] # pylint: disable=protected-access
assert rv_obj.data == structure_data

def test_structure_download_false(self):
@pytest.mark.parametrize('download', ['false', 'False'])
def test_structure_download_false(self, download):
"""
Test download=false that displays the content in the browser instead
of downloading the structure file
"""
from aiida.orm import load_node

node_uuid = self.get_dummy_data()['structuredata'][0]['uuid']
url = f'{self.get_url_prefix()}/nodes/{node_uuid}/download?download_format=xsf&download=False'
url = f'{self.get_url_prefix()}/nodes/{node_uuid}/download?download_format=xsf&download={download}'
with self.app.test_client() as client:
rv_obj = client.get(url)
response = json.loads(rv_obj.data)
Expand Down

0 comments on commit aac8e43

Please sign in to comment.