From 016acdc6a8734a225f1ce5d885d63956eea214d3 Mon Sep 17 00:00:00 2001 From: Hoss Date: Thu, 13 Jun 2024 11:51:09 +0800 Subject: [PATCH] fix: DSVError are not JSON serializable --- data_spec_validator/decorator/decorators.py | 2 +- data_spec_validator/spec/features.py | 4 +++- test/test_decorator_dj.py | 3 ++- test/test_decorator_drf.py | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/data_spec_validator/decorator/decorators.py b/data_spec_validator/decorator/decorators.py index 45ce6ae..25d5446 100644 --- a/data_spec_validator/decorator/decorators.py +++ b/data_spec_validator/decorator/decorators.py @@ -165,7 +165,7 @@ def _get_error_response(error, use_drf): Return the error response based on the error type. If the attribute use_drf is True, Raise DRF's exception to let DRF's exception handler do something about it. """ - error_msg = {'messages': error.message} + error_msg = {'messages': [str(err) for err in error.message]} if use_drf: err_map = { diff --git a/data_spec_validator/spec/features.py b/data_spec_validator/spec/features.py index 80524a7..422cb70 100644 --- a/data_spec_validator/spec/features.py +++ b/data_spec_validator/spec/features.py @@ -61,7 +61,9 @@ def wrap(cls: Type) -> Type: def get_spec_name(spec) -> str: - return getattr(spec, _FEAT_PARAMS).spec_name if hasattr(spec, _FEAT_PARAMS) else spec.__name__ + if hasattr(spec, _FEAT_PARAMS) and getattr(spec, _FEAT_PARAMS).spec_name: + return getattr(spec, _FEAT_PARAMS).spec_name + return spec.__name__ def get_err_mode(spec) -> ErrorMode: diff --git a/test/test_decorator_dj.py b/test/test_decorator_dj.py index e82d5e3..3ec5914 100644 --- a/test/test_decorator_dj.py +++ b/test/test_decorator_dj.py @@ -7,7 +7,7 @@ from data_spec_validator.decorator import dsv, dsv_request_meta from data_spec_validator.decorator.decorators import ParseError -from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, dsv_feature +from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, ErrorMode, dsv_feature from .utils import is_django_installed, make_request @@ -223,6 +223,7 @@ def decorated_func(self, request, field_a): def test_json_response_content(self): # arrange + @dsv_feature(err_mode=ErrorMode.ALL) class _ViewSpec: named_arg = Checker([DIGIT_STR]) diff --git a/test/test_decorator_drf.py b/test/test_decorator_drf.py index c813328..92f05f5 100644 --- a/test/test_decorator_drf.py +++ b/test/test_decorator_drf.py @@ -5,7 +5,7 @@ from parameterized import parameterized from data_spec_validator.decorator import dsv, dsv_request_meta -from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, dsv_feature +from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, ErrorMode, dsv_feature from .utils import is_drf_installed, make_request @@ -178,6 +178,7 @@ def decorated_func(self, request, field_a): def test_json_response_content(self): # arrange + @dsv_feature(err_mode=ErrorMode.ALL) class _ViewSpec: field_a = Checker([DIGIT_STR])