From 63a79712605c129eed8d6e9705298f8f1cfe15a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Thu, 5 Jan 2017 17:39:10 +0100 Subject: [PATCH] Handle errors on converting invalid integer/float values When trying to convert integer/float values and the passed value is invalid (like a string containing non-digit characters), handle the resulting ValueError and use the original value which will cause a proper validation error later on. Fixes #83. --- swagger_py_codegen/templates/flask/validators.tpl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/swagger_py_codegen/templates/flask/validators.tpl b/swagger_py_codegen/templates/flask/validators.tpl index 49a2a24..b60827a 100644 --- a/swagger_py_codegen/templates/flask/validators.tpl +++ b/swagger_py_codegen/templates/flask/validators.tpl @@ -31,6 +31,12 @@ class FlaskValidatorAdaptor(object): def __init__(self, schema): self.validator = Draft4Validator(schema) + def validate_number(self, type_, value): + try: + return type_(value) + except ValueError: + return value + def type_convert(self, obj): if obj is None: return None @@ -41,10 +47,10 @@ class FlaskValidatorAdaptor(object): result = dict() convert_funs = { - 'integer': lambda v: int(v[0]), + 'integer': lambda v: self.validate_number(int, v[0]), 'boolean': lambda v: v[0].lower() not in ['n', 'no', 'false', '', '0'], 'null': lambda v: None, - 'number': lambda v: float(v[0]), + 'number': lambda v: self.validate_number(float, v[0]), 'string': lambda v: v[0] }