openapi-core/openapi_core/deserializing/parameters/deserializers.py

31 lines
1.1 KiB
Python
Raw Normal View History

from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.parameters.exceptions import (
EmptyParameterValue,
)
2021-04-27 21:39:28 +00:00
from openapi_core.schema.parameters import get_aslist, get_explode, get_style
class PrimitiveDeserializer(object):
def __init__(self, param, deserializer_callable):
self.param = param
self.deserializer_callable = deserializer_callable
2021-04-23 11:36:27 +00:00
self.aslist = get_aslist(self.param)
self.explode = get_explode(self.param)
self.style = get_style(self.param)
def __call__(self, value):
2021-04-23 11:36:27 +00:00
style = get_style(self.param)
if (self.param['in'] == 'query' and value == "" and
not self.param.getkey('allowEmptyValue', False)):
raise EmptyParameterValue(
2021-04-23 11:36:27 +00:00
value, self.style, self.param['name'])
2021-04-23 11:36:27 +00:00
if not self.aslist or self.explode:
return value
try:
return self.deserializer_callable(value)
2020-02-03 13:41:26 +00:00
except (ValueError, TypeError, AttributeError):
2021-04-23 11:36:27 +00:00
raise DeserializeError(value, self.style)