From 302d893d7e1f1c4205f2723fb7da4c43424d608e Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Fri, 26 Feb 2021 11:41:18 -0800 Subject: [PATCH 01/14] ENH: Add an ObjectCaster. --- openapi_core/casting/schemas/casters.py | 16 ++++++++++++++++ openapi_core/casting/schemas/factories.py | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/openapi_core/casting/schemas/casters.py b/openapi_core/casting/schemas/casters.py index 554f36f7..560eb008 100644 --- a/openapi_core/casting/schemas/casters.py +++ b/openapi_core/casting/schemas/casters.py @@ -37,3 +37,19 @@ def __call__(self, value): if value in (None, NoValue): return value return list(map(self.items_caster, value)) + + +class ObjectCaster(object): + + def __init__(self, schema, casters_factory): + self.schema = schema + self.casters_factory = casters_factory + + def __call__(self, value): + if value in (None, NoValue): + return value + casted = {} + for key, schema in self.schema.get_all_properties().items(): + if key in value: + casted[key] = self.casters_factory.create(schema)(value[key]) + return casted diff --git a/openapi_core/casting/schemas/factories.py b/openapi_core/casting/schemas/factories.py index 80528892..a21ad23b 100644 --- a/openapi_core/casting/schemas/factories.py +++ b/openapi_core/casting/schemas/factories.py @@ -1,7 +1,7 @@ from openapi_core.schema.schemas.enums import SchemaType from openapi_core.casting.schemas.casters import ( - PrimitiveCaster, DummyCaster, ArrayCaster + PrimitiveCaster, DummyCaster, ArrayCaster, ObjectCaster ) from openapi_core.casting.schemas.util import forcebool @@ -9,7 +9,7 @@ class SchemaCastersFactory(object): DUMMY_CASTERS = [ - SchemaType.STRING, SchemaType.OBJECT, SchemaType.ANY, + SchemaType.STRING, SchemaType.ANY, ] PRIMITIVE_CASTERS = { SchemaType.INTEGER: int, @@ -18,6 +18,7 @@ class SchemaCastersFactory(object): } COMPLEX_CASTERS = { SchemaType.ARRAY: ArrayCaster, + SchemaType.OBJECT: ObjectCaster } def create(self, schema): From 4db0e02081c31c044821a4a3187ec0554694c267 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Tue, 2 Mar 2021 08:37:34 -0800 Subject: [PATCH 02/14] MNT: Revert changes back. --- openapi_core/casting/schemas/casters.py | 18 +++--------------- openapi_core/casting/schemas/factories.py | 4 ++-- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/openapi_core/casting/schemas/casters.py b/openapi_core/casting/schemas/casters.py index 560eb008..7e2a197b 100644 --- a/openapi_core/casting/schemas/casters.py +++ b/openapi_core/casting/schemas/casters.py @@ -36,20 +36,8 @@ def items_caster(self): def __call__(self, value): if value in (None, NoValue): return value - return list(map(self.items_caster, value)) - - -class ObjectCaster(object): - def __init__(self, schema, casters_factory): - self.schema = schema - self.casters_factory = casters_factory + if not isinstance(value, list): + raise CastError(value, self.schema.type.value) - def __call__(self, value): - if value in (None, NoValue): - return value - casted = {} - for key, schema in self.schema.get_all_properties().items(): - if key in value: - casted[key] = self.casters_factory.create(schema)(value[key]) - return casted + return list(map(self.items_caster, value)) diff --git a/openapi_core/casting/schemas/factories.py b/openapi_core/casting/schemas/factories.py index a21ad23b..dc2bae60 100644 --- a/openapi_core/casting/schemas/factories.py +++ b/openapi_core/casting/schemas/factories.py @@ -9,7 +9,7 @@ class SchemaCastersFactory(object): DUMMY_CASTERS = [ - SchemaType.STRING, SchemaType.ANY, + SchemaType.STRING, SchemaType.ANY, SchemaType.OBJECT ] PRIMITIVE_CASTERS = { SchemaType.INTEGER: int, @@ -18,7 +18,7 @@ class SchemaCastersFactory(object): } COMPLEX_CASTERS = { SchemaType.ARRAY: ArrayCaster, - SchemaType.OBJECT: ObjectCaster + # SchemaType.OBJECT: ObjectCaster } def create(self, schema): From b8608919d0e15602d47ca8c4789a9e1bdec99e31 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Tue, 2 Mar 2021 08:38:05 -0800 Subject: [PATCH 03/14] ENH: Move casting into the unmarshaller. --- .../unmarshalling/schemas/unmarshallers.py | 26 +++++++++++++++---- .../validation/response/validators.py | 7 +---- openapi_core/validation/validators.py | 10 ------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index 83a47dc9..ad77f8bb 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -20,6 +20,8 @@ UnmarshalError, ValidateError, InvalidSchemaValue, InvalidSchemaFormatValue, ) +from openapi_core.casting.schemas.exceptions import CastError + from openapi_core.unmarshalling.schemas.formatters import Formatter from openapi_core.unmarshalling.schemas.util import ( forcebool, format_date, format_byte, format_uuid, @@ -44,9 +46,22 @@ def __call__(self, value=NoValue): if value is None: return - self.validate(value) + try: + casted = self._cast(self.schema, value) + except CastError as exc: + raise InvalidSchemaValue(value, self.schema.type) + self.validate(casted) + + return self.unmarshal(casted) + + def _cast(self, schema, value): + if not schema: + return value - return self.unmarshal(value) + from openapi_core.casting.schemas.factories import SchemaCastersFactory + casters_factory = SchemaCastersFactory() + caster = casters_factory.create(schema) + return caster(value) def _formatter_validate(self, value): result = self.formatter.validate(value) @@ -63,7 +78,7 @@ def validate(self, value): def unmarshal(self, value): try: return self.formatter.unmarshal(value) - except ValueError as exc: + except (ValueError, CastError) as exc: raise InvalidSchemaFormatValue( value, self.schema.format, exc) @@ -262,11 +277,12 @@ def unmarshal(self, value=NoValue): self.schema, type_override=schema_type) # validate with validator of formatter (usualy type validator) try: - unmarshaller._formatter_validate(value) + casted = self._cast(self.schema, value) + unmarshaller._formatter_validate(casted) except ValidateError: continue else: - return unmarshaller(value) + return unmarshaller(casted) log.warning("failed to unmarshal any type") return value diff --git a/openapi_core/validation/response/validators.py b/openapi_core/validation/response/validators.py index 10acdc93..57a3a511 100644 --- a/openapi_core/validation/response/validators.py +++ b/openapi_core/validation/response/validators.py @@ -85,12 +85,7 @@ def _get_data(self, response, operation_response): return None, [exc, ] try: - casted = self._cast(media_type, deserialised) - except CastError as exc: - return None, [exc, ] - - try: - data = self._unmarshal(media_type, casted) + data = self._unmarshal(media_type, deserialised) except (ValidateError, UnmarshalError) as exc: return None, [exc, ] diff --git a/openapi_core/validation/validators.py b/openapi_core/validation/validators.py index 4d3639ca..7ab5c7e4 100644 --- a/openapi_core/validation/validators.py +++ b/openapi_core/validation/validators.py @@ -30,16 +30,6 @@ def _deserialise_media_type(self, media_type, value): deserializer = deserializers_factory.create(media_type) return deserializer(value) - def _cast(self, param_or_media_type, value): - # return param_or_media_type.cast(value) - if not param_or_media_type.schema: - return value - - from openapi_core.casting.schemas.factories import SchemaCastersFactory - casters_factory = SchemaCastersFactory() - caster = casters_factory.create(param_or_media_type.schema) - return caster(value) - def _unmarshal(self, param_or_media_type, value, context): if not param_or_media_type.schema: return value From 0836dffadeccbcd1d0506f00cbb733ce652b15aa Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Tue, 2 Mar 2021 08:39:37 -0800 Subject: [PATCH 04/14] TST: Adjust tests to account for casting in unmarshaller. --- tests/unit/unmarshalling/test_unmarshal.py | 45 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 906689e5..526a9119 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -294,16 +294,25 @@ def test_integer_valid(self, unmarshaller_factory): assert result == int(value) - def test_integer_string_invalid(self, unmarshaller_factory): + def test_integer_string_cast(self, unmarshaller_factory): schema = Schema('integer') value = '123' + expected = 123 + + result = unmarshaller_factory(schema)(value) + + assert result == expected + + def test_integer_string_invalid(self, unmarshaller_factory): + schema = Schema('integer') + value = 'not-a-number' with pytest.raises(InvalidSchemaValue): unmarshaller_factory(schema)(value) def test_integer_enum_invalid(self, unmarshaller_factory): schema = Schema('integer', enum=[1, 2, 3]) - value = '123' + value = 4 with pytest.raises(UnmarshalError): unmarshaller_factory(schema)(value) @@ -316,12 +325,13 @@ def test_integer_enum(self, unmarshaller_factory): assert result == int(value) - def test_integer_enum_string_invalid(self, unmarshaller_factory): + def test_integer_enum_string_cast(self, unmarshaller_factory): schema = Schema('integer', enum=[1, 2, 3]) value = '2' - with pytest.raises(UnmarshalError): - unmarshaller_factory(schema)(value) + result = unmarshaller_factory(schema)(value) + + assert result == int(value) def test_integer_default(self, unmarshaller_factory): default_value = 123 @@ -399,9 +409,18 @@ def test_boolean_valid(self, unmarshaller_factory): assert result == value - def test_boolean_string_invalid(self, unmarshaller_factory): + def test_boolean_string_cast(self, unmarshaller_factory): schema = Schema('boolean') value = 'True' + expected = True + + result = unmarshaller_factory(schema)(value) + + assert result == expected + + def test_boolean_string_invalid(self, unmarshaller_factory): + schema = Schema('boolean') + value = 'positive' with pytest.raises(InvalidSchemaValue): unmarshaller_factory(schema)(value) @@ -414,15 +433,25 @@ def test_number_valid(self, unmarshaller_factory): assert result == value - def test_number_string_invalid(self, unmarshaller_factory): + def test_number_string_cast(self, unmarshaller_factory): schema = Schema('number') value = '1.23' + expected = 1.23 + + # with pytest.raises(InvalidSchemaValue): + result = unmarshaller_factory(schema)(value) + + assert result == expected + + def test_number_string_invalid(self, unmarshaller_factory): + schema = Schema('number') + value = 'not-a-number' with pytest.raises(InvalidSchemaValue): unmarshaller_factory(schema)(value) def test_number_int(self, unmarshaller_factory): - schema = Schema('number') + schema = Schema('integer') value = 1 result = unmarshaller_factory(schema)(value) From 73bbb8a6b3b8048acee6c0b8f82a75d4025816cb Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 07:05:00 -0800 Subject: [PATCH 05/14] TST: Test setup doesnt allow for testing enums. --- tests/unit/unmarshalling/test_unmarshal.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 526a9119..e06e61e9 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -310,13 +310,6 @@ def test_integer_string_invalid(self, unmarshaller_factory): with pytest.raises(InvalidSchemaValue): unmarshaller_factory(schema)(value) - def test_integer_enum_invalid(self, unmarshaller_factory): - schema = Schema('integer', enum=[1, 2, 3]) - value = 4 - - with pytest.raises(UnmarshalError): - unmarshaller_factory(schema)(value) - def test_integer_enum(self, unmarshaller_factory): schema = Schema('integer', enum=[1, 2, 3]) value = 2 From 817c2475a51050814419dd7d4b7749a7148d69b8 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:13:40 -0800 Subject: [PATCH 06/14] MNT: Remove references to ObjectCaster. --- openapi_core/casting/schemas/factories.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/openapi_core/casting/schemas/factories.py b/openapi_core/casting/schemas/factories.py index dc2bae60..2d06ae35 100644 --- a/openapi_core/casting/schemas/factories.py +++ b/openapi_core/casting/schemas/factories.py @@ -1,7 +1,7 @@ from openapi_core.schema.schemas.enums import SchemaType from openapi_core.casting.schemas.casters import ( - PrimitiveCaster, DummyCaster, ArrayCaster, ObjectCaster + PrimitiveCaster, DummyCaster, ArrayCaster ) from openapi_core.casting.schemas.util import forcebool @@ -18,7 +18,6 @@ class SchemaCastersFactory(object): } COMPLEX_CASTERS = { SchemaType.ARRAY: ArrayCaster, - # SchemaType.OBJECT: ObjectCaster } def create(self, schema): From ed773ac2126e9f7cb4fa57e9c2d225edb57dacbe Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:14:11 -0800 Subject: [PATCH 07/14] ENH: Remove casting from validator. --- openapi_core/validation/request/validators.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index 4e5c4af0..e5a372fd 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -124,7 +124,7 @@ def _get_parameters(self, request, params): except MissingParameter: if not param.schema or not param.schema.has_default(): continue - casted = param.schema.default + deserialised = param.schema.default else: try: deserialised = self._deserialise_parameter( @@ -133,14 +133,8 @@ def _get_parameters(self, request, params): errors.append(exc) continue - try: - casted = self._cast(param, deserialised) - except CastError as exc: - errors.append(exc) - continue - try: - unmarshalled = self._unmarshal(param, casted) + unmarshalled = self._unmarshal(param, deserialised) except (ValidateError, UnmarshalError) as exc: errors.append(exc) else: @@ -169,12 +163,7 @@ def _get_body(self, request, operation): return None, [exc, ] try: - casted = self._cast(media_type, deserialised) - except CastError as exc: - return None, [exc, ] - - try: - body = self._unmarshal(media_type, casted) + body = self._unmarshal(media_type, deserialised) except (ValidateError, UnmarshalError) as exc: return None, [exc, ] From 00495a0728952b01148bf974bd02e3a4807076b9 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:14:48 -0800 Subject: [PATCH 08/14] ENH: Pass CastError to InvalidSchemaValueError. --- openapi_core/unmarshalling/schemas/unmarshallers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index ad77f8bb..bf699fb0 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -49,7 +49,7 @@ def __call__(self, value=NoValue): try: casted = self._cast(self.schema, value) except CastError as exc: - raise InvalidSchemaValue(value, self.schema.type) + raise InvalidSchemaValue(value, self.schema.type, schema_errors=exc) self.validate(casted) return self.unmarshal(casted) From 7fd42075aa8321a4f6f0a38099858c6e7c7618a9 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:17:03 -0800 Subject: [PATCH 09/14] TST: Fix tests for new casting behavior. --- .../contrib/falcon/test_falcon_middlewares.py | 9 ++++++--- tests/integration/contrib/flask/test_flask_decorator.py | 9 ++++++--- tests/integration/contrib/flask/test_flask_views.py | 9 ++++++--- tests/integration/validation/test_petstore.py | 2 +- tests/integration/validation/test_validators.py | 2 +- tests/unit/unmarshalling/test_unmarshal.py | 2 +- 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/integration/contrib/falcon/test_falcon_middlewares.py b/tests/integration/contrib/falcon/test_falcon_middlewares.py index fbed4339..e831b233 100644 --- a/tests/integration/contrib/falcon/test_falcon_middlewares.py +++ b/tests/integration/contrib/falcon/test_falcon_middlewares.py @@ -166,16 +166,19 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( - "Failed to cast value invalidparameter to type integer" + "Value invalidparameter not valid for schema of type " + "SchemaType.INTEGER: Failed to cast value " + "invalidparameter to type integer" ) } ] } + assert result.status_code == 400 assert result.json == expected_data def test_valid(self, client): diff --git a/tests/integration/contrib/flask/test_flask_decorator.py b/tests/integration/contrib/flask/test_flask_decorator.py index afa5ad20..18782d04 100644 --- a/tests/integration/contrib/flask/test_flask_decorator.py +++ b/tests/integration/contrib/flask/test_flask_decorator.py @@ -151,16 +151,19 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( - "Failed to cast value invalidparameter to type integer" + "Value invalidparameter not valid for schema of type " + "SchemaType.INTEGER: Failed to cast value " + "invalidparameter to type integer" ) } ] } + assert result.status_code == 400 assert result.json == expected_data def test_valid(self, client): diff --git a/tests/integration/contrib/flask/test_flask_views.py b/tests/integration/contrib/flask/test_flask_views.py index 92355e2e..7eee6975 100644 --- a/tests/integration/contrib/flask/test_flask_views.py +++ b/tests/integration/contrib/flask/test_flask_views.py @@ -144,17 +144,20 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( - "Failed to cast value invalidparameter to type integer" + "Value invalidparameter not valid for schema of type " + "SchemaType.INTEGER: Failed to cast value " + "invalidparameter to type integer" ) } ] } assert result.status_code == 400 + print(result.json) assert result.json == expected_data def test_valid(self, client): diff --git a/tests/integration/validation/test_petstore.py b/tests/integration/validation/test_petstore.py index c483c03e..54d038e5 100644 --- a/tests/integration/validation/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -302,7 +302,7 @@ def test_get_pets_wrong_parameter_type(self, spec): path_pattern=path_pattern, args=query_params, ) - with pytest.raises(CastError): + with pytest.raises(InvalidSchemaValue): validate_parameters(spec, request) body = validate_body(spec, request) diff --git a/tests/integration/validation/test_validators.py b/tests/integration/validation/test_validators.py index e244dfc4..cdb51082 100644 --- a/tests/integration/validation/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -345,7 +345,7 @@ def test_request_invalid_param(self, validator): result = validator.validate(request) assert len(result.errors) == 1 - assert type(result.errors[0]) == CastError + assert type(result.errors[0]) == InvalidSchemaValue assert result.body is None assert result.parameters == RequestParameters() diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index e06e61e9..77153bf1 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -12,7 +12,7 @@ from openapi_core.unmarshalling.schemas.enums import UnmarshalContext from openapi_core.unmarshalling.schemas.exceptions import ( InvalidSchemaFormatValue, InvalidSchemaValue, UnmarshalError, - FormatterNotFoundError, + FormatterNotFoundError ) from openapi_core.unmarshalling.schemas.factories import ( SchemaUnmarshallersFactory, From 49ee3c115aa35e81dd174b0971b1517b9e572108 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:54:11 -0800 Subject: [PATCH 10/14] MNT: Revert changes. --- openapi_core/casting/schemas/factories.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_core/casting/schemas/factories.py b/openapi_core/casting/schemas/factories.py index 2d06ae35..9982ca98 100644 --- a/openapi_core/casting/schemas/factories.py +++ b/openapi_core/casting/schemas/factories.py @@ -9,7 +9,7 @@ class SchemaCastersFactory(object): DUMMY_CASTERS = [ - SchemaType.STRING, SchemaType.ANY, SchemaType.OBJECT + SchemaType.STRING, SchemaType.OBJECT, SchemaType.ANY ] PRIMITIVE_CASTERS = { SchemaType.INTEGER: int, From 58ec5a126e501a31a447ab2c04284f1035eea975 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:54:55 -0800 Subject: [PATCH 11/14] ENH: Remove unecessary cast in AnyUnmarshaller. --- openapi_core/unmarshalling/schemas/unmarshallers.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index bf699fb0..ab9b42db 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -21,7 +21,6 @@ InvalidSchemaFormatValue, ) from openapi_core.casting.schemas.exceptions import CastError - from openapi_core.unmarshalling.schemas.formatters import Formatter from openapi_core.unmarshalling.schemas.util import ( forcebool, format_date, format_byte, format_uuid, @@ -78,7 +77,7 @@ def validate(self, value): def unmarshal(self, value): try: return self.formatter.unmarshal(value) - except (ValueError, CastError) as exc: + except ValueError as exc: raise InvalidSchemaFormatValue( value, self.schema.format, exc) @@ -277,12 +276,11 @@ def unmarshal(self, value=NoValue): self.schema, type_override=schema_type) # validate with validator of formatter (usualy type validator) try: - casted = self._cast(self.schema, value) - unmarshaller._formatter_validate(casted) + unmarshaller._formatter_validate(value) except ValidateError: continue else: - return unmarshaller(casted) + return unmarshaller(value) log.warning("failed to unmarshal any type") return value From 9221a4a38981130e4a0b1d6e5637aa18d5b63fd8 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 11:55:14 -0800 Subject: [PATCH 12/14] MNT: Remove debug cruft. --- tests/integration/contrib/flask/test_flask_views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/contrib/flask/test_flask_views.py b/tests/integration/contrib/flask/test_flask_views.py index 7eee6975..06f3e378 100644 --- a/tests/integration/contrib/flask/test_flask_views.py +++ b/tests/integration/contrib/flask/test_flask_views.py @@ -157,7 +157,6 @@ def test_endpoint_error(self, client): ] } assert result.status_code == 400 - print(result.json) assert result.json == expected_data def test_valid(self, client): From 2d74b79976f6149de1b5e6d2c67d27f184d33848 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 12:15:48 -0800 Subject: [PATCH 13/14] LNT: Fix lints. --- openapi_core/unmarshalling/schemas/unmarshallers.py | 3 ++- openapi_core/validation/request/validators.py | 1 - openapi_core/validation/response/validators.py | 1 - tests/integration/contrib/falcon/test_falcon_middlewares.py | 4 ++-- tests/integration/contrib/flask/test_flask_decorator.py | 4 ++-- tests/integration/contrib/flask/test_flask_views.py | 4 ++-- tests/integration/validation/test_petstore.py | 1 - tests/integration/validation/test_validators.py | 1 - tests/unit/unmarshalling/test_unmarshal.py | 3 +-- 9 files changed, 9 insertions(+), 13 deletions(-) diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index ab9b42db..314ceb56 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -48,7 +48,8 @@ def __call__(self, value=NoValue): try: casted = self._cast(self.schema, value) except CastError as exc: - raise InvalidSchemaValue(value, self.schema.type, schema_errors=exc) + raise InvalidSchemaValue( + value, self.schema.type, schema_errors=exc) self.validate(casted) return self.unmarshal(casted) diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index e5a372fd..d6ef8bf3 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -2,7 +2,6 @@ from itertools import chain from six import iteritems -from openapi_core.casting.schemas.exceptions import CastError from openapi_core.deserializing.exceptions import DeserializeError from openapi_core.schema.media_types.exceptions import InvalidContentType from openapi_core.schema.parameters.exceptions import ( diff --git a/openapi_core/validation/response/validators.py b/openapi_core/validation/response/validators.py index 57a3a511..4976fce0 100644 --- a/openapi_core/validation/response/validators.py +++ b/openapi_core/validation/response/validators.py @@ -1,5 +1,4 @@ """OpenAPI core validation response validators module""" -from openapi_core.casting.schemas.exceptions import CastError from openapi_core.deserializing.exceptions import DeserializeError from openapi_core.schema.media_types.exceptions import InvalidContentType from openapi_core.schema.responses.exceptions import ( diff --git a/tests/integration/contrib/falcon/test_falcon_middlewares.py b/tests/integration/contrib/falcon/test_falcon_middlewares.py index e831b233..f78b6018 100644 --- a/tests/integration/contrib/falcon/test_falcon_middlewares.py +++ b/tests/integration/contrib/falcon/test_falcon_middlewares.py @@ -166,8 +166,8 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( diff --git a/tests/integration/contrib/flask/test_flask_decorator.py b/tests/integration/contrib/flask/test_flask_decorator.py index 18782d04..3d4b223f 100644 --- a/tests/integration/contrib/flask/test_flask_decorator.py +++ b/tests/integration/contrib/flask/test_flask_decorator.py @@ -151,8 +151,8 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( diff --git a/tests/integration/contrib/flask/test_flask_views.py b/tests/integration/contrib/flask/test_flask_views.py index 06f3e378..d16fc04f 100644 --- a/tests/integration/contrib/flask/test_flask_views.py +++ b/tests/integration/contrib/flask/test_flask_views.py @@ -144,8 +144,8 @@ def test_endpoint_error(self, client): 'errors': [ { 'class': ( - "" + "" ), 'status': 400, 'title': ( diff --git a/tests/integration/validation/test_petstore.py b/tests/integration/validation/test_petstore.py index 54d038e5..394b5782 100644 --- a/tests/integration/validation/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -6,7 +6,6 @@ from isodate.tzinfo import UTC from six import text_type -from openapi_core.casting.schemas.exceptions import CastError from openapi_core.deserializing.exceptions import DeserializeError from openapi_core.deserializing.parameters.exceptions import ( EmptyParameterValue, diff --git a/tests/integration/validation/test_validators.py b/tests/integration/validation/test_validators.py index cdb51082..ad1efd39 100644 --- a/tests/integration/validation/test_validators.py +++ b/tests/integration/validation/test_validators.py @@ -3,7 +3,6 @@ import pytest from six import text_type -from openapi_core.casting.schemas.exceptions import CastError from openapi_core.deserializing.exceptions import DeserializeError from openapi_core.schema.media_types.exceptions import ( InvalidContentType, diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 77153bf1..5f42d875 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -11,8 +11,7 @@ from openapi_core.schema.schemas.types import NoValue from openapi_core.unmarshalling.schemas.enums import UnmarshalContext from openapi_core.unmarshalling.schemas.exceptions import ( - InvalidSchemaFormatValue, InvalidSchemaValue, UnmarshalError, - FormatterNotFoundError + InvalidSchemaFormatValue, InvalidSchemaValue, FormatterNotFoundError ) from openapi_core.unmarshalling.schemas.factories import ( SchemaUnmarshallersFactory, From 939222582ffdc9349cf0874b0dfc5821fb63a605 Mon Sep 17 00:00:00 2001 From: Sebastian Mainberger Date: Wed, 3 Mar 2021 12:33:49 -0800 Subject: [PATCH 14/14] TST: debug test_string_format_datetime_invalid. --- tests/unit/unmarshalling/test_unmarshal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 5f42d875..deb078d5 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -205,7 +205,7 @@ def test_string_format_date(self, unmarshaller_factory): def test_string_format_datetime_invalid(self, unmarshaller_factory): schema = Schema('string', schema_format='date-time') - value = '2018-01-02T00:00:00' + value = '2018-01-02A00:00:00' with pytest.raises(InvalidSchemaValue): unmarshaller_factory(schema)(value)