utils: accept empty value on date type with None default value (#74089)
This commit is contained in:
parent
87c0698f55
commit
91fa126653
|
@ -388,12 +388,19 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
|
|||
except ValueError:
|
||||
raise InvalidParameterValue(parameter)
|
||||
elif parameter_info.get('type') == 'date':
|
||||
try:
|
||||
d[parameter] = parse_date(d[parameter].strip())
|
||||
except ValueError:
|
||||
raise InvalidParameterValue('%s (not a valid date)' % parameter)
|
||||
if not d[parameter]:
|
||||
raise InvalidParameterValue('%s (YYYY-MM-DD expected)' % parameter)
|
||||
if (
|
||||
parameter_info.get('optional')
|
||||
and parameter_info.get('default_value') is None
|
||||
and not d[parameter]
|
||||
):
|
||||
d[parameter] = None
|
||||
else:
|
||||
try:
|
||||
d[parameter] = parse_date(d[parameter].strip())
|
||||
except ValueError:
|
||||
raise InvalidParameterValue('%s (not a valid date)' % parameter)
|
||||
if not d[parameter]:
|
||||
raise InvalidParameterValue('%s (YYYY-MM-DD expected)' % parameter)
|
||||
if request.method == 'POST' and self.endpoint.endpoint_info.post:
|
||||
request_body = self.endpoint.endpoint_info.post.get('request_body', {})
|
||||
if 'application/json' in request_body.get('schema', {}):
|
||||
|
|
|
@ -748,6 +748,8 @@ def test_endpoint_typed_params(app, db, monkeypatch):
|
|||
connector.save()
|
||||
|
||||
json_res = app.get('/stub-invoices/fake/httpcall').json
|
||||
assert json_res == {'boolean': False, 'integer': 1, 'floating': 1.1, 'date': None, 'err': 0}
|
||||
|
||||
json_res = app.get('/stub-invoices/fake/httpcall?boolean=True').json
|
||||
assert json_res['boolean'] is True
|
||||
json_res = app.get('/stub-invoices/fake/httpcall?boolean=on').json
|
||||
|
@ -788,12 +790,12 @@ def test_endpoint_typed_params(app, db, monkeypatch):
|
|||
json_res = app.get('/stub-invoices/fake/httpcall?date=nodate', status=400).json
|
||||
assert json_res['err'] == 1
|
||||
assert json_res['err_desc'] == 'invalid value for parameter "date (YYYY-MM-DD expected)"'
|
||||
json_res = app.get('/stub-invoices/fake/httpcall?date=', status=400).json
|
||||
assert json_res['err'] == 1
|
||||
assert json_res['err_desc'] == 'invalid value for parameter "date (YYYY-MM-DD expected)"'
|
||||
json_res = app.get('/stub-invoices/fake/httpcall?date=1970-02-31', status=400).json
|
||||
assert json_res['err'] == 1
|
||||
assert json_res['err_desc'] == 'invalid value for parameter "date (not a valid date)"'
|
||||
json_res = app.get('/stub-invoices/fake/httpcall?date=').json
|
||||
assert not json_res['err']
|
||||
assert json_res['date'] is None
|
||||
|
||||
|
||||
def test_endpoint_params_type_detection(app, db, monkeypatch):
|
||||
|
|
Loading…
Reference in New Issue